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
public void write(String output, ObfuscationType type, IMappingConsumer.MappingSet<MappingField> fields, IMappingConsumer.MappingSet<MappingMethod> methods) {
if (output != null) {
PrintWriter writer = null;
try {
String from = type.getKey().split(":")[0];
String to = type.getKey().split(":")[1];
writer = this.openFileWriter(output, type + " output TinyMappings");
try (PrintWriter writer = this.openFileWriter(output, type + " output TinyMappings")) {
writer.println(String.format("v1\t%s\t%s", from, to));
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()));
@ -62,13 +59,6 @@ public class MixinMappingWriterTiny extends MappingWriter {
}
} catch (IOException e) {
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 {
InputStream inputStream = getResourceAsStream(name.replace(".", "/") + ".class");
byte[] classBytes = ByteStreams.toByteArray(inputStream);
inputStream.close();
return classBytes;
try (InputStream inputStream = getResourceAsStream(name.replace(".", "/") + ".class")) {
return ByteStreams.toByteArray(inputStream);
}
}
@Override

View File

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

View File

@ -54,11 +54,10 @@ public class MinecraftJarProvider {
public void mergeJars(Project project) throws IOException {
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.merge();
jarMerger.close();
}
}
private void initFiles(Project project, MinecraftProvider minecraftProvider) {

View File

@ -63,9 +63,9 @@ public class MinecraftProvider extends DependencyProvider {
initFiles(project);
downloadMcJson(project);
FileReader reader = new FileReader(MINECRAFT_JSON);
try (FileReader reader = new FileReader(MINECRAFT_JSON)) {
versionInfo = gson.fromJson(reader, MinecraftVersionInfo.class);
reader.close();
}
// Add Loom as an annotation processor
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) -> {
if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) {
// JSON file in root directory
InputStreamReader inputStreamReader = new InputStreamReader(stream);
try {
try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) {
JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class);
if (json != null) {
@ -100,9 +99,6 @@ public final class MixinRefmapHelper {
}
} catch (Exception e) {
// ...
} finally {
inputStreamReader.close();
stream.close();
}
}
});
@ -116,17 +112,13 @@ public final class MixinRefmapHelper {
ZipUtil.iterate(output, (stream, entry) -> {
if (!entry.isDirectory() && entry.getName().endsWith(".json") && !entry.getName().contains("/") && !entry.getName().contains("\\")) {
// JSON file in root directory
InputStreamReader inputStreamReader = new InputStreamReader(stream);
try {
try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) {
JsonObject json = GSON.fromJson(inputStreamReader, JsonObject.class);
if (json != null && json.has("refmap")) {
mixinRefmapFilenames.add(json.get("refmap").getAsString());
}
} catch (Exception e) {
// ...
} finally {
inputStreamReader.close();
stream.close();
}
}
});

View File

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

View File

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