diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClass.java deleted file mode 100644 index efa968405de3..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClass.java +++ /dev/null @@ -1,15 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - new Runnable() { - @Override - public void run() { - for (String s : Test.this) { - System.out.println(s); - } - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClassIterator.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClassIterator.java deleted file mode 100644 index efa968405de3..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForOuterClassIterator.java +++ /dev/null @@ -1,15 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - new Runnable() { - @Override - public void run() { - for (String s : Test.this) { - System.out.println(s); - } - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForThisClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForThisClass.java deleted file mode 100644 index 224dddf6edb1..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/afterForThisClass.java +++ /dev/null @@ -1,10 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - for (String s : this) { - System.out.println(s); - } - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClass.java deleted file mode 100644 index c0e66865c819..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClass.java +++ /dev/null @@ -1,15 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - new Runnable() { - @Override - public void run() { - for (int i = 0; i < size(); i++) { - System.out.println(get(i)); - } - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClassIterator.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClassIterator.java deleted file mode 100644 index 912fcadcc8d5..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForOuterClassIterator.java +++ /dev/null @@ -1,15 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - new Runnable() { - @Override - public void run() { - for (Iterator it = iterator(); it.hasNext(); ) { - System.out.println(it.next()); - } - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForThisClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForThisClass.java deleted file mode 100644 index f2d81bf12076..000000000000 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach/beforeForThisClass.java +++ /dev/null @@ -1,10 +0,0 @@ -// "Replace with 'foreach'" "true" -import java.util.*; - -public class Test extends ArrayList { - public void print() { - for (int i = 0; i < size(); i++) { - System.out.println(get(i)); - } - } -} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java index 86a522be6df8..387dd30fb0c3 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/migration/ForCanBeForeachInspection.java @@ -196,8 +196,16 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase { finalString = ""; statementToSkip = null; } - @NonNls final StringBuilder out = new StringBuilder( - "for(" + finalString + typeString + ' ' + contentVariableName + ": " + qualifier.getText() + ')'); + @NonNls final StringBuilder out = new StringBuilder("for("); + out.append(finalString).append(typeString).append(' ').append(contentVariableName).append(": "); + @NonNls final String listName; + if (listReference == null) { + listName = qualifier.getText(); + } + else { + listName = getVariableReferenceText(listReference, listVariable, forStatement); + } + out.append(listName).append(')'); if (body != null) { replaceCollectionGetAccess(body, contentVariableName, listVariable, indexName, statementToSkip, out); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.after.java new file mode 100644 index 000000000000..1d9d72adf000 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.after.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + new Runnable() { + @Override + public void run() { + for (String s : Test.this) { + System.out.println(s); + } + } + }; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.java new file mode 100644 index 000000000000..431c0a34cf47 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClass.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + new Runnable() { + @Override + public void run() { + for (int i = 0; i < size(); i++) { + System.out.println(get(i)); + } + } + }; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.after.java new file mode 100644 index 000000000000..1d9d72adf000 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.after.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + new Runnable() { + @Override + public void run() { + for (String s : Test.this) { + System.out.println(s); + } + } + }; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.java new file mode 100644 index 000000000000..fc3005d3aa5b --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForOuterClassIterator.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + new Runnable() { + @Override + public void run() { + for (Iterator it = iterator(); it.hasNext(); ) { + System.out.println(it.next()); + } + } + }; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.after.java new file mode 100644 index 000000000000..678e177ca588 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.after.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + for (String s : this) { + System.out.println(s); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.java new file mode 100644 index 000000000000..5a1072721b82 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/for_can_be_foreach/ForThisClass.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2017 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. + */ + +// "Replace with 'foreach'" "true" +import java.util.*; + +public class Test extends ArrayList { + public void print() { + for (int i = 0; i < size(); i++) { + System.out.println(get(i)); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java index 1faa3907a627..d475041c36e6 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/ForCanBeForeachFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -26,6 +26,9 @@ public class ForCanBeForeachFixTest extends IGQuickFixesTestCase { public void testQualifyWithThis1() { doTest(); } public void testQualifyWithThis2() { doTest(); } public void testNoQualifier() { doTest(); } + public void testForThisClass() { doTest(); } + public void testForOuterClass() { doTest(); } + public void testForOuterClassIterator() { doTest(); } @Override public void setUp() throws Exception { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/ForCanBeForeachInspectionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/ForCanBeForeachInspectionFixTest.java deleted file mode 100644 index 944cc280bcaf..000000000000 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/ForCanBeForeachInspectionFixTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2000-2017 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.migration; - -import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; -import com.intellij.codeInspection.LocalInspectionTool; -import org.jetbrains.annotations.NotNull; - -public class ForCanBeForeachInspectionFixTest extends LightQuickFixParameterizedTestCase { - @NotNull - @Override - protected LocalInspectionTool[] configureLocalInspectionTools() { - return new LocalInspectionTool[]{new ForCanBeForeachInspection()}; - } - - public void test() throws Exception { doAllTests(); } - - @Override - protected String getBasePath() { - return "/codeInsight/daemonCodeAnalyzer/quickFix/forCanBeForEach"; - } -} \ No newline at end of file