WhileCanBeForeachInspection#getContentType moved to ForCanBeForeachInspection to avoid circular dependency (IDEA-CR-17306); getVariableReferenceText: support implicit reference to outer class

This commit is contained in:
Tagir Valeev
2017-01-16 14:20:34 +07:00
parent cebc9d6db8
commit e5a9e1e3eb
5 changed files with 80 additions and 14 deletions
@@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
@@ -51,6 +52,12 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase {
return panel;
}
@Nullable
static PsiType getContentType(PsiType type, String containerClassName) {
PsiType parameterType = PsiUtil.substituteTypeParameter(type, containerClassName, 0, true);
return GenericsUtil.getVariableTypeByExpressionType(parameterType);
}
private class ForCanBeForeachFix extends InspectionGadgetsFix {
@Override
@@ -145,7 +152,7 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase {
if (type == null) {
return null;
}
parameterType = WhileCanBeForeachInspection.getContentType(type, CommonClassNames.JAVA_UTIL_COLLECTION);
parameterType = getContentType(type, CommonClassNames.JAVA_UTIL_COLLECTION);
if (parameterType == null) {
parameterType = TypeUtils.getObjectType(forStatement);
}
@@ -238,13 +245,13 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase {
if (iteratorType == null) {
return null;
}
final PsiType iteratorContentType = WhileCanBeForeachInspection.getContentType(iteratorType, CommonClassNames.JAVA_UTIL_ITERATOR);
final PsiType iteratorContentType = getContentType(iteratorType, CommonClassNames.JAVA_UTIL_ITERATOR);
final PsiType iteratorVariableType = iteratorVariable.getType();
final PsiType contentType;
final PsiClassType javaLangObject = TypeUtils.getObjectType(forStatement);
if (iteratorContentType == null) {
final PsiType iteratorVariableContentType =
WhileCanBeForeachInspection.getContentType(iteratorVariableType, CommonClassNames.JAVA_UTIL_ITERATOR);
getContentType(iteratorVariableType, CommonClassNames.JAVA_UTIL_ITERATOR);
if (iteratorVariableContentType == null) {
contentType = javaLangObject;
}
@@ -803,6 +810,6 @@ public class ForCanBeForeachInspection extends ForCanBeForeachInspectionBase {
final String text = reference.getText();
final PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(context.getProject());
final PsiVariable target = resolveHelper.resolveReferencedVariable(text, context);
return variable != target ? "this." + text : text;
return variable != target ? ExpressionUtils.getQualifierOrThis(reference).getText() + "." + text : text;
}
}
@@ -21,7 +21,6 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.Query;
import com.siyeh.InspectionGadgetsBundle;
@@ -30,7 +29,6 @@ import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class WhileCanBeForeachInspection extends WhileCanBeForeachInspectionBase {
@@ -79,12 +77,12 @@ public class WhileCanBeForeachInspection extends WhileCanBeForeachInspectionBase
if (collectionType == null) {
return;
}
final PsiType contentType = getContentType(collectionType, CommonClassNames.JAVA_LANG_ITERABLE);
final PsiType contentType = ForCanBeForeachInspection.getContentType(collectionType, CommonClassNames.JAVA_LANG_ITERABLE);
if (contentType == null) {
return;
}
final PsiType iteratorType = iterator.getType();
final PsiType iteratorContentType = getContentType(iteratorType, "java.util.Iterator");
final PsiType iteratorContentType = ForCanBeForeachInspection.getContentType(iteratorType, "java.util.Iterator");
if (iteratorContentType == null) {
return;
}
@@ -156,10 +154,4 @@ public class WhileCanBeForeachInspection extends WhileCanBeForeachInspectionBase
PsiReplacementUtil.replaceStatementAndShortenClassNames(whileStatement, result);
}
}
@Nullable
static PsiType getContentType(PsiType type, String containerClassName) {
PsiType parameterType = PsiUtil.substituteTypeParameter(type, containerClassName, 0, true);
return GenericsUtil.getVariableTypeByExpressionType(parameterType);
}
}
@@ -0,0 +1,33 @@
/*
* 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.
*/
import java.util.List;
public class Test {
List<String> values;
void test() {
new Runnable() {
@Override
public void run() {
int size = values.size();
int values = 5;
for (String value : Test.this.values) {
System.out.println(value);
}
}
};
}
}
@@ -0,0 +1,33 @@
/*
* 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.
*/
import java.util.List;
public class Test {
List<String> values;
void test() {
new Runnable() {
@Override
public void run() {
int size = values.size();
int values = 5;
<caret>for (int i = 0; i < size; i++) {
System.out.println(Test.this.values.get(i));
}
}
};
}
}
@@ -25,6 +25,7 @@ public class ForCanBeForeachFixTest extends IGQuickFixesTestCase {
public void testInstanceofAndWhitespace() { doTest(); }
public void testQualifyWithThis1() { doTest(); }
public void testQualifyWithThis2() { doTest(); }
public void testQualifyWithThisInner() { doTest(); }
public void testNoQualifier() { doTest(); }
public void testForThisClass() { doTest(); }
public void testForOuterClass() { doTest(); }