Search in sources :

Example 71 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkMustBeThrowable.

@Nullable
static HighlightInfo checkMustBeThrowable(@Nullable PsiType type, @NotNull PsiElement context, boolean addCastIntention) {
    if (type == null)
        return null;
    PsiElementFactory factory = JavaPsiFacade.getInstance(context.getProject()).getElementFactory();
    PsiClassType throwable = factory.createTypeByFQClassName("java.lang.Throwable", context.getResolveScope());
    if (!TypeConversionUtil.isAssignable(throwable, type)) {
        HighlightInfo highlightInfo = createIncompatibleTypeHighlightInfo(throwable, type, context.getTextRange(), 0);
        if (addCastIntention && TypeConversionUtil.areTypesConvertible(type, throwable)) {
            if (context instanceof PsiExpression) {
                QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createAddTypeCastFix(throwable, (PsiExpression) context));
            }
        }
        final PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(type);
        if (aClass != null) {
            QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createExtendsListFix(aClass, throwable, true));
        }
        return highlightInfo;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 72 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkIntersectionInTypeCast.

/**
   * 15.16 Cast Expressions
   * ( ReferenceType {AdditionalBound} ) expression, where AdditionalBound: & InterfaceType then all must be true
   *  • ReferenceType must denote a class or interface type.
   *  • The erasures of all the listed types must be pairwise different.
   *  • No two listed types may be subtypes of different parameterization of the same generic interface.
   */
@Nullable
static HighlightInfo checkIntersectionInTypeCast(@NotNull PsiTypeCastExpression expression, @NotNull LanguageLevel languageLevel, @NotNull PsiFile file) {
    PsiTypeElement castTypeElement = expression.getCastType();
    if (castTypeElement != null && isIntersection(castTypeElement, castTypeElement.getType())) {
        HighlightInfo info = checkFeature(expression, Feature.INTERSECTION_CASTS, languageLevel, file);
        if (info != null)
            return info;
        final PsiTypeElement[] conjuncts = PsiTreeUtil.getChildrenOfType(castTypeElement, PsiTypeElement.class);
        if (conjuncts != null) {
            final Set<PsiType> erasures = new HashSet<>(conjuncts.length);
            erasures.add(TypeConversionUtil.erasure(conjuncts[0].getType()));
            final List<PsiTypeElement> conjList = new ArrayList<>(Arrays.asList(conjuncts));
            for (int i = 1; i < conjuncts.length; i++) {
                final PsiTypeElement conjunct = conjuncts[i];
                final PsiType conjType = conjunct.getType();
                if (conjType instanceof PsiClassType) {
                    final PsiClass aClass = ((PsiClassType) conjType).resolve();
                    if (aClass != null && !aClass.isInterface()) {
                        final HighlightInfo errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(conjunct).descriptionAndTooltip(JavaErrorMessages.message("interface.expected")).create();
                        QuickFixAction.registerQuickFixAction(errorResult, new FlipIntersectionSidesFix(aClass.getName(), conjList, conjunct, castTypeElement), null);
                        return errorResult;
                    }
                } else {
                    return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(conjunct).descriptionAndTooltip("Unexpected type: class is expected").create();
                }
                if (!erasures.add(TypeConversionUtil.erasure(conjType))) {
                    final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(conjunct).descriptionAndTooltip("Repeated interface").create();
                    QuickFixAction.registerQuickFixAction(highlightInfo, new DeleteRepeatedInterfaceFix(conjunct, conjList), null);
                    return highlightInfo;
                }
            }
            final List<PsiType> typeList = ContainerUtil.map(conjList, PsiTypeElement::getType);
            final Ref<String> differentArgumentsMessage = new Ref<>();
            final PsiClass sameGenericParameterization = InferenceSession.findParameterizationOfTheSameGenericClass(typeList, pair -> {
                if (!TypesDistinctProver.provablyDistinct(pair.first, pair.second)) {
                    return true;
                }
                differentArgumentsMessage.set(pair.first.getPresentableText() + " and " + pair.second.getPresentableText());
                return false;
            });
            if (sameGenericParameterization != null) {
                final String message = formatClass(sameGenericParameterization) + " cannot be inherited with different arguments: " + differentArgumentsMessage.get();
                return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(message).create();
            }
        }
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Ref(com.intellij.openapi.util.Ref) HashSet(com.intellij.util.containers.hash.HashSet)

Example 73 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkReference.

@Nullable
static HighlightInfo checkReference(@NotNull PsiJavaCodeReferenceElement ref, @NotNull JavaResolveResult result, @NotNull PsiFile containingFile, @NotNull LanguageLevel languageLevel) {
    PsiElement refName = ref.getReferenceNameElement();
    if (!(refName instanceof PsiIdentifier) && !(refName instanceof PsiKeyword))
        return null;
    PsiElement resolved = result.getElement();
    HighlightInfo highlightInfo = checkMemberReferencedBeforeConstructorCalled(ref, resolved, containingFile);
    if (highlightInfo != null)
        return highlightInfo;
    PsiElement refParent = ref.getParent();
    PsiElement granny;
    if (refParent instanceof PsiReferenceExpression && (granny = refParent.getParent()) instanceof PsiMethodCallExpression) {
        PsiReferenceExpression referenceToMethod = ((PsiMethodCallExpression) granny).getMethodExpression();
        PsiExpression qualifierExpression = referenceToMethod.getQualifierExpression();
        if (qualifierExpression == ref && resolved != null && !(resolved instanceof PsiClass) && !(resolved instanceof PsiVariable)) {
            String message = JavaErrorMessages.message("qualifier.must.be.expression");
            return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(qualifierExpression).descriptionAndTooltip(message).create();
        }
    } else if (refParent instanceof PsiMethodCallExpression) {
        // methods checked elsewhere
        return null;
    }
    if (resolved == null) {
        // do not highlight unknown packages (javac does not care), Javadoc, and module references (checked elsewhere)
        PsiElement outerParent = getOuterReferenceParent(ref);
        if (outerParent instanceof PsiPackageStatement || result.isPackagePrefixPackageReference() || PsiUtil.isInsideJavadocComment(ref) || outerParent instanceof PsiPackageAccessibilityStatement) {
            return null;
        }
        JavaResolveResult[] results = ref.multiResolve(true);
        String description;
        if (results.length > 1) {
            String t1 = format(ObjectUtils.notNull(results[0].getElement()));
            String t2 = format(ObjectUtils.notNull(results[1].getElement()));
            description = JavaErrorMessages.message("ambiguous.reference", refName.getText(), t1, t2);
        } else {
            description = JavaErrorMessages.message("cannot.resolve.symbol", refName.getText());
        }
        HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
        UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarImpl(info));
        return info;
    }
    if (!result.isValidResult() && !PsiUtil.isInsideJavadocComment(ref)) {
        if (!result.isAccessible()) {
            String message = buildProblemWithAccessDescription(ref, result, resolved);
            HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(message).create();
            if (result.isStaticsScopeCorrect()) {
                registerAccessQuickFixAction((PsiMember) resolved, ref, info, result.getCurrentFileResolveScope());
                if (ref instanceof PsiReferenceExpression) {
                    QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRenameWrongRefFix((PsiReferenceExpression) ref));
                }
            }
            UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarImpl(info));
            return info;
        }
        if (!result.isStaticsScopeCorrect()) {
            String description = buildProblemWithStaticDescription(resolved);
            HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
            registerStaticProblemQuickFixAction(resolved, info, ref);
            if (ref instanceof PsiReferenceExpression) {
                QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createRenameWrongRefFix((PsiReferenceExpression) ref));
            }
            return info;
        }
    }
    if ((resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter) && !(resolved instanceof ImplicitVariable)) {
        return HighlightControlFlowUtil.checkVariableMustBeFinal((PsiVariable) resolved, ref, languageLevel);
    }
    if (resolved instanceof PsiClass && ((PsiClass) resolved).getContainingClass() == null && PsiTreeUtil.getParentOfType(ref, PsiImportStatementBase.class) != null && PsiUtil.isFromDefaultPackage((PsiClass) resolved)) {
        String description = JavaErrorMessages.message("cannot.resolve.symbol", refName.getText());
        return HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(refName).descriptionAndTooltip(description).create();
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 74 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkElementInReferenceList.

@Nullable
static HighlightInfo checkElementInReferenceList(@NotNull PsiJavaCodeReferenceElement ref, @NotNull PsiReferenceList referenceList, @NotNull JavaResolveResult resolveResult) {
    PsiElement resolved = resolveResult.getElement();
    HighlightInfo highlightInfo = null;
    PsiElement refGrandParent = referenceList.getParent();
    if (resolved instanceof PsiClass) {
        PsiClass aClass = (PsiClass) resolved;
        if (refGrandParent instanceof PsiClass) {
            if (refGrandParent instanceof PsiTypeParameter) {
                highlightInfo = GenericsHighlightUtil.checkElementInTypeParameterExtendsList(referenceList, (PsiClass) refGrandParent, resolveResult, ref);
            } else {
                highlightInfo = HighlightClassUtil.checkExtendsClassAndImplementsInterface(referenceList, resolveResult, ref);
                if (highlightInfo == null) {
                    highlightInfo = HighlightClassUtil.checkCannotInheritFromFinal(aClass, ref);
                }
                if (highlightInfo == null) {
                    highlightInfo = GenericsHighlightUtil.checkCannotInheritFromEnum(aClass, ref);
                }
                if (highlightInfo == null) {
                    highlightInfo = GenericsHighlightUtil.checkCannotInheritFromTypeParameter(aClass, ref);
                }
            }
        } else if (refGrandParent instanceof PsiMethod && ((PsiMethod) refGrandParent).getThrowsList() == referenceList) {
            highlightInfo = checkMustBeThrowable(aClass, ref);
        }
    } else if (refGrandParent instanceof PsiMethod && referenceList == ((PsiMethod) refGrandParent).getThrowsList()) {
        String description = JavaErrorMessages.message("class.name.expected");
        highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create();
    }
    return highlightInfo;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 75 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class HighlightUtil method checkAssignmentCompatibleTypes.

@Nullable
static HighlightInfo checkAssignmentCompatibleTypes(@NotNull PsiAssignmentExpression assignment) {
    PsiExpression lExpr = assignment.getLExpression();
    PsiExpression rExpr = assignment.getRExpression();
    if (rExpr == null)
        return null;
    PsiType lType = lExpr.getType();
    PsiType rType = rExpr.getType();
    if (rType == null)
        return null;
    final IElementType sign = assignment.getOperationTokenType();
    HighlightInfo highlightInfo;
    if (JavaTokenType.EQ.equals(sign)) {
        highlightInfo = checkAssignability(lType, rType, rExpr, assignment);
    } else {
        // 15.26.2. Compound Assignment Operators
        final IElementType opSign = TypeConversionUtil.convertEQtoOperation(sign);
        final PsiType type = TypeConversionUtil.calcTypeForBinaryExpression(lType, rType, opSign, true);
        if (type == null || lType == null || TypeConversionUtil.areTypesConvertible(type, lType)) {
            return null;
        }
        highlightInfo = createIncompatibleTypeHighlightInfo(lType, type, assignment.getTextRange(), 0);
        QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createChangeToAppendFix(sign, lType, assignment));
    }
    if (highlightInfo == null) {
        return null;
    }
    registerChangeVariableTypeFixes(lExpr, rType, rExpr, highlightInfo);
    if (lType != null) {
        registerChangeVariableTypeFixes(rExpr, lType, lExpr, highlightInfo);
    }
    return highlightInfo;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)221 Nullable (org.jetbrains.annotations.Nullable)51 TextRange (com.intellij.openapi.util.TextRange)33 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 NotNull (org.jetbrains.annotations.NotNull)17 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)16 Document (com.intellij.openapi.editor.Document)12 ArrayList (java.util.ArrayList)11 PsiElement (com.intellij.psi.PsiElement)10 File (java.io.File)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Pair (com.intellij.openapi.util.Pair)6 PsiFile (com.intellij.psi.PsiFile)6 Editor (com.intellij.openapi.editor.Editor)5 NonNls (org.jetbrains.annotations.NonNls)5 StringUtil (com.intellij.openapi.util.text.StringUtil)4 IElementType (com.intellij.psi.tree.IElementType)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4