diff --git a/java/java-impl/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java b/java/java-impl/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java index bb6918265523..debbdcf58616 100644 --- a/java/java-impl/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java +++ b/java/java-impl/src/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntention.java @@ -23,6 +23,7 @@ import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.psi.codeStyle.VariableKind; +import com.intellij.psi.util.PsiTypesUtil; import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.psiutils.CommentTracker; @@ -215,8 +216,9 @@ public class ReplaceForEachLoopWithIndexedForLoopIntention extends MCIntention { final String variableName = createVariableName(variableNameRoot, iteratedValue); final Project project = context.getProject(); - final PsiType iteratedValueType = iteratedValue.getType(); + PsiType iteratedValueType = iteratedValue.getType(); assert iteratedValueType != null; + iteratedValueType = PsiTypesUtil.removeExternalAnnotations(iteratedValueType); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); final PsiDeclarationStatement declarationStatement = diff --git a/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno.java b/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno.java new file mode 100644 index 000000000000..323776d810d3 --- /dev/null +++ b/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno.java @@ -0,0 +1,7 @@ +class X { + void test(String str) { + for(String s : str.split("/")) { + System.out.println(s); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno_after.java b/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno_after.java new file mode 100644 index 000000000000..f8e8d12d017d --- /dev/null +++ b/java/java-tests/testData/ipp/com/siyeh/ipp/forloop/indexed/NoExternalAnno_after.java @@ -0,0 +1,9 @@ +class X { + void test(String str) { + String[] split = str.split("/"); + for (int i = 0, splitLength = split.length; i < splitLength; i++) { + String s = split[i]; + System.out.println(s); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntentionTest.java b/java/java-tests/testSrc/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntentionTest.java index a79a6b32d1a9..004f6e8ef247 100644 --- a/java/java-tests/testSrc/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntentionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ipp/forloop/ReplaceForEachLoopWithIndexedForLoopIntentionTest.java @@ -18,6 +18,7 @@ public class ReplaceForEachLoopWithIndexedForLoopIntentionTest extends IPPTestCa public void testBlockNeeded() { doTest(); } public void testBlockNeededLabeled() { doTest(); } public void testVar() { doTest(); } + public void testNoExternalAnno() { doTest(); } @Override protected String getIntentionName() {