Replace parser conditionals with language level highlighting

This commit is contained in:
Roman Shevchenko
2012-05-11 21:09:41 +04:00
parent 97eb59356d
commit 7115bb9787
14 changed files with 249 additions and 189 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* 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.
@@ -51,7 +51,6 @@ import java.util.*;
*/
public class GenericsHighlightUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil");
private static final String GENERICS_ARE_NOT_SUPPORTED = JavaErrorMessages.message("generics.are.not.supported");
private static final QuickFixFactory QUICK_FIX_FACTORY = QuickFixFactory.getInstance();
private GenericsHighlightUtil() {}
@@ -102,6 +101,7 @@ public class GenericsHighlightUtil {
return null;
}
@Nullable
public static HighlightInfo checkParameterizedReferenceTypeArguments(PsiElement resolved,
final PsiJavaCodeReferenceElement referenceElement,
final PsiSubstitutor substitutor) {
@@ -110,17 +110,14 @@ public class GenericsHighlightUtil {
return checkReferenceTypeArgumentList(typeParameterListOwner, referenceElement.getParameterList(), substitutor, true);
}
@Nullable
public static HighlightInfo checkReferenceTypeArgumentList(final PsiTypeParameterListOwner typeParameterListOwner,
final PsiReferenceParameterList referenceParameterList,
final PsiSubstitutor substitutor,
boolean registerIntentions) {
if (referenceParameterList != null && !PsiUtil.isLanguageLevel5OrHigher(referenceParameterList)) {
if (referenceParameterList.getTypeParameterElements().length > 0) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, referenceParameterList, GENERICS_ARE_NOT_SUPPORTED);
QuickFixAction.registerQuickFixAction(info, new ShowModulePropertiesFix(referenceParameterList));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
return info;
}
if (referenceParameterList != null) {
HighlightInfo info = HighlightUtil.checkGenericsFeature(referenceParameterList, referenceParameterList.getTypeParameterElements().length);
if (info != null) return info;
}
PsiDiamondType.DiamondInferenceResult inferenceResult = null;
@@ -914,15 +911,13 @@ public class GenericsHighlightUtil {
return valueOfMethod.equals(methodSignature);
}
@Nullable
public static HighlightInfo checkTypeParametersList(PsiTypeParameterList parameterList) {
PsiTypeParameter[] typeParameters = parameterList.getTypeParameters();
if (typeParameters.length == 0) return null;
if (!PsiUtil.isLanguageLevel5OrHigher(parameterList)) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, parameterList, GENERICS_ARE_NOT_SUPPORTED);
QuickFixAction.registerQuickFixAction(info, new ShowModulePropertiesFix(parameterList));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
return info;
}
HighlightInfo info = HighlightUtil.checkGenericsFeature(parameterList, typeParameters.length);
if (info != null) return info;
final PsiElement parent = parameterList.getParent();
if (parent instanceof PsiClass && ((PsiClass)parent).isEnum()) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR,
@@ -1208,21 +1203,18 @@ public class GenericsHighlightUtil {
return null;
}
@Nullable
public static HighlightInfo checkVarArgParameterIsLast(PsiParameter parameter) {
PsiElement declarationScope = parameter.getDeclarationScope();
if (declarationScope instanceof PsiMethod) {
PsiParameter[] params = ((PsiMethod)declarationScope).getParameterList().getParameters();
if (parameter.isVarArgs()) {
if (!PsiUtil.getLanguageLevel(parameter).hasEnumKeywordAndAutoboxing()) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, parameter, JavaErrorMessages.message("varargs.prior.15"));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
return info;
}
HighlightInfo info = HighlightUtil.checkVarargFeature(parameter);
if (info != null) return info;
if (params[params.length - 1] != parameter) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, parameter,
JavaErrorMessages.message("vararg.not.last.parameter"));
QuickFixAction.registerQuickFixAction(info, new MakeVarargParameterLastFix(parameter), null);
info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, parameter, JavaErrorMessages.message("vararg.not.last.parameter"));
QuickFixAction.registerQuickFixAction(info, new MakeVarargParameterLastFix(parameter));
return info;
}
}
@@ -1230,6 +1222,7 @@ public class GenericsHighlightUtil {
return null;
}
@Nullable
public static List<HighlightInfo> checkEnumConstantModifierList(PsiModifierList modifierList) {
List<HighlightInfo> list = null;
PsiElement[] children = modifierList.getChildren();
@@ -58,6 +58,7 @@ import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
import java.util.*;
@@ -593,6 +594,7 @@ public class HighlightUtil {
if (variable instanceof PsiLocalVariable ||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection ||
variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement) {
@SuppressWarnings("unchecked")
PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {
@Override
@@ -602,7 +604,9 @@ public class HighlightUtil {
};
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
if (scope instanceof PsiResourceList && proc.size() == 0) {
scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
NavigatablePsiElement parent = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
scope = parent;
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
}
if (proc.size() > 0) {
@@ -1403,7 +1407,7 @@ public class HighlightUtil {
}
@Nullable
static HighlightInfo checkValidArrayAccessExpression(PsiExpression arrayExpression, PsiExpression indexExpression, PsiType type) {
static HighlightInfo checkValidArrayAccessExpression(@Nullable PsiExpression arrayExpression, PsiExpression indexExpression, PsiType type) {
PsiType arrayExpressionType = arrayExpression == null ? null : arrayExpression.getType();
if (arrayExpressionType != null && !(arrayExpressionType instanceof PsiArrayType)) {
String description = JavaErrorMessages.message("array.type.expected", formatType(arrayExpressionType));
@@ -1744,9 +1748,11 @@ public class HighlightUtil {
referencedClass = PsiUtil.resolveClassInType(type);
}
else if (qualifier instanceof PsiThisExpression || qualifier == null) {
resolved = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiMember.class);
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
PsiMethod parent = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiMember.class);
resolved = parent;
expression = qualifier == null ? expression : qualifier;
if (resolved instanceof PsiMethod) {
if (resolved != null) {
referencedClass = ((PsiMethod)resolved).getContainingClass();
}
}
@@ -2452,7 +2458,7 @@ public class HighlightUtil {
public static void registerChangeVariableTypeFixes(PsiVariable parameter, PsiType itemType, HighlightInfo highlightInfo) {
for (ChangeVariableTypeQuickFixProvider fixProvider : Extensions.getExtensions(ChangeVariableTypeQuickFixProvider.EP_NAME)) {
for (IntentionAction action : fixProvider.getFixes(parameter, itemType)) {
QuickFixAction.registerQuickFixAction(highlightInfo, action, null);
QuickFixAction.registerQuickFixAction(highlightInfo, action);
}
}
ChangeParameterClassFix.registerQuickFixAction(parameter, itemType, highlightInfo);
@@ -2466,4 +2472,76 @@ public class HighlightUtil {
}
return null;
}
private static enum Feature {
GENERICS(LanguageLevel.JDK_1_5, "feature.generics"),
ANNOTATIONS(LanguageLevel.JDK_1_5, "feature.annotations"),
STATIC_IMPORTS(LanguageLevel.JDK_1_5, "feature.static.imports"),
FOR_EACH(LanguageLevel.JDK_1_5, "feature.for.each"),
VARARGS(LanguageLevel.JDK_1_5, "feature.varargs"),
DIAMOND_TYPES(LanguageLevel.JDK_1_7, "feature.diamond.types"),
MULTI_CATCH(LanguageLevel.JDK_1_7, "feature.multi.catch"),
TRY_WITH_RESOURCES(LanguageLevel.JDK_1_7, "feature.try.with.resources");
private final LanguageLevel level;
private final String key;
private Feature(final LanguageLevel level, @PropertyKey(resourceBundle = JavaErrorMessages.BUNDLE) final String key) {
this.level = level;
this.key = key;
}
}
@Nullable
private static HighlightInfo checkFeature(@Nullable final PsiElement element, @NotNull final Feature feature) {
if (element != null && !PsiUtil.getLanguageLevel(element).isAtLeast(feature.level)) {
final String message = JavaErrorMessages.message("insufficient.language.level", JavaErrorMessages.message(feature.key));
final HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, element, message);
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(feature.level));
QuickFixAction.registerQuickFixAction(info, new ShowModulePropertiesFix(element));
return info;
}
return null;
}
@Nullable
public static HighlightInfo checkGenericsFeature(final PsiElement parameterList, final int listSize) {
return listSize > 0 ? checkFeature(parameterList, Feature.GENERICS) : null;
}
@Nullable
public static HighlightInfo checkAnnotationFeature(final PsiElement element) {
return checkFeature(element, Feature.ANNOTATIONS);
}
@Nullable
public static HighlightInfo checkForEachFeature(final PsiForeachStatement statement) {
return checkFeature(statement, Feature.FOR_EACH);
}
@Nullable
public static HighlightInfo checkStaticImportFeature(final PsiImportStaticStatement statement) {
return checkFeature(statement, Feature.STATIC_IMPORTS);
}
@Nullable
public static HighlightInfo checkVarargFeature(final PsiParameter parameter) {
return checkFeature(parameter, Feature.VARARGS);
}
@Nullable
public static HighlightInfo checkDiamondFeature(final PsiTypeElement typeElement) {
return typeElement.getType() instanceof PsiDiamondType ? checkFeature(typeElement.getParent(), Feature.DIAMOND_TYPES) : null;
}
@Nullable
public static HighlightInfo checkMultiCatchFeature(final PsiParameter parameter) {
return parameter.getType() instanceof PsiDisjunctionType ? checkFeature(parameter, Feature.MULTI_CATCH) : null;
}
@Nullable
public static HighlightInfo checkTryWithResourcesFeature(final PsiResourceVariable resourceVariable) {
return checkFeature(resourceVariable.getParent(), Feature.TRY_WITH_RESOURCES);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* 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.
@@ -30,7 +30,6 @@ import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.ControlFlowUtil;
import com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef;
@@ -170,16 +169,11 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
}
@Override public void visitAnnotation(PsiAnnotation annotation) {
@Override
public void visitAnnotation(PsiAnnotation annotation) {
super.visitAnnotation(annotation);
if (!PsiUtil.isLanguageLevel5OrHigher(annotation)) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, annotation, JavaErrorMessages.message("annotations.prior.15"));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
myHolder.add(info);
return;
}
myHolder.add(AnnotationsHighlightUtil.checkApplicability(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAnnotationFeature(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkApplicability(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkAnnotationType(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkMissingAttributes(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkTargetAnnotationDuplicates(annotation));
@@ -187,7 +181,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkForeignInnerClassesUsed(annotation));
}
@Override public void visitAnnotationArrayInitializer(PsiArrayInitializerMemberValue initializer) {
@Override
public void visitAnnotationArrayInitializer(PsiArrayInitializerMemberValue initializer) {
PsiMethod method = null;
PsiElement parent = initializer.getParent();
if (parent instanceof PsiNameValuePair) {
@@ -364,28 +359,24 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
}
@Override public void visitField(PsiField field) {
@Override
public void visitField(PsiField field) {
super.visitField(field);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkFinalFieldInitialized(field));
}
@Override public void visitForeachStatement(PsiForeachStatement statement) {
if (!PsiUtil.isLanguageLevel5OrHigher(statement)) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, statement.getFirstChild(), JavaErrorMessages.message("foreach.prior.15"));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
myHolder.add(info);
}
@Override
public void visitForeachStatement(final PsiForeachStatement statement) {
myHolder.add(HighlightUtil.checkForEachFeature(statement));
}
@Override public void visitImportStaticStatement(PsiImportStaticStatement statement) {
if (!PsiUtil.isLanguageLevel5OrHigher(statement)) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, statement.getFirstChild(), JavaErrorMessages.message("static.imports.prior.15"));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
myHolder.add(info);
}
@Override
public void visitImportStaticStatement(final PsiImportStaticStatement statement) {
myHolder.add(HighlightUtil.checkStaticImportFeature(statement));
}
@Override public void visitIdentifier(PsiIdentifier identifier) {
@Override
public void visitIdentifier(final PsiIdentifier identifier) {
PsiElement parent = identifier.getParent();
final EditorColorsScheme colorsScheme = myHolder.getColorsScheme();
if (parent instanceof PsiVariable) {
@@ -411,10 +402,8 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass)parent;
if (aClass.isAnnotationType() && !PsiUtil.isLanguageLevel5OrHigher(aClass)) {
HighlightInfo info = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, identifier, JavaErrorMessages.message("annotations.prior.15"));
QuickFixAction.registerQuickFixAction(info, new IncreaseLanguageLevelFix(LanguageLevel.JDK_1_5));
myHolder.add(info);
if (aClass.isAnnotationType()) {
myHolder.add(HighlightUtil.checkAnnotationFeature(identifier));
}
myHolder.add(HighlightClassUtil.checkClassAlreadyImported(aClass, identifier));
@@ -742,6 +731,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkForeachLoopParameterType((PsiForeachStatement)parent));
}
else if (parent instanceof PsiCatchSection) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkMultiCatchFeature(parameter));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkCatchParameterIsThrowable(parameter));
if (!myHolder.hasErrorResults()) myHolder.addAll(GenericsHighlightUtil.checkCatchParameterIsClass(parameter));
if (!myHolder.hasErrorResults()) myHolder.addAll(HighlightUtil.checkCatchTypeIsDisjoint(parameter));
@@ -987,11 +977,14 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitResourceVariable(final PsiResourceVariable resourceVariable) {
visitVariable(resourceVariable);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTryWithResourcesFeature(resourceVariable));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTryResourceIsAutoCloseable(resourceVariable));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnhandledCloserExceptions(resourceVariable));
}
@Override public void visitTypeElement(PsiTypeElement type) {
@Override
public void visitTypeElement(final PsiTypeElement type) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkDiamondFeature(type));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalType(type));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkReferenceTypeUsedAsTypeArgument(type));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkWildcardUsage(type));