Use try-with-resources instead of manual try/finally

This commit is contained in:
Alexander Udalov
2017-04-01 00:06:30 +03:00
parent 08b50cab08
commit 463bbbd386
8 changed files with 29 additions and 60 deletions
@@ -195,21 +195,18 @@ public class SDKDownloader {
}
try {
byte[] buf = new byte[1024];
ZipInputStream zipinputstream;
ZipEntry zipentry;
zipinputstream = new ZipInputStream(new FileInputStream(pathToFile));
zipentry = zipinputstream.getNextEntry();
try {
while (zipentry != null) {
String entryName = zipentry.getName();
ZipEntry zipEntry = null;
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(pathToFile))) {
zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName();
int n;
File outputFile = new File(outputFolder + "/" + entryName);
if (zipentry.isDirectory()) {
if (zipEntry.isDirectory()) {
outputFile.mkdirs();
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
continue;
}
else {
@@ -220,23 +217,19 @@ public class SDKDownloader {
outputFile.createNewFile();
}
FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
try {
while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
fileoutputstream.write(buf, 0, n);
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
while ((n = zipInputStream.read(buf, 0, 1024)) > -1) {
fileOutputStream.write(buf, 0, n);
}
}
finally {
fileoutputstream.close();
}
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
zipinputstream.close();
}
catch (IOException e) {
System.err.println("Entry name: " + zipentry.getName());
if (zipEntry != null) {
System.err.println("Entry name: " + zipEntry.getName());
}
e.printStackTrace();
}
}
@@ -64,11 +64,8 @@ public class AndroidRunner extends TestSuite {
private static void writeAndroidSkdToLocalProperties() throws IOException {
System.out.println("Writing android sdk to local.properties: " + pathManager.getAndroidSdkRoot());
File file = new File(pathManager.getTmpFolder() + "/local.properties");
FileWriter fw = new FileWriter(file);
try {
try (FileWriter fw = new FileWriter(file)) {
fw.write("sdk.dir=" + pathManager.getAndroidSdkRoot());
} finally {
fw.close();
}
}
}
@@ -132,8 +132,7 @@ public class CompileEnvironmentUtil {
}
private static void copyJarImpl(JarOutputStream stream, File jarPath) throws IOException {
JarInputStream jis = new JarInputStream(new FileInputStream(jarPath));
try {
try (JarInputStream jis = new JarInputStream(new FileInputStream(jarPath))) {
while (true) {
JarEntry e = jis.getNextJarEntry();
if (e == null) {
@@ -145,9 +144,6 @@ public class CompileEnvironmentUtil {
}
}
}
finally {
jis.close();
}
}
@NotNull
@@ -77,15 +77,11 @@ public class DxChecker {
private static String generateExceptionMessage(Throwable e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
try {
try (PrintWriter printWriter = new PrintWriter(writer)) {
e.printStackTrace(printWriter);
String stackTrace = writer.toString();
Matcher matcher = STACK_TRACE_PATTERN.matcher(stackTrace);
return matcher.replaceAll("");
}
finally {
printWriter.close();
}
}
}
@@ -24,6 +24,7 @@ import com.intellij.util.ArrayUtil;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.junit.Assert;
import java.io.BufferedReader;
@@ -185,8 +186,7 @@ public final class InTextDirectivesUtils {
List<String> result = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new StringReader(fileText));
try {
try (BufferedReader reader = new BufferedReader(new StringReader(fileText))) {
String line;
while ((line = reader.readLine()) != null) {
@@ -198,11 +198,10 @@ public final class InTextDirectivesUtils {
}
}
}
} finally {
reader.close();
}
} catch(IOException e) {
throw new AssertionError(e);
}
catch (IOException e) {
throw ExceptionUtilsKt.rethrow(e);
}
return result;
@@ -812,8 +812,8 @@ public class KotlinTestUtils {
private static boolean compileJavaFiles(@NotNull Collection<File> files, List<String> options, @Nullable File javaErrorFile) throws IOException {
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8"));
try {
try (StandardJavaFileManager fileManager =
javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8"))) {
Iterable<? extends JavaFileObject> javaFileObjectsFromFiles = fileManager.getJavaFileObjectsFromFiles(files);
JavaCompiler.CompilationTask task = javaCompiler.getTask(
@@ -832,8 +832,6 @@ public class KotlinTestUtils {
assertEqualsToFile(javaErrorFile, errorsToString(diagnosticCollector, false));
}
return success;
} finally {
fileManager.close();
}
}
@@ -170,10 +170,8 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir {
File outputFile = new File(jarPath.getParentFile(), FileUtil.getNameWithoutExtension(jarPath) + "-after.jar");
Set<String> toDelete = SetsKt.setOf(entriesToDelete);
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
JarFile jar = new JarFile(jarPath);
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
try {
try (JarFile jar = new JarFile(jarPath);
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)))) {
for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements(); ) {
JarEntry jarEntry = enumeration.nextElement();
String name = jarEntry.getName();
@@ -189,10 +187,6 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir {
output.closeEntry();
}
}
finally {
output.close();
jar.close();
}
return outputFile;
}
@@ -391,14 +391,10 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
private static String printStackTraceToString(Throwable e) {
StringWriter out = new StringWriter(1024);
PrintWriter printWriter = new PrintWriter(out);
try {
try (PrintWriter printWriter = new PrintWriter(out)) {
e.printStackTrace(printWriter);
return out.toString().replace("\r", "");
}
finally {
printWriter.close();
}
}
private void setText(@NotNull final String resultText) {