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 2f4238879681..69607a9150d7 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java @@ -142,6 +142,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(Integer.valueOf(j)); + j++; + } newTryStatementText.append(") {"); final int tryBlockStatementsLength = tryBlockChildren.length - 1; for (int i = 1; i < tryBlockStatementsLength; i++) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.after.java new file mode 100644 index 000000000000..9b6dcdf2ed0f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.after.java @@ -0,0 +1,9 @@ +import java.io.*; + +class Dependents { + void m() throws IOException { + String filename = "filename"; + try (InputStream in = new FileInputStream(filename)) { + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.java new file mode 100644 index 000000000000..bacd212ac072 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Dependents.java @@ -0,0 +1,13 @@ +import java.io.*; + +class Dependents { + void m() throws IOException { + InputStream in = null; + try { + String filename = "filename"; + in = new FileInputStream(filename); + }finally{ + in.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 index fcfcbdad0761..766077afdebd 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java @@ -23,6 +23,7 @@ public class TryFinallyCanBeTryWithResourcesFixTest extends IGQuickFixesTestCase public void testComments() { doTest(); } public void testMultiple() { doTest(); } + public void testDependents() { doTest(); } @Override public void setUp() throws Exception {