From 2880efaa342f365126f62b80467fe3adba267712 Mon Sep 17 00:00:00 2001 From: CoolMineman <62723322+CoolMineman@users.noreply.github.com> Date: Mon, 16 Aug 2021 22:50:16 -0500 Subject: [PATCH 1/2] try with resources --- .../java/net/fabricmc/tinyremapper/Main.java | 57 +++--- .../fabricmc/tinyremapper/TinyRemapper.java | 8 +- .../tinyremapper/IntegrationTest1.java | 190 +++++++++--------- .../tinyremapper/IntegrationTest2.java | 88 ++++---- 4 files changed, 170 insertions(+), 173 deletions(-) diff --git a/src/main/java/net/fabricmc/tinyremapper/Main.java b/src/main/java/net/fabricmc/tinyremapper/Main.java index ea21d123..1856072d 100644 --- a/src/main/java/net/fabricmc/tinyremapper/Main.java +++ b/src/main/java/net/fabricmc/tinyremapper/Main.java @@ -194,34 +194,35 @@ public static void main(String[] rawArgs) { long startTime = System.nanoTime(); - TinyRemapper remapper = TinyRemapper.newRemapper() - .withMappings(TinyUtils.createTinyMappingProvider(mappings, fromM, toM)) - .ignoreFieldDesc(ignoreFieldDesc) - .withForcedPropagation(forcePropagation) - .propagatePrivate(propagatePrivate) - .propagateBridges(propagateBridges) - .removeFrames(removeFrames) - .ignoreConflicts(ignoreConflicts) - .checkPackageAccess(checkPackageAccess) - .fixPackageAccess(fixPackageAccess) - .resolveMissing(resolveMissing) - .rebuildSourceFilenames(rebuildSourceFilenames) - .skipLocalVariableMapping(skipLocalVariableMapping) - .renameInvalidLocals(renameInvalidLocals) - .threads(threads) - .build(); - - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - - remapper.readInputs(input); - remapper.readClassPath(classpath); - - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + // This checkstyle is stupid + try ( + TinyRemapper remapper = TinyRemapper.newRemapper() + .withMappings(TinyUtils.createTinyMappingProvider(mappings, fromM, toM)) + .ignoreFieldDesc(ignoreFieldDesc) + .withForcedPropagation(forcePropagation) + .propagatePrivate(propagatePrivate) + .propagateBridges(propagateBridges) + .removeFrames(removeFrames) + .ignoreConflicts(ignoreConflicts) + .checkPackageAccess(checkPackageAccess) + .fixPackageAccess(fixPackageAccess) + .resolveMissing(resolveMissing) + .rebuildSourceFilenames(rebuildSourceFilenames) + .skipLocalVariableMapping(skipLocalVariableMapping) + .renameInvalidLocals(renameInvalidLocals) + .threads(threads) + .build() + ) { + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + + remapper.readInputs(input); + remapper.readClassPath(classpath); + + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } } System.out.printf("Finished after %.2f ms.\n", (System.nanoTime() - startTime) / 1e6); diff --git a/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java b/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java index 00e8e105..6fbb52a6 100644 --- a/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java +++ b/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java @@ -69,7 +69,7 @@ import net.fabricmc.tinyremapper.api.TrMember; import net.fabricmc.tinyremapper.api.TrMember.MemberType; -public class TinyRemapper { +public class TinyRemapper implements AutoCloseable { public static class Builder { private Builder() { } @@ -291,7 +291,13 @@ public static Builder newRemapper() { return new Builder(); } + @Deprecated public void finish() { + close(); + } + + @Override + public void close() { threadPool.shutdown(); try { diff --git a/src/test/java/net/fabricmc/tinyremapper/IntegrationTest1.java b/src/test/java/net/fabricmc/tinyremapper/IntegrationTest1.java index 4c095f6e..59cdf637 100644 --- a/src/test/java/net/fabricmc/tinyremapper/IntegrationTest1.java +++ b/src/test/java/net/fabricmc/tinyremapper/IntegrationTest1.java @@ -101,33 +101,32 @@ private TinyRemapper setupRemapper() { */ @Test public void basic() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; - Path output = TestUtil.output(BASIC_INPUT_PATH); - Path input = TestUtil.input(BASIC_INPUT_PATH); + Path output = TestUtil.output(BASIC_INPUT_PATH); + Path input = TestUtil.input(BASIC_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.readInputs(input); - remapper.readClassPath(classpath); + remapper.readInputs(input); + remapper.readClassPath(classpath); - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); - } + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } - final String MAIN_CLASS = "com/github/logicf/Main.class"; - final String GREETING_CLASS = "com/github/logicf/Greeting.class"; + final String MAIN_CLASS = "com/github/logicf/Main.class"; + final String GREETING_CLASS = "com/github/logicf/Greeting.class"; - JarFile result = new JarFile(output.toFile()); - assertNotNull(result.getEntry(MAIN_CLASS)); - assertNotNull(result.getEntry(GREETING_CLASS)); - result.close(); + try (JarFile result = new JarFile(output.toFile())) { + assertNotNull(result.getEntry(MAIN_CLASS)); + assertNotNull(result.getEntry(GREETING_CLASS)); + } + } } /** @@ -137,35 +136,34 @@ public void basic() throws IOException { */ @Test public void mrj1() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; - Path output = TestUtil.output(MRJ1_INPUT_PATH); Path input = TestUtil.input(MRJ1_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; + + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.readInputs(input); - remapper.readClassPath(classpath); + remapper.readInputs(input); + remapper.readClassPath(classpath); - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } } final String MAIN_CLASS = "com/github/logicf/Main.class"; final String GREETING_CLASS = "com/github/logicf/Greeting.class"; final String MRJ_GREETING_CLASS = "META-INF/versions/9/com/github/logicf/Greeting.class"; - JarFile result = new JarFile(output.toFile()); - assertNotNull(result.getEntry(MAIN_CLASS)); - assertNotNull(result.getEntry(GREETING_CLASS)); - assertNotNull(result.getEntry(MRJ_GREETING_CLASS)); - result.close(); + try (JarFile result = new JarFile(output.toFile())) { + assertNotNull(result.getEntry(MAIN_CLASS)); + assertNotNull(result.getEntry(GREETING_CLASS)); + assertNotNull(result.getEntry(MRJ_GREETING_CLASS)); + } } /** @@ -176,24 +174,23 @@ public void mrj1() throws IOException { */ @Test public void mrj2() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; - Path output = TestUtil.output(MRJ2_INPUT_PATH); Path input = TestUtil.input(MRJ2_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; - remapper.readInputs(input); - remapper.readClassPath(classpath); + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + remapper.readInputs(input); + remapper.readClassPath(classpath); + + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } } final String J8_MAIN_CLASS = "com/github/logicf/Main.class"; @@ -204,33 +201,31 @@ public void mrj2() throws IOException { final String J9_D2_CLASS = "META-INF/versions/9/com/github/logicf/D2.class"; final String J9_D4_CLASS = "META-INF/versions/9/com/github/logicf/D4.class"; - JarFile result = new JarFile(output.toFile()); - - assertNotNull(result.getEntry(J8_MAIN_CLASS)); - assertNotNull(result.getEntry(J8_D1_CLASS)); - assertNotNull(result.getEntry(J8_D3_CLASS)); - assertNotNull(result.getEntry(J8_D5_CLASS)); - assertNotNull(result.getEntry(J9_D1_CLASS)); - assertNotNull(result.getEntry(J9_D2_CLASS)); - assertNotNull(result.getEntry(J9_D4_CLASS)); - - ClassReader readerJ8D1 = new ClassReader(result.getInputStream(result.getEntry(J8_D1_CLASS))); - readerJ8D1.accept(new ClassVisitor(Opcodes.ASM9, null) { - @Override - public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { - assertEquals("com/github/logicf/D3", superName); - } - }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); - - ClassReader readerJ9D1 = new ClassReader(result.getInputStream(result.getEntry(J9_D1_CLASS))); - readerJ9D1.accept(new ClassVisitor(Opcodes.ASM9, null) { - @Override - public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { - assertEquals("com/github/logicf/D2", superName); - } - }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); - - result.close(); + try (JarFile result = new JarFile(output.toFile())) { + assertNotNull(result.getEntry(J8_MAIN_CLASS)); + assertNotNull(result.getEntry(J8_D1_CLASS)); + assertNotNull(result.getEntry(J8_D3_CLASS)); + assertNotNull(result.getEntry(J8_D5_CLASS)); + assertNotNull(result.getEntry(J9_D1_CLASS)); + assertNotNull(result.getEntry(J9_D2_CLASS)); + assertNotNull(result.getEntry(J9_D4_CLASS)); + + ClassReader readerJ8D1 = new ClassReader(result.getInputStream(result.getEntry(J8_D1_CLASS))); + readerJ8D1.accept(new ClassVisitor(Opcodes.ASM9, null) { + @Override + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + assertEquals("com/github/logicf/D3", superName); + } + }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); + + ClassReader readerJ9D1 = new ClassReader(result.getInputStream(result.getEntry(J9_D1_CLASS))); + readerJ9D1.accept(new ClassVisitor(Opcodes.ASM9, null) { + @Override + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + assertEquals("com/github/logicf/D2", superName); + } + }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); + } } /** @@ -240,36 +235,35 @@ public void visit(int version, int access, String name, String signature, String */ @Test public void mrj4() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; - InputTag tag = remapper.createInputTag(); - Path output = TestUtil.output(MRJ1_INPUT_PATH); Path input = TestUtil.input(MRJ1_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; + InputTag tag = remapper.createInputTag(); + + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.readInputs(tag, input); - remapper.readClassPath(classpath); + remapper.readInputs(tag, input); + remapper.readClassPath(classpath); - remapper.apply(outputConsumer, tag); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + remapper.apply(outputConsumer, tag); + } catch (IOException e) { + throw new RuntimeException(e); + } } final String MAIN_CLASS = "com/github/logicf/Main.class"; final String GREETING_CLASS = "com/github/logicf/Greeting.class"; final String MRJ_GREETING_CLASS = "META-INF/versions/9/com/github/logicf/Greeting.class"; - JarFile result = new JarFile(output.toFile()); - assertNotNull(result.getEntry(MAIN_CLASS)); - assertNotNull(result.getEntry(GREETING_CLASS)); - assertNotNull(result.getEntry(MRJ_GREETING_CLASS)); - result.close(); + try (JarFile result = new JarFile(output.toFile())) { + assertNotNull(result.getEntry(MAIN_CLASS)); + assertNotNull(result.getEntry(GREETING_CLASS)); + assertNotNull(result.getEntry(MRJ_GREETING_CLASS)); + } } @AfterAll diff --git a/src/test/java/net/fabricmc/tinyremapper/IntegrationTest2.java b/src/test/java/net/fabricmc/tinyremapper/IntegrationTest2.java index 24b6b5e7..db6f10da 100644 --- a/src/test/java/net/fabricmc/tinyremapper/IntegrationTest2.java +++ b/src/test/java/net/fabricmc/tinyremapper/IntegrationTest2.java @@ -100,24 +100,23 @@ private TinyRemapper setupRemapper() { */ @Test public void access() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; - Path output = TestUtil.output(ACCESS_INPUT_PATH); Path input = TestUtil.input(ACCESS_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; + + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.readInputs(input); - remapper.readClassPath(classpath); + remapper.readInputs(input); + remapper.readClassPath(classpath); - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } } final String MAIN_CLASS = "com/github/logicf/Main.class"; @@ -157,24 +156,23 @@ public MethodVisitor visitMethod(int access, String name, String desc, String si */ @Test public void mrj3() throws IOException { - final TinyRemapper remapper = setupRemapper(); - final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; - final Path[] classpath = new Path[]{}; - Path output = TestUtil.output(MRJ3_INPUT_PATH); Path input = TestUtil.input(MRJ3_INPUT_PATH); - try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { - outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); + try (TinyRemapper remapper = setupRemapper()) { + final NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF; + final Path[] classpath = new Path[]{}; + + try (OutputConsumerPath outputConsumer = new OutputConsumerPath.Builder(output).build()) { + outputConsumer.addNonClassFiles(input, ncCopyMode, remapper); - remapper.readInputs(input); - remapper.readClassPath(classpath); + remapper.readInputs(input); + remapper.readClassPath(classpath); - remapper.apply(outputConsumer); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - remapper.finish(); + remapper.apply(outputConsumer); + } catch (IOException e) { + throw new RuntimeException(e); + } } final String J8_MAIN_CLASS = "com/github/logicf/Main.class"; @@ -182,32 +180,30 @@ public void mrj3() throws IOException { final String J8_D3_CLASS = "com/github/logicf/pkg2/D3.class"; final String J9_D1_CLASS = "META-INF/versions/9/com/github/logicf/pkg1/D1.class"; - JarFile result = new JarFile(output.toFile()); - - assertNotNull(result.getEntry(J8_MAIN_CLASS)); - assertNotNull(result.getEntry(J8_D1_CLASS)); - assertNotNull(result.getEntry(J8_D3_CLASS)); - assertNotNull(result.getEntry(J9_D1_CLASS)); + try (JarFile result = new JarFile(output.toFile())) { + assertNotNull(result.getEntry(J8_MAIN_CLASS)); + assertNotNull(result.getEntry(J8_D1_CLASS)); + assertNotNull(result.getEntry(J8_D3_CLASS)); + assertNotNull(result.getEntry(J9_D1_CLASS)); - ClassReader readerD3 = new ClassReader(result.getInputStream(result.getEntry(J8_D3_CLASS))); + ClassReader readerD3 = new ClassReader(result.getInputStream(result.getEntry(J8_D3_CLASS))); - readerD3.accept(new ClassVisitor(Opcodes.ASM9, null) { - @Override - public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { - assertNotEquals(0, (access & Opcodes.ACC_PUBLIC)); - } - - @Override - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - if (name.equals("say")) { + readerD3.accept(new ClassVisitor(Opcodes.ASM9, null) { + @Override + public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { assertNotEquals(0, (access & Opcodes.ACC_PUBLIC)); } - return super.visitMethod(access, name, desc, signature, exceptions); - } - }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); + @Override + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { + if (name.equals("say")) { + assertNotEquals(0, (access & Opcodes.ACC_PUBLIC)); + } - result.close(); + return super.visitMethod(access, name, desc, signature, exceptions); + } + }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); + } } @AfterAll From 5efaf2f4c635f946f1e75a2030b939e3c07e3cbe Mon Sep 17 00:00:00 2001 From: CoolMineman <62723322+CoolMineman@users.noreply.github.com> Date: Wed, 18 Aug 2021 17:56:56 -0500 Subject: [PATCH 2/2] deprecated javadoc --- src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java b/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java index 6fbb52a6..bd4c9515 100644 --- a/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java +++ b/src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java @@ -291,6 +291,10 @@ public static Builder newRemapper() { return new Builder(); } + /** + * @see #close + * @deprecated Use try-with-resources + */ @Deprecated public void finish() { close();