From d50a5c7a0715317112bfea0af372b9d2504fcb25 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 1 Jul 2016 16:06:13 +0300 Subject: [PATCH] Java intention: Quick fix for error "foreach not applicable to type java.util.Iterator" (IDEA-124751) --- .../intention/QuickFixFactory.java | 2 + .../impl/analysis/GenericsHighlightUtil.java | 4 +- .../intention/EmptyQuickFixFactory.java | 5 + ...atorForEachLoopWithIteratorForLoopFix.java | 126 ++++++++++++++++++ .../impl/config/QuickFixFactoryImpl.java | 14 +- .../afterCodeBlockBody.java | 12 ++ .../afterEmptyBody.java | 10 ++ .../afterIncompatibleItemType.java | 11 ++ .../afterMissingBody.java | 10 ++ .../afterOneStatementBody.java | 11 ++ .../afterPrimitiveItem.java | 11 ++ .../beforeCodeBlockBody.java | 11 ++ .../beforeEmptyBody.java | 8 ++ .../beforeIncompatibleItemType.java | 8 ++ .../beforeMissingBody.java | 8 ++ .../beforeNotIterator.java | 8 ++ .../beforeOneStatementBody.java | 8 ++ .../beforePrimitiveItem.java | 8 ++ ...ForEachLoopWithIteratorForLoopFixTest.java | 29 ++++ 19 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceIteratorForEachLoopWithIteratorForLoopFix.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterCodeBlockBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterEmptyBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterIncompatibleItemType.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterMissingBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterOneStatementBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterPrimitiveItem.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeCodeBlockBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeEmptyBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeIncompatibleItemType.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeMissingBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeNotIterator.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeOneStatementBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforePrimitiveItem.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReplaceIteratorForEachLoopWithIteratorForLoopFixTest.java diff --git a/java/java-analysis-api/src/com/intellij/codeInsight/intention/QuickFixFactory.java b/java/java-analysis-api/src/com/intellij/codeInsight/intention/QuickFixFactory.java index 0375803c77f0..997eb0042b53 100644 --- a/java/java-analysis-api/src/com/intellij/codeInsight/intention/QuickFixFactory.java +++ b/java/java-analysis-api/src/com/intellij/codeInsight/intention/QuickFixFactory.java @@ -274,4 +274,6 @@ public abstract class QuickFixFactory { public IntentionAction createWrapWithOptionalFix(@Nullable PsiType type, @NotNull PsiExpression expression) { throw new UnsupportedOperationException(); } + + public abstract IntentionAction createNotIterableForEachLoopFix(PsiExpression expression); } \ No newline at end of file diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index 6230168baf79..d1a7cea73e24 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -753,7 +753,9 @@ public class GenericsHighlightUtil { if (itemType == null) { String description = JavaErrorMessages.message("foreach.not.applicable", JavaHighlightUtil.formatType(expression.getType())); - return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create(); + final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create(); + QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createNotIterableForEachLoopFix(expression)); + return highlightInfo; } return null; } diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/intention/EmptyQuickFixFactory.java b/java/java-analysis-impl/src/com/intellij/codeInsight/intention/EmptyQuickFixFactory.java index f97e4feda520..84be882e9e06 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/intention/EmptyQuickFixFactory.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/intention/EmptyQuickFixFactory.java @@ -626,4 +626,9 @@ public class EmptyQuickFixFactory extends QuickFixFactory { public IntentionAction createWrapWithOptionalFix(@Nullable PsiType type, @NotNull PsiExpression expression) { return QuickFixes.EMPTY_FIX; } + + @Override + public IntentionAction createNotIterableForEachLoopFix(PsiExpression expression) { + return QuickFixes.EMPTY_FIX; + } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceIteratorForEachLoopWithIteratorForLoopFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceIteratorForEachLoopWithIteratorForLoopFix.java new file mode 100644 index 000000000000..2d71379b96a0 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ReplaceIteratorForEachLoopWithIteratorForLoopFix.java @@ -0,0 +1,126 @@ +/* + * Copyright 2000-2016 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.intellij.codeInsight.daemon.impl.quickfix; + +import com.intellij.codeInsight.FileModificationService; +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel.Dolgov + */ +public class ReplaceIteratorForEachLoopWithIteratorForLoopFix implements IntentionAction { + private final PsiForeachStatement myStatement; + + public ReplaceIteratorForEachLoopWithIteratorForLoopFix(@NotNull PsiForeachStatement statement) { + myStatement = statement; + } + + @Nls + @NotNull + @Override + public String getText() { + return "Replace 'for each' loop with iterator 'for' loop"; + } + + @Nls + @NotNull + @Override + public String getFamilyName() { + return getText(); + } + + @Override + public boolean startInWriteAction() { + return true; + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return myStatement.isValid() && myStatement.getManager().isInProject(myStatement); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; + final PsiExpression iteratedValue = myStatement.getIteratedValue(); + if (iteratedValue == null) { + return; + } + final PsiType iteratedValueType = iteratedValue.getType(); + if (iteratedValueType == null) { + return; + } + final PsiParameter iterationParameter = myStatement.getIterationParameter(); + final String iterationParameterName = iterationParameter.getName(); + if (iterationParameterName == null) { + return; + } + final PsiStatement forEachBody = myStatement.getBody(); + + final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); + final JavaCodeStyleManager javaStyleManager = JavaCodeStyleManager.getInstance(project); + final String name = javaStyleManager.suggestUniqueVariableName("it", myStatement, true); + PsiForStatement newForLoop = (PsiForStatement)elementFactory.createStatementFromText( + "for (Iterator " + name + " = initializer; " + name + ".hasNext();) { Object next = " + name + ".next(); }", myStatement); + + final PsiDeclarationStatement newDeclaration = (PsiDeclarationStatement)newForLoop.getInitialization(); + if (newDeclaration == null) return; + final PsiLocalVariable newIteratorVariable = (PsiLocalVariable)newDeclaration.getDeclaredElements()[0]; + final PsiTypeElement newIteratorTypeElement = elementFactory.createTypeElement(iteratedValueType); + newIteratorVariable.getTypeElement().replace(newIteratorTypeElement); + newIteratorVariable.setInitializer(iteratedValue); + + final PsiBlockStatement newBody = (PsiBlockStatement)newForLoop.getBody(); + if (newBody == null) return; + final PsiCodeBlock newBodyBlock = newBody.getCodeBlock(); + + final PsiDeclarationStatement newFirstStatement = (PsiDeclarationStatement)newBodyBlock.getStatements()[0]; + final PsiLocalVariable newItemVariable = (PsiLocalVariable)newFirstStatement.getDeclaredElements()[0]; + final PsiTypeElement newItemTypeElement = elementFactory.createTypeElement(iterationParameter.getType()); + newItemVariable.getTypeElement().replace(newItemTypeElement); + newItemVariable.setName(iterationParameterName); + final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project); + if (codeStyleSettings.GENERATE_FINAL_LOCALS) { + final PsiModifierList modifierList = newItemVariable.getModifierList(); + if (modifierList != null) modifierList.setModifierProperty(PsiModifier.FINAL, true); + } + final CodeStyleManager styleManager = CodeStyleManager.getInstance(project); + newForLoop = (PsiForStatement)javaStyleManager.shortenClassReferences(newForLoop); + newForLoop = (PsiForStatement)styleManager.reformat(newForLoop); + + if (forEachBody instanceof PsiBlockStatement) { + final PsiStatement[] statements = ((PsiBlockStatement)forEachBody).getCodeBlock().getStatements(); + for (int i = statements.length - 1; i >= 0; i--) { + newBodyBlock.addAfter(statements[i], newFirstStatement); + } + } + else if (forEachBody != null && !(forEachBody instanceof PsiEmptyStatement)) { + newBodyBlock.addAfter(forEachBody, newFirstStatement); + } + + myStatement.replace(newForLoop); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java index 295098fcefa7..84f7861741b2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java @@ -52,11 +52,11 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.ClassKind; +import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PropertyMemberType; import com.intellij.refactoring.changeSignature.ChangeSignatureGestureDetector; import com.intellij.util.DocumentUtil; import com.intellij.util.IncorrectOperationException; -import com.intellij.util.Processor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -778,6 +778,18 @@ public class QuickFixFactoryImpl extends QuickFixFactory { return WrapObjectWithOptionalOfNullableFix.createFix(type, expression); } + @Override + public IntentionAction createNotIterableForEachLoopFix(PsiExpression expression) { + final PsiElement parent = expression.getParent(); + if (parent instanceof PsiForeachStatement) { + final PsiType type = expression.getType(); + if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_ITERATOR)) { + return new ReplaceIteratorForEachLoopWithIteratorForLoopFix((PsiForeachStatement)parent); + } + } + return null; + } + private static boolean timeToOptimizeImports(@NotNull PsiFile file) { if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterCodeBlockBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterCodeBlockBody.java new file mode 100644 index 000000000000..bc57adb5ee1d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterCodeBlockBody.java @@ -0,0 +1,12 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class CodeBlockBody { + void foo(Iterator it,Iterator it1) { + for (Iterator it2 = it1; it2.hasNext(); ) { + Integer integer = it2.next(); + System.out.println(integer + " a"); + System.out.println(integer + " b"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterEmptyBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterEmptyBody.java new file mode 100644 index 000000000000..2e2bc167b9fb --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterEmptyBody.java @@ -0,0 +1,10 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class EmptyBody { + void foo(Iterator it) { + for (Iterator it1 = it; it1.hasNext(); ) { + Integer integer = it1.next(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterIncompatibleItemType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterIncompatibleItemType.java new file mode 100644 index 000000000000..920e7747e682 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterIncompatibleItemType.java @@ -0,0 +1,11 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class IncompatibleItemType { + void foo(Iterator it) { + for (Iterator it1 = it; it1.hasNext(); ) { + String string = it1.next(); + System.out.println(string); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterMissingBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterMissingBody.java new file mode 100644 index 000000000000..801c3d5a3881 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterMissingBody.java @@ -0,0 +1,10 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class MissingBody { + void foo(Iterator it1) { + for (Iterator it = it1; it.hasNext(); ) { + Integer integer = it.next(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterOneStatementBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterOneStatementBody.java new file mode 100644 index 000000000000..3c6fb8af30f6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterOneStatementBody.java @@ -0,0 +1,11 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class OneStatementBody { + void foo(Iterator it) { + for (Iterator it1 = it; it1.hasNext(); ) { + Integer integer = it1.next(); + System.out.println(integer); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterPrimitiveItem.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterPrimitiveItem.java new file mode 100644 index 000000000000..704b33b4baae --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/afterPrimitiveItem.java @@ -0,0 +1,11 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class PrimitiveItem { + void foo(Iterator it) { + for (Iterator it1 = it; it1.hasNext(); ) { + int i = it1.next(); + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeCodeBlockBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeCodeBlockBody.java new file mode 100644 index 000000000000..4e98a66b426d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeCodeBlockBody.java @@ -0,0 +1,11 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class CodeBlockBody { + void foo(Iterator it,Iterator it1) { + for (Integer integer : it1) { + System.out.println(integer + " a"); + System.out.println(integer + " b"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeEmptyBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeEmptyBody.java new file mode 100644 index 000000000000..5206b73df0a9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeEmptyBody.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class EmptyBody { + void foo(Iterator it) { + for (Integer integer : it) ; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeIncompatibleItemType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeIncompatibleItemType.java new file mode 100644 index 000000000000..0566bec5b631 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeIncompatibleItemType.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class IncompatibleItemType { + void foo(Iterator it) { + for (String string : it) System.out.println(string); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeMissingBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeMissingBody.java new file mode 100644 index 000000000000..b894eecb9637 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeMissingBody.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class MissingBody { + void foo(Iterator it1) { + for (Integer integer : it1) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeNotIterator.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeNotIterator.java new file mode 100644 index 000000000000..7d4785c2a441 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeNotIterator.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "false" +import java.lang.ref.Reference; + +public class NotIterator { + void foo(Reference it) { + for (String string : it) System.out.println(string); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeOneStatementBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeOneStatementBody.java new file mode 100644 index 000000000000..ab8f3ac72e4d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforeOneStatementBody.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class OneStatementBody { + void foo(Iterator it) { + for (Integer integer : it) System.out.println(integer); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforePrimitiveItem.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforePrimitiveItem.java new file mode 100644 index 000000000000..c39756a22224 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor/beforePrimitiveItem.java @@ -0,0 +1,8 @@ +// "Replace 'for each' loop with iterator 'for' loop" "true" +import java.util.Iterator; + +public class PrimitiveItem { + void foo(Iterator it) { + for (int i : it) System.out.println(i); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReplaceIteratorForEachLoopWithIteratorForLoopFixTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReplaceIteratorForEachLoopWithIteratorForLoopFixTest.java new file mode 100644 index 000000000000..751f5dd7e95f --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ReplaceIteratorForEachLoopWithIteratorForLoopFixTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2016 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.intellij.codeInsight.daemon.quickFix; + +/** + * @author Pavel.Dolgov + */ +public class ReplaceIteratorForEachLoopWithIteratorForLoopFixTest extends LightQuickFixParameterizedTestCase { + + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceIteratorForEachWithFor"; + } +}