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