IDEA-115584 (try-with-resources quickfix munges code)

This commit is contained in:
Bas Leijdekkers
2013-11-01 14:54:12 +01:00
parent a31a0d47b6
commit b7bade691f
4 changed files with 78 additions and 7 deletions
@@ -96,7 +96,7 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
}
final PsiElement[] tryBlockChildren = tryBlock.getChildren();
final Set<PsiLocalVariable> variables = new HashSet();
for (PsiLocalVariable variable : collectVariables(tryStatement)) {
for (final PsiLocalVariable variable : collectVariables(tryStatement)) {
if (!isVariableUsedOutsideContext(variable, tryBlock)) {
variables.add(variable);
}
@@ -105,7 +105,7 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
@NonNls final StringBuilder newTryStatementText = new StringBuilder("try (");
final Set<Integer> unwantedChildren = new HashSet(2);
boolean separator = false;
for (PsiLocalVariable variable : variables) {
for (final PsiLocalVariable variable : variables) {
final boolean hasInitializer;
final PsiExpression initializer = variable.getInitializer();
if (initializer == null) {
@@ -152,7 +152,7 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
}
newTryStatementText.append('}');
final PsiCatchSection[] catchSections = tryStatement.getCatchSections();
for (PsiCatchSection catchSection : catchSections) {
for (final PsiCatchSection catchSection : catchSections) {
newTryStatementText.append(catchSection.getText());
}
final PsiElement[] finallyChildren = finallyBlock.getChildren();
@@ -166,15 +166,20 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
}
if (!appended) {
if (child instanceof PsiComment) {
final PsiComment comment = (PsiComment)child;
final PsiElement prevSibling = child.getPrevSibling();
if (prevSibling instanceof PsiWhiteSpace) {
if (prevSibling instanceof PsiWhiteSpace && savedComments.isEmpty()) {
savedComments.add(prevSibling);
}
savedComments.add(child);
savedComments.add(comment);
final PsiElement nextSibling = child.getNextSibling();
if (nextSibling instanceof PsiWhiteSpace) {
savedComments.add(nextSibling);
}
}
else if (!(child instanceof PsiWhiteSpace)) {
newTryStatementText.append(" finally {");
for (PsiElement savedComment : savedComments) {
for (final PsiElement savedComment : savedComments) {
newTryStatementText.append(savedComment.getText());
}
newTryStatementText.append(child.getText());
@@ -188,7 +193,7 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection {
if (appended) {
newTryStatementText.append('}');
}
for (PsiLocalVariable variable : variables) {
for (final PsiLocalVariable variable : variables) {
variable.delete();
}
if (!appended) {
@@ -0,0 +1,15 @@
package com.siyeh.igfixes.migration.try_finally_can_be_try_with_resources;
import java.io.*;
class Comments {
void m(OutputStream out) throws IOException {
try (InputStream in = new FileInputStream("filename")) {
} finally {
// stop
// now
out.close();
}
}
}
@@ -0,0 +1,17 @@
package com.siyeh.igfixes.migration.try_finally_can_be_try_with_resources;
import java.io.*;
class Comments {
void m(OutputStream out) throws IOException {
InputStream in = new FileInputStream("filename");
try<caret> {
} finally {
// stop
// now
in.close();
out.close();
}
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.fixes.migration;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.migration.IfCanBeSwitchInspection;
import com.siyeh.ig.migration.TryFinallyCanBeTryWithResourcesInspection;
public class TryFinallyCanBeTryWithResourcesFixTest extends IGQuickFixesTestCase {
public void testComments() { doTest(); }
@Override
public void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new TryFinallyCanBeTryWithResourcesInspection());
myRelativePath = "migration/try_finally_can_be_try_with_resources";
myDefaultHint = InspectionGadgetsBundle.message("try.finally.can.be.try.with.resources.quickfix");
}
}