Search in sources :

Example 6 with MethodCandidateInfo

use of com.intellij.psi.infos.MethodCandidateInfo in project intellij-community by JetBrains.

the class HighlightVisitorImpl method visitLambdaExpression.

@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
    myHolder.add(checkFeature(expression, Feature.LAMBDA_EXPRESSIONS));
    final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
    if (parent instanceof PsiExpressionStatement)
        return;
    if (!myHolder.hasErrorResults() && !LambdaUtil.isValidLambdaContext(parent)) {
        myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Lambda expression not expected here").create());
    }
    PsiType functionalInterfaceType = null;
    if (!myHolder.hasErrorResults()) {
        functionalInterfaceType = expression.getFunctionalInterfaceType();
        if (functionalInterfaceType != null) {
            final String notFunctionalMessage = LambdaHighlightingUtil.checkInterfaceFunctional(functionalInterfaceType);
            if (notFunctionalMessage != null) {
                myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(notFunctionalMessage).create());
            } else {
                checkFunctionalInterfaceTypeAccessible(expression, functionalInterfaceType);
            }
        } else if (LambdaUtil.getFunctionalInterfaceType(expression, true) != null) {
            myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cannot infer functional interface type").create());
        }
    }
    if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
        String parentInferenceErrorMessage = null;
        final PsiCallExpression callExpression = parent instanceof PsiExpressionList && parent.getParent() instanceof PsiCallExpression ? (PsiCallExpression) parent.getParent() : null;
        final JavaResolveResult containingCallResolveResult = callExpression != null ? callExpression.resolveMethodGenerics() : null;
        if (containingCallResolveResult instanceof MethodCandidateInfo) {
            parentInferenceErrorMessage = ((MethodCandidateInfo) containingCallResolveResult).getParentInferenceErrorMessage((PsiExpressionList) parent);
        }
        final Map<PsiElement, String> returnErrors = LambdaUtil.checkReturnTypeCompatible(expression, LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType));
        if (parentInferenceErrorMessage != null && (returnErrors == null || !returnErrors.containsValue(parentInferenceErrorMessage))) {
            myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(parentInferenceErrorMessage).create());
        } else if (returnErrors != null) {
            for (Map.Entry<PsiElement, String> entry : returnErrors.entrySet()) {
                myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(entry.getKey()).descriptionAndTooltip(entry.getValue()).create());
            }
        }
    }
    if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
        final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
        final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
        if (interfaceMethod != null) {
            final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
            myHolder.add(LambdaHighlightingUtil.checkParametersCompatible(expression, parameters, LambdaUtil.getSubstitutor(interfaceMethod, resolveResult)));
        }
    }
    if (!myHolder.hasErrorResults()) {
        final PsiElement body = expression.getBody();
        if (body instanceof PsiCodeBlock) {
            myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement((PsiCodeBlock) body));
        }
    }
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)

Example 7 with MethodCandidateInfo

use of com.intellij.psi.infos.MethodCandidateInfo in project intellij-community by JetBrains.

the class PsiDiamondTypeImpl method getStaticFactoryCandidateInfo.

private static JavaResolveResult getStaticFactoryCandidateInfo(final PsiNewExpression newExpression, final PsiElement context) {
    return ourDiamondGuard.doPreventingRecursion(context, false, () -> {
        final PsiExpressionList argumentList = newExpression.getArgumentList();
        if (argumentList == null) {
            //token expected diagnostic is provided by parser
            return null;
        }
        final JavaMethodsConflictResolver resolver = new JavaMethodsConflictResolver(argumentList, PsiUtil.getLanguageLevel(newExpression));
        final JavaResolveResult[] result = collectStaticFactories(newExpression, resolver);
        final PsiMethod staticFactory = result != null && result.length == 1 ? (PsiMethod) result[0].getElement() : null;
        if (staticFactory == null) {
            //additional diagnostics: inference fails due to unresolved constructor
            return JavaResolveResult.EMPTY;
        }
        final MethodCandidateInfo staticFactoryCandidateInfo = createMethodCandidate(staticFactory, context, false, argumentList);
        if (!staticFactory.isVarArgs()) {
            return staticFactoryCandidateInfo;
        }
        final ArrayList<CandidateInfo> conflicts = new ArrayList<>();
        conflicts.add(staticFactoryCandidateInfo);
        conflicts.add(createMethodCandidate(staticFactory, context, true, argumentList));
        return resolver.resolveConflict(conflicts);
    });
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) JavaMethodsConflictResolver(com.intellij.psi.scope.conflictResolvers.JavaMethodsConflictResolver)

Example 8 with MethodCandidateInfo

use of com.intellij.psi.infos.MethodCandidateInfo in project intellij-community by JetBrains.

the class ExceptionUtil method getUnhandledExceptions.

@NotNull
public static List<PsiClassType> getUnhandledExceptions(@NotNull final PsiCallExpression methodCall, @Nullable final PsiElement topElement, final boolean includeSelfCalls) {
    //exceptions only influence the invocation type after overload resolution is complete
    if (MethodCandidateInfo.isOverloadCheck()) {
        return Collections.emptyList();
    }
    final MethodCandidateInfo.CurrentCandidateProperties properties = MethodCandidateInfo.getCurrentMethod(methodCall.getArgumentList());
    final JavaResolveResult result = properties != null ? properties.getInfo() : InferenceSession.getResolveResult(methodCall);
    final PsiElement element = result.getElement();
    final PsiMethod method = element instanceof PsiMethod ? (PsiMethod) element : null;
    if (method == null) {
        return Collections.emptyList();
    }
    final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
    if (!includeSelfCalls && method == containingMethod) {
        return Collections.emptyList();
    }
    if (properties != null) {
        PsiUtilCore.ensureValid(method);
    }
    final PsiClassType[] thrownExceptions = method.getThrowsList().getReferencedTypes();
    if (thrownExceptions.length == 0) {
        return Collections.emptyList();
    }
    final PsiSubstitutor substitutor = result.getSubstitutor();
    if (!isArrayClone(method, methodCall) && methodCall instanceof PsiMethodCallExpression) {
        final PsiFile containingFile = (containingMethod == null ? methodCall : containingMethod).getContainingFile();
        final MethodResolverProcessor processor = new MethodResolverProcessor((PsiMethodCallExpression) methodCall, containingFile);
        try {
            PsiScopesUtil.setupAndRunProcessor(processor, methodCall, false);
            final List<Pair<PsiMethod, PsiSubstitutor>> candidates = ContainerUtil.mapNotNull(processor.getResults(), info -> {
                PsiElement element1 = info.getElement();
                if (info instanceof MethodCandidateInfo && MethodSignatureUtil.areSignaturesEqual(method, (PsiMethod) element1) && !MethodSignatureUtil.isSuperMethod((PsiMethod) element1, method)) {
                    return Pair.create((PsiMethod) element1, ((MethodCandidateInfo) info).getSubstitutor(false));
                }
                return null;
            });
            if (candidates.size() > 1) {
                GlobalSearchScope scope = methodCall.getResolveScope();
                final List<PsiClassType> ex = collectSubstituted(substitutor, thrownExceptions, scope);
                for (Pair<PsiMethod, PsiSubstitutor> pair : candidates) {
                    final PsiClassType[] exceptions = pair.first.getThrowsList().getReferencedTypes();
                    if (exceptions.length == 0) {
                        return getUnhandledExceptions(methodCall, topElement, PsiSubstitutor.EMPTY, PsiClassType.EMPTY_ARRAY);
                    }
                    retainExceptions(ex, collectSubstituted(pair.second, exceptions, scope));
                }
                return getUnhandledExceptions(methodCall, topElement, PsiSubstitutor.EMPTY, ex.toArray(new PsiClassType[ex.size()]));
            }
        } catch (MethodProcessorSetupFailedException ignore) {
            return Collections.emptyList();
        }
    }
    return getUnhandledExceptions(method, methodCall, topElement, substitutor);
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) MethodResolverProcessor(com.intellij.psi.scope.processor.MethodResolverProcessor) MethodProcessorSetupFailedException(com.intellij.psi.scope.MethodProcessorSetupFailedException) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with MethodCandidateInfo

use of com.intellij.psi.infos.MethodCandidateInfo in project intellij-community by JetBrains.

the class ResolveMethod15Test method testGenericsAndVarargsNoConflict.

public void testGenericsAndVarargsNoConflict() throws Exception {
    final PsiReference ref = configureByFile();
    final PsiElement element = ref.resolve();
    assertNotNull(element);
    assertThat(element, instanceOf(PsiMethod.class));
    final PsiMethod method = (PsiMethod) element;
    assertEquals("method", method.getName());
    assertEquals(method.getTypeParameters().length, 0);
    assertThat(ref, instanceOf(PsiReferenceExpression.class));
    final PsiReferenceExpression refExpr = (PsiReferenceExpression) ref;
    final JavaResolveResult[] resolveResults = refExpr.multiResolve(false);
    assertEquals(1, resolveResults.length);
    final JavaResolveResult resolveResult = resolveResults[0];
    assertTrue(resolveResult.isValidResult());
    assertThat(resolveResult, instanceOf(MethodCandidateInfo.class));
    final MethodCandidateInfo methodCandidateInfo = (MethodCandidateInfo) resolveResult;
    assertTrue(methodCandidateInfo.isApplicable());
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Example 10 with MethodCandidateInfo

use of com.intellij.psi.infos.MethodCandidateInfo in project intellij-community by JetBrains.

the class ResolveMethod15Test method testStaticImportConflict.

public void testStaticImportConflict() throws Exception {
    final PsiReference ref = configureByFile();
    final PsiElement element = ref.resolve();
    assertNotNull(element);
    assertThat(element, instanceOf(PsiMethod.class));
    final PsiMethod method = (PsiMethod) element;
    assertEquals("sort", method.getName());
    assertEquals("java.util.Collections", method.getContainingClass().getQualifiedName());
    assertThat(ref, instanceOf(PsiReferenceExpression.class));
    final PsiReferenceExpression refExpr = (PsiReferenceExpression) ref;
    final JavaResolveResult[] resolveResults = refExpr.multiResolve(false);
    assertEquals(1, resolveResults.length);
    final JavaResolveResult resolveResult = resolveResults[0];
    assertFalse(resolveResult.isValidResult());
    assertThat(resolveResult.getCurrentFileResolveScope(), instanceOf(PsiImportStaticStatement.class));
    assertThat(resolveResult, instanceOf(MethodCandidateInfo.class));
    final MethodCandidateInfo methodCandidateInfo = (MethodCandidateInfo) resolveResult;
    assertFalse(methodCandidateInfo.isApplicable());
}
Also used : MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo)

Aggregations

MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)27 CandidateInfo (com.intellij.psi.infos.CandidateInfo)12 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)4 TextRange (com.intellij.openapi.util.TextRange)4 MethodParameterInfoHandler (com.intellij.codeInsight.hint.api.impls.MethodParameterInfoHandler)3 CreateParameterInfoContext (com.intellij.lang.parameterInfo.CreateParameterInfoContext)3 MockCreateParameterInfoContext (com.intellij.testFramework.utils.parameterInfo.MockCreateParameterInfoContext)3 Nullable (org.jetbrains.annotations.Nullable)3 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)2 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)2 ParameterInfoUIContextEx (com.intellij.lang.parameterInfo.ParameterInfoUIContextEx)2 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)2 NotNull (org.jetbrains.annotations.NotNull)2 TextAttributesScheme (com.intellij.openapi.editor.colors.TextAttributesScheme)1 Pair (com.intellij.openapi.util.Pair)1 Ref (com.intellij.openapi.util.Ref)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 JavaStubPsiElement (com.intellij.psi.impl.source.JavaStubPsiElement)1 ParameterTypeInferencePolicy (com.intellij.psi.impl.source.resolve.ParameterTypeInferencePolicy)1