Search in sources :

Example 1 with LocalQuickFixAndIntentionActionOnPsiElement

use of com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement in project intellij-community by JetBrains.

the class HighlightVisitorImpl method visitMethodReferenceExpression.

@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
    myHolder.add(checkFeature(expression, Feature.METHOD_REFERENCES));
    final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
    if (parent instanceof PsiExpressionStatement)
        return;
    final JavaResolveResult result;
    final JavaResolveResult[] results;
    try {
        results = expression.multiResolve(true);
        result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
    } catch (IndexNotReadyException e) {
        return;
    }
    if (myRefCountHolder != null) {
        myRefCountHolder.registerReference(expression, result);
    }
    final PsiElement method = result.getElement();
    if (method != null && !result.isAccessible()) {
        final String accessProblem = HighlightUtil.buildProblemWithAccessDescription(expression, result);
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(accessProblem).create();
        HighlightUtil.registerAccessQuickFixAction((PsiMember) method, expression, info, result.getCurrentFileResolveScope());
        myHolder.add(info);
    } else {
        final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
        if (method instanceof PsiMethod && !expression.isConstructor()) {
            PsiElement methodNameElement = expression.getReferenceNameElement();
            if (methodNameElement != null) {
                myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod) method, methodNameElement, false, colorsScheme));
            }
        }
        myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(expression, colorsScheme));
    }
    if (!LambdaUtil.isValidLambdaContext(parent)) {
        String description = "Method reference expression is not expected here";
        myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
    }
    final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
    if (!myHolder.hasErrorResults()) {
        if (functionalInterfaceType != null) {
            final boolean notFunctional = !LambdaUtil.isFunctionalType(functionalInterfaceType);
            if (notFunctional) {
                String description = functionalInterfaceType.getPresentableText() + " is not a functional interface";
                myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        final PsiElement referenceNameElement = expression.getReferenceNameElement();
        if (referenceNameElement instanceof PsiKeyword) {
            if (!PsiMethodReferenceUtil.isValidQualifier(expression)) {
                PsiElement qualifier = expression.getQualifier();
                if (qualifier != null) {
                    String description = "Cannot find class " + qualifier.getText();
                    myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create());
                }
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        checkFunctionalInterfaceTypeAccessible(expression, functionalInterfaceType);
    }
    if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
        final String errorMessage = PsiMethodReferenceUtil.checkMethodReferenceContext(expression);
        if (errorMessage != null) {
            final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(errorMessage).create();
            if (method instanceof PsiMethod && !((PsiMethod) method).isConstructor() && !((PsiMethod) method).hasModifierProperty(PsiModifier.ABSTRACT)) {
                final boolean shouldHave = !((PsiMethod) method).hasModifierProperty(PsiModifier.STATIC);
                final LocalQuickFixAndIntentionActionOnPsiElement fixStaticModifier = QuickFixFactory.getInstance().createModifierListFix((PsiModifierListOwner) method, PsiModifier.STATIC, shouldHave, false);
                QuickFixAction.registerQuickFixAction(info, fixStaticModifier);
            }
            myHolder.add(info);
        }
    }
    if (!myHolder.hasErrorResults()) {
        PsiElement qualifier = expression.getQualifier();
        if (qualifier instanceof PsiTypeElement) {
            final PsiType psiType = ((PsiTypeElement) qualifier).getType();
            final HighlightInfo genericArrayCreationInfo = GenericsHighlightUtil.checkGenericArrayCreation(qualifier, psiType);
            if (genericArrayCreationInfo != null) {
                myHolder.add(genericArrayCreationInfo);
            } else {
                final String wildcardMessage = PsiMethodReferenceUtil.checkTypeArguments((PsiTypeElement) qualifier, psiType);
                if (wildcardMessage != null) {
                    myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(wildcardMessage).create());
                }
            }
        }
    }
    if (!myHolder.hasErrorResults()) {
        myHolder.add(PsiMethodReferenceHighlightingUtil.checkRawConstructorReference(expression));
    }
    if (!myHolder.hasErrorResults()) {
        myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, expression.getTextRange()));
    }
    if (!myHolder.hasErrorResults()) {
        final String badReturnTypeMessage = PsiMethodReferenceUtil.checkReturnType(expression, result, functionalInterfaceType);
        if (badReturnTypeMessage != null) {
            myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(badReturnTypeMessage).create());
        }
    }
    if (!myHolder.hasErrorResults()) {
        if (results.length == 0 || results[0] instanceof MethodCandidateInfo && !((MethodCandidateInfo) results[0]).isApplicable() && functionalInterfaceType != null) {
            String description = null;
            if (results.length == 1) {
                description = ((MethodCandidateInfo) results[0]).getInferenceErrorMessage();
            }
            if (expression.isConstructor()) {
                final PsiClass containingClass = PsiMethodReferenceUtil.getQualifierResolveResult(expression).getContainingClass();
                if (containingClass != null) {
                    if (!myHolder.add(HighlightClassUtil.checkInstantiationOfAbstractClass(containingClass, expression)) && !myHolder.add(GenericsHighlightUtil.checkEnumInstantiation(expression, containingClass)) && containingClass.isPhysical() && description == null) {
                        description = JavaErrorMessages.message("cannot.resolve.constructor", containingClass.getName());
                    }
                }
            } else if (description == null) {
                description = JavaErrorMessages.message("cannot.resolve.method", expression.getReferenceName());
            }
            if (description != null) {
                final PsiElement referenceNameElement = expression.getReferenceNameElement();
                final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(results.length == 0 ? HighlightInfoType.WRONG_REF : HighlightInfoType.ERROR).descriptionAndTooltip(description).range(referenceNameElement).create();
                myHolder.add(highlightInfo);
                final TextRange fixRange = HighlightMethodUtil.getFixRange(referenceNameElement);
                QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QuickFixFactory.getInstance().createCreateMethodFromUsageFix(expression));
            }
        }
    }
}
Also used : TextAttributesScheme(com.intellij.openapi.editor.colors.TextAttributesScheme) TextRange(com.intellij.openapi.util.TextRange) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Aggregations

LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 TextAttributesScheme (com.intellij.openapi.editor.colors.TextAttributesScheme)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 TextRange (com.intellij.openapi.util.TextRange)1 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)1