mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-167841 IncorrectOperationException on for -> foreach
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
+26
@@ -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) {
|
||||
}
|
||||
}
|
||||
+28
@@ -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];
|
||||
|
||||
}
|
||||
}
|
||||
+1
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user