diff --git a/compiler/android-tests/src/org/jetbrains/kotlin/android/tests/download/SDKDownloader.java b/compiler/android-tests/src/org/jetbrains/kotlin/android/tests/download/SDKDownloader.java index 50b7b132146..e4c718c8b10 100644 --- a/compiler/android-tests/src/org/jetbrains/kotlin/android/tests/download/SDKDownloader.java +++ b/compiler/android-tests/src/org/jetbrains/kotlin/android/tests/download/SDKDownloader.java @@ -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(); } } diff --git a/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/AndroidRunner.java b/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/AndroidRunner.java index 1596ac6871e..b55a7e4b044 100644 --- a/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/AndroidRunner.java +++ b/compiler/android-tests/tests/org/jetbrains/kotlin/android/tests/AndroidRunner.java @@ -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(); } } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java index 549de9bbef0..54ce316f539 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/CompileEnvironmentUtil.java @@ -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 diff --git a/compiler/tests-common/org/jetbrains/kotlin/codegen/DxChecker.java b/compiler/tests-common/org/jetbrains/kotlin/codegen/DxChecker.java index c4f6b6a13d3..c15b54deedf 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/codegen/DxChecker.java +++ b/compiler/tests-common/org/jetbrains/kotlin/codegen/DxChecker.java @@ -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(); - } } } diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java b/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java index d86c80f96cd..82738765f8f 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/InTextDirectivesUtils.java @@ -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 result = new ArrayList(); 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; diff --git a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java index dfc059f0efd..cffc825b335 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java +++ b/compiler/tests-common/org/jetbrains/kotlin/test/KotlinTestUtils.java @@ -812,8 +812,8 @@ public class KotlinTestUtils { private static boolean compileJavaFiles(@NotNull Collection files, List options, @Nullable File javaErrorFile) throws IOException { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); - 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 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(); } } diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java index 34ab751ceec..89e9bf3c736 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java @@ -170,10 +170,8 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir { File outputFile = new File(jarPath.getParentFile(), FileUtil.getNameWithoutExtension(jarPath) + "-after.jar"); Set 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 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; } diff --git a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java index d08434b274d..f15fb534e7b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java +++ b/idea/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.java @@ -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) {