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) {
// No luck, so lets try read it
try {
InputStream is = getClass(clazz);
try (InputStream is = getClass(clazz)) {
if (is != null) {
ClassReader cr;
ClassReader cr = new ClassReader(is);
// Process it
try {
cr = new ClassReader(is);
} finally {
is.close();
}
ClassNode node = new ClassNode();
cr.accept(node, 0);

View File

@ -486,13 +486,6 @@ public class JarMapping {
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());
@ -528,6 +521,8 @@ public class JarMapping {
}
}
srgWriter.write(out);
try (PrintWriter out = (logfile != null ? new PrintWriter(System.out) : new PrintWriter(logfile))) {
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
*/
public void remapJar(Jar jar, File target) throws IOException {
JarOutputStream out = new JarOutputStream(new FileOutputStream(target));
if (jar == null) {
return;
}
ClassRepo repo = new JarRepo(jar);
try {
if (jar == null) {
return;
}
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(target))) {
for (String name : jar.getEntryNames()) {
JarEntry entry;
InputStream is = jar.getResource(name);
try {
try (InputStream is = jar.getResource(name)) {
byte[] data;
if (name.endsWith(".class")) {
// remap classes
@ -211,12 +209,8 @@ public class JarRemapper extends CustomRemapper {
}
out.putNextEntry(entry);
out.write(data);
} finally {
is.close();
}
}
} finally {
out.close();
}
}

View File

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

View File

@ -51,12 +51,12 @@ public class ClassLoaderProvider implements InheritanceProvider {
public Collection<String> getParents(String owner) {
// TODO: ToInternalName
String ownerInternalName = owner.replace('.', '/').concat(".class");
InputStream input = classLoader.getResourceAsStream(ownerInternalName);
if (input == null) {
return null;
}
try {
try (InputStream input = classLoader.getResourceAsStream(ownerInternalName)) {
if (input == null) {
return null;
}
ClassReader cr = new ClassReader(input);
ClassNode node = new ClassNode();
cr.accept(node, 0);
@ -72,14 +72,6 @@ public class ClassLoaderProvider implements InheritanceProvider {
return parents;
} catch (Exception ex) {
// 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;

View File

@ -73,11 +73,8 @@ public class MinecraftCodersPack extends MappingTransformer {
}
private void readIntoMap(File file, Map<String, String> map) throws IOException {
FileReader fileReader = null;
CSVReader csvReader = null;
try {
fileReader = new FileReader(file);
csvReader = new CSVReader(fileReader);
try (FileReader fileReader = new FileReader(file);
CSVReader csvReader = new CSVReader(fileReader)) {
String[] line;
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);
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);
}
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
// 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());
fos = new FileOutputStream(file);
// TODO: Better solution for cleaning names - this extraneous '\' is introduced by path joining on the mcp dir
try (ReadableByteChannel rbc = Channels.newChannel(new URL(url.replace('\\', '/')).openStream());
FileOutputStream fos = new FileOutputStream(file)) {
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
} finally {
if (rbc != null) {
rbc.close();
}
if (fos != null) {
fos.close();
}
}
// Success!

View File

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