diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java index 804a290d4d06..447837014a78 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java @@ -96,7 +96,7 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection { } final PsiElement[] tryBlockChildren = tryBlock.getChildren(); final Set 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 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) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java new file mode 100644 index 000000000000..213c58e71b39 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.java new file mode 100644 index 000000000000..5f225aa12dcd --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.java @@ -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 { + } finally { + // stop + // now + in.close(); + out.close(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java new file mode 100644 index 000000000000..b7d9a9cf0d00 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java @@ -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"); + } +}