Bug 565282 - loop with try-with-resources can use element variable
- change ConvertIterableLoopOperation to instead of opting out of
converting a loop that has an iterator.next() reference in a
try-with-resources statement to instead treat the reference as
an element variable reference
- change CleanUpTest1d7 test case appropriately
Change-Id: I795e85f834cc4c70e059d7cc1147ec5df91d852c
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java
index dc123a5..dad6400 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertIterableLoopOperation.java
@@ -520,7 +520,7 @@
if (fragment.getParent() instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varexp= (VariableDeclarationExpression)fragment.getParent();
if (varexp.getLocationInParent() == TryStatement.RESOURCES2_PROPERTY) {
- otherInvocationThenNext[0]= true;
+ fElementVariableReferenced= true;
return true;
}
}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d7.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d7.java
index 3d94b65..a6d694a 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d7.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d7.java
@@ -312,26 +312,42 @@
@Test
public void testJava50ForLoop563267() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=563267
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=565282
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String sample= "" //
+ "package test1;\n" //
+ + "import java.io.IOException;\n" //
+ + "import java.io.InputStream;\n" //
+ "import java.util.Iterator;\n" //
+ "import java.util.List;\n" //
+ "public class E1 {\n" //
- + " public void foo(List<String> list) {\n" //
- + " List<InputStream> toClose = new ArrayList<>();\n" //
+ + " public void foo(List<InputStream> toClose) throws IOException {\n" //
+ " for (Iterator<InputStream> it = toClose.iterator(); it.hasNext();) {\n" //
+ " try (InputStream r = it.next()) {\n" //
+ " }\n" //
+ " }\n" //
- + " toClose.clear();\n" //
+ " }\n" //
+ "}\n";
ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", sample, false, null);
enable(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED);
- assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ sample= "" //
+ + "package test1;\n" //
+ + "import java.io.IOException;\n" //
+ + "import java.io.InputStream;\n" //
+ + "import java.util.List;\n" //
+ + "public class E1 {\n" //
+ + " public void foo(List<InputStream> toClose) throws IOException {\n" //
+ + " for (InputStream inputStream : toClose) {\n" //
+ + " try (InputStream r = inputStream) {\n" //
+ + " }\n" //
+ + " }\n" //
+ + " }\n" //
+ + "}\n";
+ String expected1= sample;
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 });
}
}