IDEA-142617 (Replace with 'try' with resources produces bad code)

This commit is contained in:
Bas Leijdekkers
2015-08-28 14:08:05 +02:00
parent 10a27143ed
commit 68ebd14210
4 changed files with 32 additions and 5 deletions
@@ -143,10 +143,12 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
separator = true;
}
int j = 1;
while (!unwantedChildren.contains(Integer.valueOf(j)) && j < tryBlockChildren.length - 1) {
tryStatement.getParent().addBefore(tryBlockChildren[j], tryStatement);
unwantedChildren.add(j);
j++;
if (!unwantedChildren.isEmpty()) {
while (!unwantedChildren.contains(Integer.valueOf(j)) && j < tryBlockChildren.length - 1) {
tryStatement.getParent().addBefore(tryBlockChildren[j], tryStatement);
unwantedChildren.add(j);
j++;
}
}
newTryStatementText.append(") {");
final int tryBlockStatementsLength = tryBlockChildren.length - 1;
@@ -0,0 +1,10 @@
import java.io.Reader;
class X {
public void test() throws Exception {
try (Reader r = new FileReader("")) {
r.read();
}
}
}
@@ -0,0 +1,14 @@
import java.io.Reader;
class X {
public void test() throws Exception {
Reader r = new FileReader("");
<caret>try {
r.read();
}
finally {
r.close();
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ public class TryFinallyCanBeTryWithResourcesFixTest extends IGQuickFixesTestCase
public void testComments() { doTest(); }
public void testMultiple() { doTest(); }
public void testDependents() { doTest(); }
public void testKeepResourceCallInBlock() { doTest(); }
@Override
public void setUp() throws Exception {