Use try-with-resources in all other situations that can use it

This commit is contained in:
Pokechu22 2018-01-24 22:44:30 -08:00 committed by md-5
parent 98cadeffbf
commit b454c85dab
8 changed files with 33 additions and 91 deletions

View File

@ -141,16 +141,10 @@ public class Jar implements Closeable {
*/ */
public ClassNode getNode(String clazz) { public ClassNode getNode(String clazz) {
// No luck, so lets try read it // No luck, so lets try read it
try { try (InputStream is = getClass(clazz)) {
InputStream is = getClass(clazz);
if (is != null) { if (is != null) {
ClassReader cr; ClassReader cr = new ClassReader(is);
// Process it // Process it
try {
cr = new ClassReader(is);
} finally {
is.close();
}
ClassNode node = new ClassNode(); ClassNode node = new ClassNode();
cr.accept(node, 0); cr.accept(node, 0);

View File

@ -486,13 +486,6 @@ public class JarMapping {
this.newJar = newJar; this.newJar = newJar;
PrintWriter out;
if (logfile == null) {
out = new PrintWriter(System.out);
} else {
out = new PrintWriter(logfile);
}
MappingWriter srgWriter = (compact) ? new CompactSearge(oldJar.jar.getFilename(), newJar.jar.getFilename()) : new Searge(oldJar.jar.getFilename(), newJar.jar.getFilename()); MappingWriter srgWriter = (compact) ? new CompactSearge(oldJar.jar.getFilename(), newJar.jar.getFilename()) : new Searge(oldJar.jar.getFilename(), newJar.jar.getFilename());
@ -528,6 +521,8 @@ public class JarMapping {
} }
} }
try (PrintWriter out = (logfile != null ? new PrintWriter(System.out) : new PrintWriter(logfile))) {
srgWriter.write(out); srgWriter.write(out);
} }
} }
}

View File

@ -169,18 +169,16 @@ public class JarRemapper extends CustomRemapper {
* Remap all the classes in a jar, writing a new jar to the target * Remap all the classes in a jar, writing a new jar to the target
*/ */
public void remapJar(Jar jar, File target) throws IOException { public void remapJar(Jar jar, File target) throws IOException {
JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
ClassRepo repo = new JarRepo(jar);
try {
if (jar == null) { if (jar == null) {
return; return;
} }
ClassRepo repo = new JarRepo(jar);
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(target))) {
for (String name : jar.getEntryNames()) { for (String name : jar.getEntryNames()) {
JarEntry entry; JarEntry entry;
InputStream is = jar.getResource(name); try (InputStream is = jar.getResource(name)) {
try {
byte[] data; byte[] data;
if (name.endsWith(".class")) { if (name.endsWith(".class")) {
// remap classes // remap classes
@ -211,12 +209,8 @@ public class JarRemapper extends CustomRemapper {
} }
out.putNextEntry(entry); out.putNextEntry(entry);
out.write(data); out.write(data);
} finally {
is.close();
} }
} }
} finally {
out.close();
} }
} }

View File

@ -278,9 +278,9 @@ public class SpecialSource {
InheritanceMap inheritanceMap = new InheritanceMap(); InheritanceMap inheritanceMap = new InheritanceMap();
inheritanceMap.generate(inheritanceProviders, jarMapping.classes.values()); inheritanceMap.generate(inheritanceProviders, jarMapping.classes.values());
PrintWriter printWriter = new PrintWriter((File) options.valueOf("write-inheritance")); try (PrintWriter printWriter = new PrintWriter((File) options.valueOf("write-inheritance"))) {
inheritanceMap.save(printWriter); inheritanceMap.save(printWriter);
printWriter.close(); }
} }
if (access != null) { if (access != null) {
@ -306,18 +306,10 @@ public class SpecialSource {
JarComparer visitor2 = visitors.second; JarComparer visitor2 = visitors.second;
ClassReader clazz1, clazz2; ClassReader clazz1, clazz2;
InputStream first = null; try (InputStream first = jars.second.getClass(classes.first);
InputStream second = null; InputStream second = jars.first.getClass(classes.second)) {
try { clazz1 = new ClassReader(first);
clazz1 = new ClassReader(first = jars.second.getClass(classes.first)); clazz2 = new ClassReader(second);
clazz2 = new ClassReader(second = jars.first.getClass(classes.second));
} finally {
if (first != null) {
first.close();
}
if (second != null) {
second.close();
}
} }
clazz1.accept(visitor1, 0); clazz1.accept(visitor1, 0);
clazz2.accept(visitor2, 0); clazz2.accept(visitor2, 0);

View File

@ -51,12 +51,12 @@ public class ClassLoaderProvider implements InheritanceProvider {
public Collection<String> getParents(String owner) { public Collection<String> getParents(String owner) {
// TODO: ToInternalName // TODO: ToInternalName
String ownerInternalName = owner.replace('.', '/').concat(".class"); String ownerInternalName = owner.replace('.', '/').concat(".class");
InputStream input = classLoader.getResourceAsStream(ownerInternalName);
try (InputStream input = classLoader.getResourceAsStream(ownerInternalName)) {
if (input == null) { if (input == null) {
return null; return null;
} }
try {
ClassReader cr = new ClassReader(input); ClassReader cr = new ClassReader(input);
ClassNode node = new ClassNode(); ClassNode node = new ClassNode();
cr.accept(node, 0); cr.accept(node, 0);
@ -72,14 +72,6 @@ public class ClassLoaderProvider implements InheritanceProvider {
return parents; return parents;
} catch (Exception ex) { } catch (Exception ex) {
// Just ignore this, means that we couldn't get any lookup for the specified class // Just ignore this, means that we couldn't get any lookup for the specified class
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ex) {
// Too bad, can't recover from here
}
}
} }
return null; return null;

View File

@ -73,11 +73,8 @@ public class MinecraftCodersPack extends MappingTransformer {
} }
private void readIntoMap(File file, Map<String, String> map) throws IOException { private void readIntoMap(File file, Map<String, String> map) throws IOException {
FileReader fileReader = null; try (FileReader fileReader = new FileReader(file);
CSVReader csvReader = null; CSVReader csvReader = new CSVReader(fileReader)) {
try {
fileReader = new FileReader(file);
csvReader = new CSVReader(fileReader);
String[] line; String[] line;
while ((line = csvReader.readNext()) != null) { while ((line = csvReader.readNext()) != null) {
@ -87,13 +84,6 @@ public class MinecraftCodersPack extends MappingTransformer {
Preconditions.checkArgument(line.length >= 2, "Invalid csv line: %s", (Object) line); Preconditions.checkArgument(line.length >= 2, "Invalid csv line: %s", (Object) line);
map.put(line[0], line[1]); map.put(line[0], line[1]);
} }
} finally {
if (csvReader != null) {
csvReader.close();
}
if (fileReader != null) {
fileReader.close();
}
} }
} }

View File

@ -64,20 +64,10 @@ public class FileLocator {
System.out.println("Downloading " + url); System.out.println("Downloading " + url);
} }
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
// TODO: Better solution for cleaning names - this extraneous '\' is introduced by path joining on the mcp dir // TODO: Better solution for cleaning names - this extraneous '\' is introduced by path joining on the mcp dir
rbc = Channels.newChannel(new URL(url.replace('\\', '/')).openStream()); try (ReadableByteChannel rbc = Channels.newChannel(new URL(url.replace('\\', '/')).openStream());
fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file)) {
fos.getChannel().transferFrom(rbc, 0, 1 << 24); fos.getChannel().transferFrom(rbc, 0, 1 << 24);
} finally {
if (rbc != null) {
rbc.close();
}
if (fos != null) {
fos.close();
}
} }
// Success! // Success!

View File

@ -57,18 +57,13 @@ public abstract class MappingWriter {
public final void write(PrintWriter out) { public final void write(PrintWriter out) {
// Sort lines for easy finding // Sort lines for easy finding
Collections.sort(lines); Collections.sort(lines);
// No try with resources for us!
try {
// Format header // Format header
out.println(MessageFormat.format(HEADER, oldJarName, newJarName, new Date())); out.println(MessageFormat.format(HEADER, oldJarName, newJarName, new Date()));
// Write out lines // Write out lines
for (String s : lines) { for (String s : lines) {
out.println(s); out.println(s);
} }
} finally { // Caller is in charge of closes the output stream
// Make sure we close the outputstream in all cases
out.close();
}
} }
protected final void addLine(String line) { protected final void addLine(String line) {