Highlight misplaced reference parameter lists

This commit is contained in:
Roman Shevchenko
2012-09-11 19:31:55 +04:00
parent 2092fb60b4
commit 4cbe87de02
6 changed files with 102 additions and 57 deletions
@@ -51,11 +51,12 @@ import java.util.*;
*/
public class GenericsHighlightUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil");
private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
private GenericsHighlightUtil() {
}
private GenericsHighlightUtil() { }
@Nullable
public static HighlightInfo checkInferredTypeArguments(PsiMethod genericMethod,
PsiMethodCallExpression call,
PsiSubstitutor substitutor) {
@@ -67,7 +68,8 @@ public class GenericsHighlightUtil {
PsiClassType[] extendsTypes = typeParameter.getExtendsListTypes();
for (PsiClassType type : extendsTypes) {
PsiType extendsType = substitutor.substitute(type);
if (substituted instanceof PsiWildcardType && TypeConversionUtil.erasure(extendsType).equals(TypeConversionUtil.erasure(((PsiWildcardType)substituted).getExtendsBound()))) {
if (substituted instanceof PsiWildcardType &&
TypeConversionUtil.erasure(extendsType).equals(TypeConversionUtil.erasure(((PsiWildcardType)substituted).getExtendsBound()))) {
PsiType extendsBound = ((PsiWildcardType)substituted).getExtendsBound();
if (extendsBound instanceof PsiClassType) {
PsiType[] parameters = ((PsiClassType)extendsBound).getParameters();
@@ -103,7 +105,7 @@ public class GenericsHighlightUtil {
}
@Nullable
public static HighlightInfo checkParameterizedReferenceTypeArguments(PsiElement resolved,
public static HighlightInfo checkParameterizedReferenceTypeArguments(final PsiElement resolved,
final PsiJavaCodeReferenceElement referenceElement,
final PsiSubstitutor substitutor) {
if (!(resolved instanceof PsiTypeParameterListOwner)) return null;
@@ -116,18 +118,14 @@ public class GenericsHighlightUtil {
final PsiReferenceParameterList referenceParameterList,
final PsiSubstitutor substitutor,
boolean registerIntentions) {
if (referenceParameterList != null) {
HighlightInfo info = HighlightUtil.checkGenericsFeature(referenceParameterList, referenceParameterList.getTypeParameterElements().length);
if (info != null) return info;
}
PsiDiamondType.DiamondInferenceResult inferenceResult = null;
PsiTypeElement[] referenceElements = null;
if (referenceParameterList != null) {
referenceElements = referenceParameterList.getTypeParameterElements();
if (referenceElements.length == 1 && referenceElements[0].getType() instanceof PsiDiamondType) {
if (!typeParameterListOwner.hasTypeParameters()) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceElements[0], "Diamond operator is not applicable for non-parameterized types");
final String description = JavaErrorMessages.message("generics.diamond.not.applicable");
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceElements[0], description);
}
inferenceResult = ((PsiDiamondType)referenceElements[0].getType()).resolveInferredTypes();
final String errorMessage = inferenceResult.getErrorMessage();
@@ -165,9 +163,9 @@ public class GenericsHighlightUtil {
if (description != null) {
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceParameterList, description);
if (registerIntentions) {
PsiElement pparent = referenceParameterList.getParent().getParent();
if (pparent instanceof PsiTypeElement) {
PsiElement variable = pparent.getParent();
PsiElement grandParent = referenceParameterList.getParent().getParent();
if (grandParent instanceof PsiTypeElement) {
PsiElement variable = grandParent.getParent();
if (variable instanceof PsiVariable) {
if (targetParametersNum == 0) {
QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveTypeArgumentsFix(variable));
@@ -1029,7 +1027,7 @@ public class GenericsHighlightUtil {
MethodSignatureBackedByPsiMethod superMethod = SuperMethodsSearch.search(method, null, true, false).findFirst();
if (superMethod == null) {
HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, overrideAnnotation,
JavaErrorMessages.message("method.doesnot.override.super"));
JavaErrorMessages.message("method.does.not.override.super"));
PullAsAbstractUpFix.registerQuickFix(highlightInfo, method);
return highlightInfo;
}
@@ -1245,6 +1243,26 @@ public class GenericsHighlightUtil {
return list;
}
@Nullable
public static HighlightInfo checkParametersAllowed(PsiReferenceParameterList refParamList) {
HighlightInfo info = HighlightUtil.checkGenericsFeature(refParamList, refParamList.getTypeParameterElements().length);
if (info != null) return info;
if (refParamList.getTextLength() != 0) {
final PsiElement parent = refParamList.getParent();
if (parent instanceof PsiReferenceExpression) {
final PsiElement grandParent = parent.getParent();
if (!(grandParent instanceof PsiMethodCallExpression)) {
final String message = JavaErrorMessages.message("generics.reference.parameters.not.allowed");
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, refParamList, message);
}
}
}
return null;
}
@Nullable
public static HighlightInfo checkParametersOnRaw(PsiReferenceParameterList refParamList) {
if (refParamList.getTypeArguments().length == 0) return null;
JavaResolveResult resolveResult = null;
@@ -982,16 +982,17 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
}
@Override public void visitReferenceParameterList(PsiReferenceParameterList list) {
myHolder.add(GenericsHighlightUtil.checkParametersOnRaw(list));
@Override
public void visitReferenceParameterList(PsiReferenceParameterList list) {
myHolder.add(GenericsHighlightUtil.checkParametersAllowed(list));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkParametersOnRaw(list));
}
@Override public void visitReturnStatement(PsiReturnStatement statement) {
try {
myHolder.add(HighlightUtil.checkReturnStatementType(statement));
}
catch (IndexNotReadyException ignore) {
}
catch (IndexNotReadyException ignore) { }
}
@Override public void visitStatement(PsiStatement statement) {
@@ -17,21 +17,26 @@ package com.intellij.openapi.projectRoots;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/**
* User: anna
* Date: 3/28/12
* @author anna
* @since 3/28/12
*/
public class JavaVersionServiceImpl extends JavaVersionService {
private JavaSdkVersion myTestVersion = null;
public void setTestVersion(JavaSdkVersion testVersion) {
@TestOnly
public void setTestVersion(@Nullable JavaSdkVersion testVersion) {
myTestVersion = testVersion;
}
@Override
public boolean isAtLeast(PsiElement element, JavaSdkVersion version) {
if (ApplicationManager.getApplication().isUnitTestMode()) return myTestVersion != null && myTestVersion.isAtLeast(version);
if (ApplicationManager.getApplication().isUnitTestMode()) {
return myTestVersion != null && myTestVersion.isAtLeast(version);
}
return JavaSdkVersionUtil.isAtLeast(element, version);
}
}
@@ -62,6 +62,8 @@ generics.type.argument.cannot.be.of.primitive.type=Type argument cannot be of pr
generics.unchecked.assignment=Unchecked assignment: ''{0}'' to ''{1}''
generics.unchecked.cast=Unchecked cast: ''{0}'' to ''{1}''
generics.unchecked.call.to.member.of.raw.type=Unchecked call to ''{0}'' as a member of raw type ''{1}''
generics.diamond.not.applicable=Diamond operator is not applicable for non-parameterized types
generics.reference.parameters.not.allowed=Reference parameters are not allowed here
foreach.not.applicable=foreach not applicable to type ''{0}''.
illegal.to.access.static.member.from.enum.constructor.or.instance.initializer=It is illegal to access static member ''{0}'' from enum constructor or instance initializer
enum.types.cannot.be.instantiated=Enum types cannot be instantiated
@@ -74,7 +76,7 @@ generics.cannot.catch.type.parameters=Cannot catch type parameters
generics.cannot.instanceof.type.parameters=Class or array expected
illegal.generic.type.for.instanceof=Illegal generic type for instanceof
cannot.select.dot.class.from.type.variable=Cannot select from a type variable
method.doesnot.override.super=Method does not override method from its superclass
method.does.not.override.super=Method does not override method from its superclass
call.to.super.is.not.allowed.in.enum.constructor=Call to super is not allowed in enum constructor
vararg.not.last.parameter=Vararg parameter must be the last in the list
modifiers.for.enum.constants=No modifiers allowed for enum constants
@@ -0,0 +1,10 @@
import java.util.*;
class C {
static final List EMPTY = new ArrayList(0);
void m() {
List<String> list = C.<error descr="Reference parameters are not allowed here"><String></error>EMPTY;
System.out.println(list);
}
}
@@ -1,3 +1,18 @@
/*
* Copyright 2000-2012 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;
import com.intellij.codeInspection.LocalInspectionTool;
@@ -62,33 +77,30 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testExceptions() throws Exception { doTest(false); }
public void testExplicitMethodParameters() throws Exception { doTest(false); }
public void testExplicitMethodParameters1() throws Exception { doTest(false); }
public void testInferenceWithBounds() throws Exception {doTest(false);}
public void testInferenceWithSuperBounds() throws Exception {doTest(false);}
public void testInferenceWithUpperBoundPromotion() throws Exception {doTest(false);}
public void testVariance() throws Exception {doTest(false);}
public void testForeachTypes() throws Exception {doTest(false);}
public void testRawOverridingMethods() throws Exception {doTest(false);}
public void testAutoboxing() throws Exception {doTest(false);}
public void testAutoboxingMethods() throws Exception {doTest(false);}
public void testAutoboxingConstructors() throws Exception {doTest(false);}
public void testInferenceWithBounds() throws Exception { doTest(false); }
public void testInferenceWithSuperBounds() throws Exception { doTest(false); }
public void testInferenceWithUpperBoundPromotion() throws Exception { doTest(false); }
public void testVariance() throws Exception { doTest(false); }
public void testForeachTypes() throws Exception { doTest(false); }
public void testRawOverridingMethods() throws Exception { doTest(false); }
public void testAutoboxing() throws Exception { doTest(false); }
public void testAutoboxingMethods() throws Exception { doTest(false); }
public void testAutoboxingConstructors() throws Exception { doTest(false); }
public void testEnumWithAbstractMethods() throws Exception { doTest(false); }
public void testEnum() throws Exception { doTest(false); }
public void testSameErasure() throws Exception { doTest(false); }
public void testMethods() throws Exception { doTest(false); }
public void testFields() throws Exception { doTest(false); }
public void testStaticImports() throws Exception { doTest(true); }
public void testUncheckedCasts() throws Exception { doTest(true); }
public void testUncheckedOverriding() throws Exception { doTest(true); }
public void testWildcardTypes() throws Exception { doTest(true); }
public void testConvertibleTypes() throws Exception { doTest(true); }
public void testIntersectionTypes() throws Exception { doTest(true); }
public void testVarargs() throws Exception { doTest(true); }
public void testTypeArgsOnRaw() throws Exception { doTest(false); }
public void testConditionalExpression() throws Exception { doTest(false); }
public void testUnused() throws Exception { doTest(true); }
public void testIDEADEV7337() throws Exception { doTest(true); }
public void testIDEADEV10459() throws Exception { doTest(true); }
public void testIDEADEV12951() throws Exception { doTest(true); }
@@ -101,7 +113,6 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testIDEADEV25778() throws Exception { doTest(true); }
public void testIDEADEV57343() throws Exception { doTest(false); }
public void testSOE() throws Exception { doTest(true); }
public void testGenericExtendException() throws Exception { doTest(false); }
public void testSameErasureDifferentReturnTypes() throws Exception { doTest17Incompatibility(); }
public void testSameErasureDifferentReturnTypesJdk14() throws Exception { doTest(false); }
@@ -118,27 +129,25 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testInnerClassRef() throws Exception { doTest(false); }
public void testPrivateInnerClassRef() throws Exception { doTest(false); }
public void testWideningCastToTypeParam() throws Exception { doTest(false); }
public void testCapturedWildcardAssignments() throws Exception { doTest(false);}
public void testCapturedWildcardAssignments() throws Exception { doTest(false); }
public void testTypeParameterBoundVisibility() throws Exception { doTest17Incompatibility(); }
public void testTypeParameterBoundVisibilityJdk14() throws Exception { doTest(false);}
public void testUncheckedWarningsLevel6() throws Exception { doTest(true);}
public void testIDEA77991() throws Exception { doTest(false);}
public void testIDEA80386() throws Exception { doTest(false);}
public void testIDEA66311() throws Exception { doTest17Incompatibility();}
public void testIDEA88895() throws Exception { doTest17Incompatibility();}
public void testIDEA66311_16() throws Exception { doTest(false);}
public void testIDEA76283() throws Exception {doTest(false);}
public void testIDEA74899() throws Exception {doTest(false);}
public void testIDEA63291() throws Exception {doTest(false);}
public void testIDEA72912() throws Exception {doTest(false);}
public void testIllegalGenericTypeInInstanceof() throws Exception {doTest(false);}
public void testIDEA57339() throws Exception {doTest(false);}
public void testIDEA57340() throws Exception {doTest(false);}
public void testIDEA89771() throws Exception {doTest(false);}
public void testIDEA89801() throws Exception {doTest(false);}
public void testInconvertibleTypes() throws Exception {doTest(false);}
public void testTypeParameterBoundVisibilityJdk14() throws Exception { doTest(false); }
public void testUncheckedWarningsLevel6() throws Exception { doTest(true); }
public void testIDEA77991() throws Exception { doTest(false); }
public void testIDEA80386() throws Exception { doTest(false); }
public void testIDEA66311() throws Exception { doTest17Incompatibility(); }
public void testIDEA88895() throws Exception { doTest17Incompatibility(); }
public void testIDEA66311_16() throws Exception { doTest(false); }
public void testIDEA76283() throws Exception { doTest(false); }
public void testIDEA74899() throws Exception { doTest(false); }
public void testIDEA63291() throws Exception { doTest(false); }
public void testIDEA72912() throws Exception { doTest(false); }
public void testIllegalGenericTypeInInstanceof() throws Exception { doTest(false); }
public void testIDEA57339() throws Exception { doTest(false); }
public void testIDEA57340() throws Exception { doTest(false); }
public void testIDEA89771() throws Exception { doTest(false); }
public void testIDEA89801() throws Exception { doTest(false); }
public void testInconvertibleTypes() throws Exception { doTest(false); }
public void testJavaUtilCollections_NoVerify() throws Exception {
PsiClass collectionsClass = getJavaFacade().findClass("java.util.Collections", GlobalSearchScope.moduleWithLibrariesScope(getModule()));