migrate to try-with-resources

dev/0.11
Adrian Siekierka 2019-04-22 00:39:09 +02:00
parent 253c2ed15e
commit 221fcf2f51
8 changed files with 40 additions and 58 deletions

View File

@ -46,13 +46,10 @@ public class MixinMappingWriterTiny extends MappingWriter {
@Override @Override
public void write(String output, ObfuscationType type, IMappingConsumer.MappingSet<MappingField> fields, IMappingConsumer.MappingSet<MappingMethod> methods) { public void write(String output, ObfuscationType type, IMappingConsumer.MappingSet<MappingField> fields, IMappingConsumer.MappingSet<MappingMethod> methods) {
if (output != null) { if (output != null) {
PrintWriter writer = null; String from = type.getKey().split(":")[0];
String to = type.getKey().split(":")[1];
try { try (PrintWriter writer = this.openFileWriter(output, type + " output TinyMappings")) {
String from = type.getKey().split(":")[0];
String to = type.getKey().split(":")[1];
writer = this.openFileWriter(output, type + " output TinyMappings");
writer.println(String.format("v1\t%s\t%s", from, to)); writer.println(String.format("v1\t%s\t%s", from, to));
for (IMappingConsumer.MappingSet.Pair<MappingField> pair : fields) { for (IMappingConsumer.MappingSet.Pair<MappingField> pair : fields) {
writer.println(String.format("FIELD\t%s\t%s\t%s\t%s", pair.from.getOwner(), pair.from.getDesc(), pair.from.getSimpleName(), pair.to.getSimpleName())); writer.println(String.format("FIELD\t%s\t%s\t%s\t%s", pair.from.getOwner(), pair.from.getDesc(), pair.from.getSimpleName(), pair.to.getSimpleName()));
@ -62,13 +59,6 @@ public class MixinMappingWriterTiny extends MappingWriter {
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
}
}
} }
} }
} }

View File

@ -149,10 +149,9 @@ public class MixinServiceGradle implements IClassBytecodeProvider, IClassProvide
} }
public byte[] getClassBytes(String name, String transformedName) throws IOException { public byte[] getClassBytes(String name, String transformedName) throws IOException {
InputStream inputStream = getResourceAsStream(name.replace(".", "/") + ".class"); try (InputStream inputStream = getResourceAsStream(name.replace(".", "/") + ".class")) {
byte[] classBytes = ByteStreams.toByteArray(inputStream); return ByteStreams.toByteArray(inputStream);
inputStream.close(); }
return classBytes;
} }
@Override @Override

View File

@ -66,9 +66,10 @@ public class MinecraftAssetsProvider {
ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, MinecraftAssetsProvider.class.getName()); ProgressLogger progressLogger = ProgressLogger.getProgressFactory(project, MinecraftAssetsProvider.class.getName());
progressLogger.start("Downloading assets...", "assets"); progressLogger.start("Downloading assets...", "assets");
FileReader fileReader = new FileReader(assetsInfo); AssetIndex index;
AssetIndex index = new Gson().fromJson(fileReader, AssetIndex.class); try (FileReader fileReader = new FileReader(assetsInfo)) {
fileReader.close(); index = new Gson().fromJson(fileReader, AssetIndex.class);
}
Map<String, AssetObject> parent = index.getFileMap(); Map<String, AssetObject> parent = index.getFileMap();
final int totalSize = parent.size(); final int totalSize = parent.size();
int position = 0; int position = 0;

View File

@ -54,11 +54,10 @@ public class MinecraftJarProvider {
public void mergeJars(Project project) throws IOException { public void mergeJars(Project project) throws IOException {
project.getLogger().lifecycle(":merging jars"); project.getLogger().lifecycle(":merging jars");
JarMerger jarMerger = new JarMerger(minecraftProvider.MINECRAFT_CLIENT_JAR, minecraftProvider.MINECRAFT_SERVER_JAR, minecraftProvider.MINECRAFT_MERGED_JAR); try (JarMerger jarMerger = new JarMerger(minecraftProvider.MINECRAFT_CLIENT_JAR, minecraftProvider.MINECRAFT_SERVER_JAR, minecraftProvider.MINECRAFT_MERGED_JAR)) {
jarMerger.enableSyntheticParamsOffset(); jarMerger.enableSyntheticParamsOffset();
jarMerger.merge();
jarMerger.merge(); }
jarMerger.close();
} }
private void initFiles(Project project, MinecraftProvider minecraftProvider) { private void initFiles(Project project, MinecraftProvider minecraftProvider) {

View File

@ -63,9 +63,9 @@ public class MinecraftProvider extends DependencyProvider {
initFiles(project); initFiles(project);
downloadMcJson(project); downloadMcJson(project);
FileReader reader = new FileReader(MINECRAFT_JSON); try (FileReader reader = new FileReader(MINECRAFT_JSON)) {
versionInfo = gson.fromJson(reader, MinecraftVersionInfo.class); versionInfo = gson.fromJson(reader, MinecraftVersionInfo.class);
reader.close(); }
// Add Loom as an annotation processor // Add Loom as an annotation processor
addDependency(project.files(this.getClass().getProtectionDomain().getCodeSource().getLocation()), project, "compileOnly"); addDependency(project.files(this.getClass().getProtectionDomain().getCodeSource().getLocation()), project, "compileOnly");

View File

@ -83,8 +83,7 @@ public final class MixinRefmapHelper {
ZipUtil.iterate(output, (stream, entry) -> { ZipUtil.iterate(output, (stream, entry) -> {
if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) { if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) {
// JSON file in root directory // JSON file in root directory
InputStreamReader inputStreamReader = new InputStreamReader(stream); try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) {
try {
JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class); JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class);
if (json != null) { if (json != null) {
@ -100,9 +99,6 @@ public final class MixinRefmapHelper {
} }
} catch (Exception e) { } catch (Exception e) {
// ... // ...
} finally {
inputStreamReader.close();
stream.close();
} }
} }
}); });
@ -116,17 +112,13 @@ public final class MixinRefmapHelper {
ZipUtil.iterate(output, (stream, entry) -> { ZipUtil.iterate(output, (stream, entry) -> {
if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) { if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) {
// JSON file in root directory // JSON file in root directory
InputStreamReader inputStreamReader = new InputStreamReader(stream); try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) {
try {
JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class); JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class);
if (json != null && json.has("refmap")) { if (json != null && json.has("refmap")) {
mixinRefmapFilenames.add(json.get("refmap").getAsString()); mixinRefmapFilenames.add(json.get("refmap").getAsString());
} }
} catch (Exception e) { } catch (Exception e) {
// ... // ...
} finally {
inputStreamReader.close();
stream.close();
} }
} }
}); });

View File

@ -190,33 +190,33 @@ public class ModProcessor {
static void readInstallerJson(File file, Project project){ static void readInstallerJson(File file, Project project){
try { try {
JarFile jarFile = new JarFile(file);
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class); LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
String launchMethod = extension.getLoaderLaunchMethod(); String launchMethod = extension.getLoaderLaunchMethod();
String jsonStr;
int priority = 0; int priority = 0;
ZipEntry entry = null; try (JarFile jarFile = new JarFile(file)) {
if (!launchMethod.isEmpty()) { ZipEntry entry = null;
entry = jarFile.getEntry("fabric-installer." + launchMethod + ".json"); if (!launchMethod.isEmpty()) {
if (entry == null) { entry = jarFile.getEntry("fabric-installer." + launchMethod + ".json");
project.getLogger().warn("Could not find loader launch method '" + launchMethod + "', falling back"); if (entry == null) {
project.getLogger().warn("Could not find loader launch method '" + launchMethod + "', falling back");
}
} }
}
if(entry == null){
entry = jarFile.getEntry("fabric-installer.json");
priority = 1;
if (entry == null) { if (entry == null) {
jarFile.close(); entry = jarFile.getEntry("fabric-installer.json");
return; priority = 1;
if (entry == null) {
return;
}
}
try (InputStream inputstream = jarFile.getInputStream(entry)) {
jsonStr = IOUtils.toString(inputstream, StandardCharsets.UTF_8);
} }
} }
InputStream inputstream = jarFile.getInputStream(entry);
String jsonStr = IOUtils.toString(inputstream, StandardCharsets.UTF_8);
inputstream.close();
jarFile.close();
JsonObject jsonObject = GSON.fromJson(jsonStr, JsonObject.class); JsonObject jsonObject = GSON.fromJson(jsonStr, JsonObject.class);
extension.setInstallerJson(jsonObject, priority); extension.setInstallerJson(jsonObject, priority);

View File

@ -168,9 +168,10 @@ public class RunConfig {
} }
public String fromDummy(String dummy) throws IOException { public String fromDummy(String dummy) throws IOException {
InputStream input = SetupIntelijRunConfigs.class.getClassLoader().getResourceAsStream(dummy); String dummyConfig;
String dummyConfig = IOUtils.toString(input, StandardCharsets.UTF_8); try (InputStream input = SetupIntelijRunConfigs.class.getClassLoader().getResourceAsStream(dummy)) {
input.close(); dummyConfig = IOUtils.toString(input, StandardCharsets.UTF_8);
}
dummyConfig = dummyConfig.replace("%NAME%", configName); dummyConfig = dummyConfig.replace("%NAME%", configName);
dummyConfig = dummyConfig.replace("%MAIN_CLASS%", mainClass); dummyConfig = dummyConfig.replace("%MAIN_CLASS%", mainClass);