Search in sources :

Example 31 with HighlightInfo

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

the class HighlightMethodUtil method checkAmbiguousMethodCallArguments.

@Nullable
static HighlightInfo checkAmbiguousMethodCallArguments(@NotNull PsiReferenceExpression referenceToMethod, @NotNull JavaResolveResult[] resolveResults, @NotNull PsiExpressionList list, final PsiElement element, @NotNull JavaResolveResult resolveResult, @NotNull PsiMethodCallExpression methodCall, @NotNull PsiResolveHelper resolveHelper, @NotNull PsiElement elementToHighlight) {
    MethodCandidateInfo methodCandidate1 = null;
    MethodCandidateInfo methodCandidate2 = null;
    for (JavaResolveResult result : resolveResults) {
        if (!(result instanceof MethodCandidateInfo))
            continue;
        MethodCandidateInfo candidate = (MethodCandidateInfo) result;
        if (candidate.isApplicable() && !candidate.getElement().isConstructor()) {
            if (methodCandidate1 == null) {
                methodCandidate1 = candidate;
            } else {
                methodCandidate2 = candidate;
                break;
            }
        }
    }
    MethodCandidateInfo[] candidates = toMethodCandidates(resolveResults);
    String description;
    String toolTip;
    HighlightInfoType highlightInfoType = HighlightInfoType.ERROR;
    if (methodCandidate2 != null) {
        PsiMethod element1 = methodCandidate1.getElement();
        String m1 = PsiFormatUtil.formatMethod(element1, methodCandidate1.getSubstitutor(false), PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
        PsiMethod element2 = methodCandidate2.getElement();
        String m2 = PsiFormatUtil.formatMethod(element2, methodCandidate2.getSubstitutor(false), PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
        VirtualFile virtualFile1 = PsiUtilCore.getVirtualFile(element1);
        VirtualFile virtualFile2 = PsiUtilCore.getVirtualFile(element2);
        if (!Comparing.equal(virtualFile1, virtualFile2)) {
            if (virtualFile1 != null)
                m1 += " (In " + virtualFile1.getPresentableUrl() + ")";
            if (virtualFile2 != null)
                m2 += " (In " + virtualFile2.getPresentableUrl() + ")";
        }
        description = JavaErrorMessages.message("ambiguous.method.call", m1, m2);
        toolTip = createAmbiguousMethodHtmlTooltip(new MethodCandidateInfo[] { methodCandidate1, methodCandidate2 });
    } else {
        if (element != null && !resolveResult.isAccessible()) {
            return null;
        }
        if (element != null && !resolveResult.isStaticsScopeCorrect()) {
            return null;
        }
        String methodName = referenceToMethod.getReferenceName() + buildArgTypesList(list);
        description = JavaErrorMessages.message("cannot.resolve.method", methodName);
        if (candidates.length == 0) {
            return null;
        }
        toolTip = XmlStringUtil.escapeString(description);
    }
    HighlightInfo info = HighlightInfo.newHighlightInfo(highlightInfoType).range(elementToHighlight).description(description).escapedToolTip(toolTip).create();
    if (methodCandidate2 == null) {
        registerMethodCallIntentions(info, methodCall, list, resolveHelper);
    }
    if (!resolveResult.isAccessible() && resolveResult.isStaticsScopeCorrect() && methodCandidate2 != null) {
        HighlightUtil.registerAccessQuickFixAction((PsiMember) element, referenceToMethod, info, resolveResult.getCurrentFileResolveScope());
    }
    if (element != null && !resolveResult.isStaticsScopeCorrect()) {
        HighlightUtil.registerStaticProblemQuickFixAction(element, info, referenceToMethod);
    }
    TextRange fixRange = getFixRange(elementToHighlight);
    CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, methodCall, info, fixRange);
    WrapArrayToArraysAsListFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
    WrapLongWithMathToIntExactFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
    WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
    WrapStringWithFileFix.REGISTAR.registerCastActions(candidates, methodCall, info, fixRange);
    PermuteArgumentsFix.registerFix(info, methodCall, candidates, fixRange);
    WrapExpressionFix.registerWrapAction(candidates, list.getExpressions(), info);
    registerChangeParameterClassFix(methodCall, list, info);
    return info;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with HighlightInfo

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

the class HighlightMethodUtil method checkInterfaceInheritedMethodsReturnTypes.

private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes(@NotNull List<? extends MethodSignatureBackedByPsiMethod> superMethodSignatures, @NotNull LanguageLevel languageLevel) {
    if (superMethodSignatures.size() < 2)
        return null;
    final MethodSignatureBackedByPsiMethod[] returnTypeSubstitutable = { superMethodSignatures.get(0) };
    for (int i = 1; i < superMethodSignatures.size(); i++) {
        PsiMethod currentMethod = returnTypeSubstitutable[0].getMethod();
        PsiType currentType = returnTypeSubstitutable[0].getSubstitutor().substitute(currentMethod.getReturnType());
        MethodSignatureBackedByPsiMethod otherSuperSignature = superMethodSignatures.get(i);
        PsiMethod otherSuperMethod = otherSuperSignature.getMethod();
        PsiSubstitutor otherSubstitutor = otherSuperSignature.getSubstitutor();
        PsiType otherSuperReturnType = otherSubstitutor.substitute(otherSuperMethod.getReturnType());
        PsiSubstitutor unifyingSubstitutor = MethodSignatureUtil.getSuperMethodSignatureSubstitutor(returnTypeSubstitutable[0], otherSuperSignature);
        if (unifyingSubstitutor != null) {
            otherSuperReturnType = unifyingSubstitutor.substitute(otherSuperReturnType);
            currentType = unifyingSubstitutor.substitute(currentType);
        }
        if (otherSuperReturnType == null || currentType == null || otherSuperReturnType.equals(currentType))
            continue;
        PsiType otherReturnType = otherSuperReturnType;
        PsiType curType = currentType;
        final HighlightInfo info = LambdaUtil.performWithSubstitutedParameterBounds(otherSuperMethod.getTypeParameters(), otherSubstitutor, () -> {
            if (languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) {
                //http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8 Example 8.1.5-3
                if (!(otherReturnType instanceof PsiPrimitiveType || curType instanceof PsiPrimitiveType)) {
                    if (otherReturnType.isAssignableFrom(curType))
                        return null;
                    if (curType.isAssignableFrom(otherReturnType)) {
                        returnTypeSubstitutable[0] = otherSuperSignature;
                        return null;
                    }
                }
                if (otherSuperMethod.getTypeParameters().length > 0 && JavaGenericsUtil.isRawToGeneric(otherReturnType, curType))
                    return null;
            }
            return createIncompatibleReturnTypeMessage(otherSuperMethod, currentMethod, curType, otherReturnType, JavaErrorMessages.message("unrelated.overriding.methods.return.types"), TextRange.EMPTY_RANGE);
        });
        if (info != null)
            return info;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 33 with HighlightInfo

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

the class AnnotationsHighlightUtil method checkApplicability.

@Nullable
public static HighlightInfo checkApplicability(@NotNull PsiAnnotation annotation, @NotNull LanguageLevel level, @NotNull PsiFile file) {
    if (ANY_ANNOTATION_ALLOWED.accepts(annotation)) {
        return null;
    }
    PsiJavaCodeReferenceElement nameRef = annotation.getNameReferenceElement();
    if (nameRef == null)
        return null;
    PsiAnnotationOwner owner = annotation.getOwner();
    PsiAnnotation.TargetType[] targets = AnnotationTargetUtil.getTargetsForLocation(owner);
    if (owner == null || targets.length == 0) {
        String message = JavaErrorMessages.message("annotation.not.allowed.here");
        return annotationError(annotation, message);
    }
    if (!(owner instanceof PsiModifierList)) {
        HighlightInfo info = HighlightUtil.checkFeature(annotation, HighlightUtil.Feature.TYPE_ANNOTATIONS, level, file);
        if (info != null)
            return info;
    }
    PsiAnnotation.TargetType applicable = AnnotationTargetUtil.findAnnotationTarget(annotation, targets);
    if (applicable == PsiAnnotation.TargetType.UNKNOWN)
        return null;
    if (applicable == null) {
        String target = JavaErrorMessages.message("annotation.target." + targets[0]);
        String message = JavaErrorMessages.message("annotation.not.applicable", nameRef.getText(), target);
        return annotationError(annotation, message);
    }
    if (applicable == PsiAnnotation.TargetType.TYPE_USE) {
        if (owner instanceof PsiClassReferenceType) {
            PsiJavaCodeReferenceElement ref = ((PsiClassReferenceType) owner).getReference();
            HighlightInfo info = checkReferenceTarget(annotation, ref);
            if (info != null)
                return info;
        } else if (owner instanceof PsiModifierList) {
            PsiElement nextElement = PsiTreeUtil.skipSiblingsForward((PsiModifierList) owner, PsiComment.class, PsiWhiteSpace.class, PsiTypeParameterList.class);
            if (nextElement instanceof PsiTypeElement) {
                PsiTypeElement typeElement = (PsiTypeElement) nextElement;
                PsiType type = typeElement.getType();
                if (PsiType.VOID.equals(type)) {
                    String message = JavaErrorMessages.message("annotation.not.allowed.void");
                    return annotationError(annotation, message);
                }
                if (!(type instanceof PsiPrimitiveType)) {
                    PsiJavaCodeReferenceElement ref = getOutermostReferenceElement(typeElement.getInnermostComponentReferenceElement());
                    HighlightInfo info = checkReferenceTarget(annotation, ref);
                    if (info != null)
                        return info;
                }
            }
        } else if (owner instanceof PsiTypeElement) {
            PsiElement context = PsiTreeUtil.skipParentsOfType((PsiTypeElement) owner, PsiTypeElement.class);
            if (context instanceof PsiClassObjectAccessExpression) {
                String message = JavaErrorMessages.message("annotation.not.allowed.class");
                return annotationError(annotation, message);
            }
        }
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with HighlightInfo

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

the class AnnotationsHighlightUtil method annotationError.

private static HighlightInfo annotationError(PsiAnnotation annotation, String message) {
    HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(annotation).descriptionAndTooltip(message).create();
    QuickFixAction.registerQuickFixAction(info, new DeleteAnnotationAction(annotation));
    return info;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 35 with HighlightInfo

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

the class GenericsHighlightUtil method checkForEachParameterType.

static HighlightInfo checkForEachParameterType(@NotNull PsiForeachStatement statement, @NotNull PsiParameter parameter) {
    final PsiExpression expression = statement.getIteratedValue();
    final PsiType itemType = expression == null ? null : JavaGenericsUtil.getCollectionItemType(expression);
    if (itemType == null)
        return null;
    final PsiType parameterType = parameter.getType();
    if (TypeConversionUtil.isAssignable(parameterType, itemType)) {
        return null;
    }
    HighlightInfo highlightInfo = HighlightUtil.createIncompatibleTypeHighlightInfo(itemType, parameterType, parameter.getTextRange(), 0);
    HighlightUtil.registerChangeVariableTypeFixes(parameter, itemType, expression, highlightInfo);
    return highlightInfo;
}
Also used : 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