Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try With Resources #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions src/main/java/net/fabricmc/tinyremapper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }

Expand Down Expand Up @@ -291,7 +291,17 @@ public static Builder newRemapper() {
return new Builder();
}

/**
* @see #close
* @deprecated Use try-with-resources
*/
@Deprecated
CoolMineman marked this conversation as resolved.
Show resolved Hide resolved
public void finish() {
close();
}

@Override
public void close() {
threadPool.shutdown();

try {
Expand Down
190 changes: 92 additions & 98 deletions src/test/java/net/fabricmc/tinyremapper/IntegrationTest1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

/**
Expand All @@ -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));
}
}

/**
Expand All @@ -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";
Expand All @@ -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);
}
}

/**
Expand All @@ -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
Expand Down
Loading