IDEA-167841 IncorrectOperationException on for -> foreach

This commit is contained in:
Tagir Valeev
2017-02-09 17:21:32 +07:00
parent acebed8e8f
commit b16b9f5931
4 changed files with 60 additions and 0 deletions
@@ -809,6 +809,11 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase {
private static String getVariableReferenceText(PsiReferenceExpression reference, PsiVariable variable, PsiElement context) {
final String text = reference.getText();
final PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(context.getProject());
PsiExpression qualifier = reference.getQualifierExpression();
while(qualifier != null) {
if(!(qualifier instanceof PsiReferenceExpression)) return text;
qualifier = ((PsiReferenceExpression)qualifier).getQualifierExpression();
}
final PsiVariable target = resolveHelper.resolveReferencedVariable(text, context);
return variable != target ? ExpressionUtils.getQualifierOrThis(reference).getText() + "." + text : text;
}
@@ -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 {
private String[] fields = null;
void test() {
for (String field : this.fields) {
}
}
@@ -0,0 +1,28 @@
/*
* 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 {
private String[] fields = null;
void test() {
fo<caret>r (int i = 0; i < this.fields.length; i++) {
String field = fields[i];
}
}
@@ -30,6 +30,7 @@ public class ForCanBeForeachFixTest extends IGQuickFixesTestCase {
public void testForThisClass() { doTest(); }
public void testForOuterClass() { doTest(); }
public void testForOuterClassIterator() { doTest(); }
public void testForQualifiedArray() { doTest(); }
@Override
public void setUp() throws Exception {