From 562573b92a6c36a2ea4af73f69379acabb6e058d Mon Sep 17 00:00:00 2001 From: XiaoPangxie732 <47449269+XiaoPangxie732@users.noreply.github.com> Date: Thu, 22 Aug 2024 03:42:03 +0800 Subject: [PATCH] Fix leak and misc stuff --- .../cn/maxpixel/mcdecompiler/common/app/util/FileUtil.java | 5 +++-- .../java/cn/maxpixel/mcdecompiler/common/util/IOUtil.java | 6 +++--- .../maxpixel/mcdecompiler/mapping/NamespacedMapping.java | 5 +++-- .../mcdecompiler/mapping/util/DescriptorRemapper.java | 2 +- .../cn/maxpixel/mcdecompiler/mapping/util/MappingUtil.java | 4 ++++ .../mcdecompiler/remapper/processing/IndyRemapper.java | 7 +++---- .../processing/RuntimeParameterAnnotationFixer.java | 2 +- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/modules/app-common/src/main/java/cn/maxpixel/mcdecompiler/common/app/util/FileUtil.java b/modules/app-common/src/main/java/cn/maxpixel/mcdecompiler/common/app/util/FileUtil.java index 3fcfcbf..590d379 100644 --- a/modules/app-common/src/main/java/cn/maxpixel/mcdecompiler/common/app/util/FileUtil.java +++ b/modules/app-common/src/main/java/cn/maxpixel/mcdecompiler/common/app/util/FileUtil.java @@ -134,8 +134,9 @@ public static Stream iterateFiles(@NotNull Path path) { DirectoryStream ds = Files.newDirectoryStream(path); return StreamSupport.stream(ds.spliterator(), true) .mapMulti((Path p, Consumer cons) -> { - if (Files.isDirectory(p)) iterateFiles(p).forEach(cons); - else cons.accept(p); + if (Files.isDirectory(p)) try (var s = iterateFiles(p)) { + s.sequential().forEach(cons); + } else cons.accept(p); }).onClose(LambdaUtil.unwrap(ds::close)); } catch (IOException e) { LOGGER.fatal("Error iterating files", e); diff --git a/modules/common/src/main/java/cn/maxpixel/mcdecompiler/common/util/IOUtil.java b/modules/common/src/main/java/cn/maxpixel/mcdecompiler/common/util/IOUtil.java index d4e4a69..258b441 100644 --- a/modules/common/src/main/java/cn/maxpixel/mcdecompiler/common/util/IOUtil.java +++ b/modules/common/src/main/java/cn/maxpixel/mcdecompiler/common/util/IOUtil.java @@ -30,12 +30,12 @@ import java.util.zip.InflaterInputStream; public class IOUtil { - private static final Class ZIP_FILESYSTEM; + private static final Class ZIP_PATH; private static final Class ENTRY_INPUT_STREAM; static { try { - ZIP_FILESYSTEM = Class.forName("jdk.nio.zipfs.ZipFileSystem"); + ZIP_PATH = Class.forName("jdk.nio.zipfs.ZipPath"); ENTRY_INPUT_STREAM = Class.forName("jdk.nio.zipfs.ZipFileSystem$EntryInputStream"); } catch (ClassNotFoundException e) { throw Utils.wrapInRuntime(e); @@ -43,7 +43,7 @@ public class IOUtil { } public static byte[] readAllBytes(@NotNull Path file) throws IOException { - if (ZIP_FILESYSTEM != file.getFileSystem().getClass()) throw new IllegalArgumentException(); // Ensure the filesystem is zipfs + if (ZIP_PATH != file.getClass()) throw new IllegalArgumentException(); // Ensure this is zipfs path try (InputStream is = Files.newInputStream(file)) { byte[] bytes = new byte[is.available()]; if (is instanceof InflaterInputStream) { diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/NamespacedMapping.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/NamespacedMapping.java index 67f24a4..f0427c6 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/NamespacedMapping.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/NamespacedMapping.java @@ -165,8 +165,9 @@ public NamespacedMapping(String[] namespaces, String[] names, int nameStart, Com if (namespaces.length != (names.length - Objects.checkIndex(nameStart, names.length))) throw new IllegalArgumentException(); for (int i = 0; i < namespaces.length; i++) { - var n = i + nameStart; - this.names.put(Objects.requireNonNull(namespaces[i]), n >= names.length ? names[names.length - 1] : names[i + nameStart]); +// var n = i + nameStart;// FIXME: Maybe useless? +// this.names.put(Objects.requireNonNull(namespaces[i]), n >= names.length ? names[names.length - 1] : names[n]); + this.names.put(Objects.requireNonNull(namespaces[i]), names[i + nameStart]); } } diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/DescriptorRemapper.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/DescriptorRemapper.java index 5c3fc86..02a2f82 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/DescriptorRemapper.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/DescriptorRemapper.java @@ -14,7 +14,7 @@ import org.jetbrains.annotations.NotNull; /** - * Lightweight remapper for descriptors in place of the general heavyweight remappers. + * Lightweight remapper for descriptors in place of the general heavyweight {@link cn.maxpixel.mcdecompiler.mapping.remapper.MappingRemapper}s. */ public class DescriptorRemapper {// TODO private final Object2ObjectOpenHashMap> mappingByUnm; diff --git a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/MappingUtil.java b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/MappingUtil.java index b4c1882..1afcfac 100644 --- a/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/MappingUtil.java +++ b/modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/MappingUtil.java @@ -27,6 +27,10 @@ import org.jetbrains.annotations.Nullable; public final class MappingUtil { + private MappingUtil() { + throw new AssertionError("No instances"); + } + public static void checkOwner(Owned owned, ClassMapping owner) { if (owned.owner != owner) throw new IllegalArgumentException("Owner mismatch"); } diff --git a/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/IndyRemapper.java b/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/IndyRemapper.java index be81c89..00d30ee 100644 --- a/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/IndyRemapper.java +++ b/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/IndyRemapper.java @@ -44,13 +44,12 @@ public Remapper(MethodVisitor methodVisitor) { @Override public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) { - // We only remap name here. Other things are remapped by ASM's ClassRemapper - String newName = name; + // Only remap name here. Other things are remapped by ASM's ClassRemapper if (bootstrapMethodHandle.getOwner().equals("java/lang/invoke/LambdaMetafactory")) { Type interfaceMethodType = (Type) bootstrapMethodArguments[0]; - newName = mappingRemapper.mapMethodName(Type.getReturnType(descriptor).getInternalName(), name, interfaceMethodType.getDescriptor()); + name = mappingRemapper.mapMethodName(Type.getReturnType(descriptor).getInternalName(), name, interfaceMethodType.getDescriptor()); } - super.visitInvokeDynamicInsn(newName, descriptor, bootstrapMethodHandle, bootstrapMethodArguments); + super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments); } } } \ No newline at end of file diff --git a/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/RuntimeParameterAnnotationFixer.java b/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/RuntimeParameterAnnotationFixer.java index cdce185..edef358 100644 --- a/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/RuntimeParameterAnnotationFixer.java +++ b/modules/remapper/src/main/java/cn/maxpixel/mcdecompiler/remapper/processing/RuntimeParameterAnnotationFixer.java @@ -67,7 +67,7 @@ public void visitInnerClass(String name, String outerName, String innerName, int @Override public MethodVisitor visitMethod(int access, String name, @Subst("(Ljava/lang/String;I)V") String descriptor, String signature, String[] exceptions) { - if(toProcess != null && "".equals(name) && descriptor.startsWith(toProcess)) { + if (toProcess != null && "".equals(name) && descriptor.startsWith(toProcess)) { return new MethodVisitor(Deobfuscator.ASM_VERSION, super.visitMethod(access, name, descriptor, signature, exceptions)) { private final int params = DescriptorUtil.getArgumentCount(descriptor); private boolean processVisible;