diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/WhileToForEach.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/WhileToForEach.java index 662f0f93e44..3e0a1ff1ab2 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/WhileToForEach.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/helper/WhileToForEach.java @@ -260,6 +260,7 @@ private static boolean validate(final WhileLoopToChangeHit hit) { }); return !hit.isInvalid; } + private static String computeNextVarname(WhileStatement whilestatement) { String name= null; Expression exp= whilestatement.getExpression(); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java index 605b63d4a4e..30d63702d85 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d8.java @@ -5817,6 +5817,85 @@ void m(List strings) { new HashSet<>(Arrays.asList(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description))); } + @Test + public void testWhileIssue120_WithOptionVarUsed() throws Exception { + IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); + String sample= "" // + + "package test;\n" + + "import java.util.*;\n" + + "public class Test {\n" + + " private static List method(Map> map) {\n" + + " List results = new ArrayList<>();\n" + + " Iterator> iterator = map.values().iterator();\n" + + " while (iterator.hasNext()) {\n" + + " results.addAll(iterator.next());\n" + + " }\n" + + " return results;\n" + + " }\n" + + "}\n"; + ICompilationUnit cu1= pack.createCompilationUnit("Test.java", sample, false, null); + + enable(CleanUpConstants.CONTROL_STATEMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, + CleanUpConstants.CONTROL_STATEMENTS_CONVERT_FOR_LOOP_ONLY_IF_LOOP_VAR_USED); + + sample= "" // + + "package test;\n" + + "import java.util.*;\n" + + "public class Test {\n" + + " private static List method(Map> map) {\n" + + " List results = new ArrayList<>();\n" + + " Iterator> iterator = map.values().iterator();\n" + + " while (iterator.hasNext()) {\n" + + " results.addAll(iterator.next());\n" + + " }\n" + + " return results;\n" + + " }\n" + + "}\n"; + String expected1= sample; + + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, + new HashSet<>(Arrays.asList(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description))); + } + + @Test + public void testWhileIssue120_WithoutOptionVarUsed() throws Exception { + IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); + String sample= "" // + + "package test;\n" + + "import java.util.*;\n" + + "public class Test {\n" + + " private static List method(Map> map) {\n" + + " List results = new ArrayList<>();\n" + + " Iterator> iterator = map.values().iterator();\n" + + " while (iterator.hasNext()) {\n" + + " results.addAll(iterator.next());\n" + + " }\n" + + " return results;\n" + + " }\n" + + "}\n"; + ICompilationUnit cu1= pack.createCompilationUnit("Test.java", sample, false, null); + + enable(CleanUpConstants.CONTROL_STATEMENTS_CONVERT_FOR_LOOP_TO_ENHANCED); + + sample= "" // + + "package test;\n" + + "import java.util.*;\n" + + "public class Test {\n" + + " private static List method(Map> map) {\n" + + " List results = new ArrayList<>();\n" + + " Iterator> iterator = map.values().iterator();\n" + + " while (iterator.hasNext()) {\n" + + " results.addAll(iterator.next());\n" + + " }\n" + + " return results;\n" + + " }\n" + + "}\n"; + String expected1= sample; + + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, + new HashSet<>(Arrays.asList(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description))); + } + @Test public void testWhileSubtype() throws Exception { IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTestCase.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTestCase.java index 96de0ca5c98..3f76a22588e 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTestCase.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTestCase.java @@ -153,6 +153,13 @@ protected void enable(String key) throws CoreException { commitProfile(); } + protected void enable(String... keys) throws CoreException { + for(String key:keys) { + fProfile.getSettings().put(key, CleanUpOptions.TRUE); + } + commitProfile(); + } + private void commitProfile() throws CoreException { List profiles= CleanUpPreferenceUtil.getBuiltInProfiles(); profiles.add(fProfile);