Search in sources :

Example 21 with HighlightInfo

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

the class SliceTreeTest method configureTree.

private SliceTreeStructure configureTree(@NonNls final String name) throws Exception {
    configureByFile("/codeInsight/slice/backward/" + name + ".java");
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    PsiElement element = new SliceHandler(true).getExpressionAtCaret(getEditor(), getFile());
    assertNotNull(element);
    Collection<HighlightInfo> errors = highlightErrors();
    assertEmpty(errors);
    SliceAnalysisParams params = new SliceAnalysisParams();
    params.scope = new AnalysisScope(getProject());
    params.dataFlowToThis = true;
    SliceUsage usage = LanguageSlicing.getProvider(element).createRootUsage(element, params);
    ToolWindowHeadlessManagerImpl.MockToolWindow toolWindow = new ToolWindowHeadlessManagerImpl.MockToolWindow(myProject);
    SlicePanel panel = new SlicePanel(getProject(), true, new SliceRootNode(getProject(), new DuplicateMap(), usage), false, toolWindow) {

        @Override
        protected void close() {
        }

        @Override
        public boolean isAutoScroll() {
            return false;
        }

        @Override
        public void setAutoScroll(boolean autoScroll) {
        }

        @Override
        public boolean isPreview() {
            return false;
        }

        @Override
        public void setPreview(boolean preview) {
        }
    };
    Disposer.register(getProject(), panel);
    return (SliceTreeStructure) panel.getBuilder().getTreeStructure();
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) AnalysisScope(com.intellij.analysis.AnalysisScope) ToolWindowHeadlessManagerImpl(com.intellij.openapi.wm.impl.ToolWindowHeadlessManagerImpl)

Example 22 with HighlightInfo

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

the class SliceBackwardTest method doTest.

private void doTest() throws Exception {
    configureByFile("/codeInsight/slice/backward/" + getTestName(false) + ".java");
    Map<String, RangeMarker> sliceUsageName2Offset = SliceTestUtil.extractSliceOffsetsFromDocument(getEditor().getDocument());
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    PsiElement element = new SliceHandler(true).getExpressionAtCaret(getEditor(), getFile());
    assertNotNull(element);
    SliceTestUtil.calcRealOffsets(element, sliceUsageName2Offset, myFlownOffsets);
    Collection<HighlightInfo> errors = highlightErrors();
    assertEmpty(errors);
    SliceAnalysisParams params = new SliceAnalysisParams();
    params.scope = new AnalysisScope(getProject());
    params.dataFlowToThis = true;
    SliceUsage usage = LanguageSlicing.getProvider(element).createRootUsage(element, params);
    SliceTestUtil.checkUsages(usage, myFlownOffsets);
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) RangeMarker(com.intellij.openapi.editor.RangeMarker) PsiElement(com.intellij.psi.PsiElement)

Example 23 with HighlightInfo

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

the class HighlightMethodUtil method checkAmbiguousMethodCallIdentifier.

@Nullable
static HighlightInfo checkAmbiguousMethodCallIdentifier(@NotNull PsiReferenceExpression referenceToMethod, @NotNull JavaResolveResult[] resolveResults, @NotNull PsiExpressionList list, final PsiElement element, @NotNull JavaResolveResult resolveResult, @NotNull PsiMethodCallExpression methodCall, @NotNull PsiResolveHelper resolveHelper, @NotNull LanguageLevel languageLevel, @NotNull PsiFile file) {
    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);
    HighlightInfoType highlightInfoType = HighlightInfoType.ERROR;
    if (methodCandidate2 != null) {
        return null;
    }
    String description;
    PsiElement elementToHighlight;
    if (element != null && !resolveResult.isAccessible()) {
        description = HighlightUtil.buildProblemWithAccessDescription(referenceToMethod, resolveResult);
        elementToHighlight = referenceToMethod.getReferenceNameElement();
    } else if (element != null && !resolveResult.isStaticsScopeCorrect()) {
        description = null;
        elementToHighlight = ObjectUtils.notNull(referenceToMethod.getReferenceNameElement(), referenceToMethod);
        if (element instanceof PsiMethod && ((PsiMethod) element).hasModifierProperty(PsiModifier.STATIC)) {
            PsiClass containingClass = ((PsiMethod) element).getContainingClass();
            if (containingClass != null && containingClass.isInterface()) {
                HighlightInfo info = HighlightUtil.checkFeature(elementToHighlight, HighlightUtil.Feature.STATIC_INTERFACE_CALLS, languageLevel, file);
                if (info != null)
                    return info;
                description = checkStaticInterfaceMethodCallQualifier(referenceToMethod, resolveResult.getCurrentFileResolveScope(), containingClass);
            }
        }
        if (description == null) {
            description = HighlightUtil.buildProblemWithStaticDescription(element);
        }
    } else {
        String methodName = referenceToMethod.getReferenceName() + buildArgTypesList(list);
        description = JavaErrorMessages.message("cannot.resolve.method", methodName);
        if (candidates.length == 0) {
            elementToHighlight = referenceToMethod.getReferenceNameElement();
            highlightInfoType = HighlightInfoType.WRONG_REF;
        } else {
            return null;
        }
    }
    String toolTip = XmlStringUtil.escapeString(description);
    HighlightInfo info = HighlightInfo.newHighlightInfo(highlightInfoType).range(elementToHighlight).description(description).escapedToolTip(toolTip).create();
    registerMethodCallIntentions(info, methodCall, list, resolveHelper);
    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);
    WrapLongWithMathToIntExactFix.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 : 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 24 with HighlightInfo

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

the class HighlightMethodUtil method checkMethodWeakerPrivileges.

static HighlightInfo checkMethodWeakerPrivileges(@NotNull MethodSignatureBackedByPsiMethod methodSignature, @NotNull List<HierarchicalMethodSignature> superMethodSignatures, boolean includeRealPositionInfo, @NotNull PsiFile containingFile) {
    PsiMethod method = methodSignature.getMethod();
    PsiModifierList modifierList = method.getModifierList();
    if (modifierList.hasModifierProperty(PsiModifier.PUBLIC))
        return null;
    int accessLevel = PsiUtil.getAccessLevel(modifierList);
    String accessModifier = PsiUtil.getAccessModifier(accessLevel);
    for (MethodSignatureBackedByPsiMethod superMethodSignature : superMethodSignatures) {
        PsiMethod superMethod = superMethodSignature.getMethod();
        if (method.hasModifierProperty(PsiModifier.ABSTRACT) && !MethodSignatureUtil.isSuperMethod(superMethod, method))
            continue;
        if (!PsiUtil.isAccessible(containingFile.getProject(), superMethod, method, null))
            continue;
        if (!includeRealPositionInfo && MethodSignatureUtil.isSuperMethod(superMethod, method))
            continue;
        HighlightInfo info = isWeaker(method, modifierList, accessModifier, accessLevel, superMethod, includeRealPositionInfo);
        if (info != null)
            return info;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 25 with HighlightInfo

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

the class HighlightMethodUtil method checkMethodCall.

@Nullable
static HighlightInfo checkMethodCall(@NotNull PsiMethodCallExpression methodCall, @NotNull PsiResolveHelper resolveHelper, @NotNull LanguageLevel languageLevel, @NotNull JavaSdkVersion javaSdkVersion, @NotNull PsiFile file) {
    PsiExpressionList list = methodCall.getArgumentList();
    PsiReferenceExpression referenceToMethod = methodCall.getMethodExpression();
    JavaResolveResult[] results = referenceToMethod.multiResolve(true);
    JavaResolveResult resolveResult = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
    PsiElement resolved = resolveResult.getElement();
    boolean isDummy = isDummyConstructorCall(methodCall, resolveHelper, list, referenceToMethod);
    if (isDummy)
        return null;
    HighlightInfo highlightInfo;
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    if (resolved instanceof PsiMethod && resolveResult.isValidResult()) {
        TextRange fixRange = getFixRange(methodCall);
        highlightInfo = HighlightUtil.checkUnhandledExceptions(methodCall, fixRange);
        if (highlightInfo == null && ((PsiMethod) resolved).hasModifierProperty(PsiModifier.STATIC)) {
            PsiClass containingClass = ((PsiMethod) resolved).getContainingClass();
            if (containingClass != null && containingClass.isInterface()) {
                PsiReferenceExpression methodRef = methodCall.getMethodExpression();
                PsiElement element = ObjectUtils.notNull(methodRef.getReferenceNameElement(), methodRef);
                highlightInfo = HighlightUtil.checkFeature(element, HighlightUtil.Feature.STATIC_INTERFACE_CALLS, languageLevel, file);
                if (highlightInfo == null) {
                    String message = checkStaticInterfaceMethodCallQualifier(methodRef, resolveResult.getCurrentFileResolveScope(), containingClass);
                    if (message != null) {
                        highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip(message).range(fixRange).create();
                    }
                }
            }
        }
        if (highlightInfo == null) {
            highlightInfo = GenericsHighlightUtil.checkInferredIntersections(substitutor, fixRange);
        }
        if (highlightInfo == null) {
            highlightInfo = checkVarargParameterErasureToBeAccessible((MethodCandidateInfo) resolveResult, methodCall);
        }
        if (highlightInfo == null) {
            String errorMessage = ((MethodCandidateInfo) resolveResult).getInferenceErrorMessage();
            if (errorMessage != null) {
                highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip(errorMessage).range(fixRange).create();
                if (highlightInfo != null) {
                    registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
                    registerMethodReturnFixAction(highlightInfo, (MethodCandidateInfo) resolveResult, methodCall);
                }
            }
        }
    } else {
        PsiMethod resolvedMethod = null;
        MethodCandidateInfo candidateInfo = null;
        if (resolveResult instanceof MethodCandidateInfo) {
            candidateInfo = (MethodCandidateInfo) resolveResult;
            resolvedMethod = candidateInfo.getElement();
        }
        if (!resolveResult.isAccessible() || !resolveResult.isStaticsScopeCorrect()) {
            highlightInfo = null;
        } else if (candidateInfo != null && !candidateInfo.isApplicable()) {
            if (candidateInfo.isTypeArgumentsApplicable()) {
                String methodName = HighlightMessageUtil.getSymbolName(resolved, substitutor);
                PsiElement parent = resolved.getParent();
                String containerName = parent == null ? "" : HighlightMessageUtil.getSymbolName(parent, substitutor);
                String argTypes = buildArgTypesList(list);
                String description = JavaErrorMessages.message("wrong.method.arguments", methodName, containerName, argTypes);
                final Ref<PsiElement> elementToHighlight = new Ref<>(list);
                String toolTip;
                if (parent instanceof PsiClass) {
                    toolTip = buildOneLineMismatchDescription(list, candidateInfo, elementToHighlight);
                    if (toolTip == null) {
                        toolTip = createMismatchedArgumentsHtmlTooltip(candidateInfo, list);
                    }
                } else {
                    toolTip = description;
                }
                PsiElement element = elementToHighlight.get();
                // argument list starts with paren which there is no need to highlight
                int navigationShift = element instanceof PsiExpressionList ? +1 : 0;
                highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).description(description).escapedToolTip(toolTip).navigationShift(navigationShift).create();
                if (highlightInfo != null) {
                    registerMethodCallIntentions(highlightInfo, methodCall, list, resolveHelper);
                    registerMethodReturnFixAction(highlightInfo, candidateInfo, methodCall);
                }
            } else {
                PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
                PsiReferenceParameterList typeArgumentList = methodCall.getTypeArgumentList();
                PsiSubstitutor applicabilitySubstitutor = candidateInfo.getSubstitutor(false);
                if (typeArgumentList.getTypeArguments().length == 0 && resolvedMethod.hasTypeParameters()) {
                    highlightInfo = GenericsHighlightUtil.checkInferredTypeArguments(resolvedMethod, methodCall, applicabilitySubstitutor);
                } else {
                    highlightInfo = GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, methodExpression, applicabilitySubstitutor, javaSdkVersion);
                }
            }
        } else {
            String description = JavaErrorMessages.message("method.call.expected");
            highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(methodCall).descriptionAndTooltip(description).create();
            if (resolved instanceof PsiClass) {
                QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createInsertNewFix(methodCall, (PsiClass) resolved));
            } else {
                TextRange range = getFixRange(methodCall);
                QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createCreateMethodFromUsageFix(methodCall));
                QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createCreateAbstractMethodFromUsageFix(methodCall));
                QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createCreatePropertyFromUsageFix(methodCall));
                if (resolved instanceof PsiVariable && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
                    PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(((PsiVariable) resolved).getType());
                    if (method != null) {
                        QuickFixAction.registerQuickFixAction(highlightInfo, range, QUICK_FIX_FACTORY.createInsertMethodCallFix(methodCall, method));
                    }
                }
            }
        }
    }
    if (highlightInfo == null) {
        highlightInfo = GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, referenceToMethod, substitutor, javaSdkVersion);
    }
    return highlightInfo;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) TextRange(com.intellij.openapi.util.TextRange) Ref(com.intellij.openapi.util.Ref) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) Nullable(org.jetbrains.annotations.Nullable)

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