Search in sources :

Example 1 with MethodCandidatesProcessor

use of com.intellij.psi.scope.processor.MethodCandidatesProcessor in project intellij-community by JetBrains.

the class PsiResolveHelperImpl method getReferencedMethodCandidates.

@NotNull
@Override
public CandidateInfo[] getReferencedMethodCandidates(@NotNull PsiCallExpression expr, boolean dummyImplicitConstructor, final boolean checkVarargs) {
    PsiFile containingFile = expr.getContainingFile();
    final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(expr, containingFile) {

        @Override
        protected boolean acceptVarargs() {
            return checkVarargs;
        }
    };
    try {
        PsiScopesUtil.setupAndRunProcessor(processor, expr, dummyImplicitConstructor);
    } catch (MethodProcessorSetupFailedException e) {
        return CandidateInfo.EMPTY_ARRAY;
    }
    return processor.getCandidates();
}
Also used : MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) MethodProcessorSetupFailedException(com.intellij.psi.scope.MethodProcessorSetupFailedException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MethodCandidatesProcessor

use of com.intellij.psi.scope.processor.MethodCandidatesProcessor in project intellij-community by JetBrains.

the class ProcessCandidateParameterTypeInferencePolicy method getResults.

@NotNull
protected JavaResolveResult[] getResults(@NotNull PsiCallExpression contextCall, final int exprIdx) throws MethodProcessorSetupFailedException {
    PsiFile containingFile = contextCall.getContainingFile();
    final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(contextCall, containingFile);
    //can't call resolve() since it obtains full substitution, that may result in infinite recursion
    PsiScopesUtil.setupAndRunProcessor(processor, contextCall, false);
    return processor.getResult();
}
Also used : MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MethodCandidatesProcessor

use of com.intellij.psi.scope.processor.MethodCandidatesProcessor in project intellij-community by JetBrains.

the class MethodReferenceResolver method resolve.

@NotNull
@Override
public JavaResolveResult[] resolve(@NotNull final PsiMethodReferenceExpressionImpl reference, @NotNull final PsiFile containingFile, boolean incompleteCode) {
    final PsiMethodReferenceUtil.QualifierResolveResult qualifierResolveResult = PsiMethodReferenceUtil.getQualifierResolveResult(reference);
    final PsiClass containingClass = qualifierResolveResult.getContainingClass();
    PsiSubstitutor substitutor = qualifierResolveResult.getSubstitutor();
    if (containingClass != null) {
        final PsiElement element = reference.getReferenceNameElement();
        final boolean isConstructor = reference.isConstructor();
        if (element instanceof PsiIdentifier || isConstructor) {
            if (isConstructor && !canBeConstructed(containingClass)) {
                return JavaResolveResult.EMPTY_ARRAY;
            }
            final PsiType functionalInterfaceType = getInterfaceType(reference);
            final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
            final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
            final PsiSubstitutor functionalInterfaceSubstitutor = interfaceMethod != null ? LambdaUtil.getSubstitutor(interfaceMethod, resolveResult) : null;
            final MethodSignature signature = interfaceMethod != null ? interfaceMethod.getSignature(functionalInterfaceSubstitutor) : null;
            final PsiType interfaceMethodReturnType = LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType);
            if (isConstructor && containingClass.getConstructors().length == 0) {
                if (interfaceMethod != null) {
                    final PsiClassType returnType = composeReturnType(containingClass, substitutor);
                    final InferenceSession session = new InferenceSession(containingClass.getTypeParameters(), substitutor, reference.getManager(), null);
                    if (!(session.isProperType(session.substituteWithInferenceVariables(returnType)) && session.isProperType(interfaceMethodReturnType))) {
                        session.registerReturnTypeConstraints(returnType, interfaceMethodReturnType);
                        substitutor = session.infer();
                    }
                }
                ClassCandidateInfo candidateInfo = null;
                final boolean isArray = PsiEquivalenceUtil.areElementsEquivalent(containingClass, JavaPsiFacade.getElementFactory(reference.getProject()).getArrayClass(PsiUtil.getLanguageLevel(reference)));
                if (signature == null || !isArray && (containingClass.getContainingClass() == null || !isLocatedInStaticContext(containingClass, reference)) && signature.getParameterTypes().length == 0 || isArray && arrayCreationSignature(signature)) {
                    candidateInfo = new ClassCandidateInfo(containingClass, substitutor);
                }
                return candidateInfo == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { candidateInfo };
            }
            final PsiConflictResolver conflictResolver = createResolver(reference, qualifierResolveResult, interfaceMethod, signature);
            final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(reference, containingFile, new PsiConflictResolver[] { conflictResolver }, new SmartList<>()) {

                @Override
                protected boolean acceptVarargs() {
                    return true;
                }

                @Override
                protected MethodCandidateInfo createCandidateInfo(@NotNull final PsiMethod method, @NotNull final PsiSubstitutor substitutor, final boolean staticProblem, final boolean accessible, final boolean varargs) {
                    final PsiExpressionList argumentList = getArgumentList();
                    final PsiType[] typeParameters = reference.getTypeParameters();
                    return new MethodCandidateInfo(method, substitutor, !accessible, staticProblem, argumentList, myCurrentFileContext, argumentList != null ? argumentList.getExpressionTypes() : null, method.hasTypeParameters() && typeParameters.length > 0 ? typeParameters : null, getLanguageLevel()) {

                        @Override
                        public boolean isVarargs() {
                            return varargs;
                        }

                        @NotNull
                        @Override
                        public PsiSubstitutor inferTypeArguments(@NotNull ParameterTypeInferencePolicy policy, boolean includeReturnConstraint) {
                            return inferTypeArguments(includeReturnConstraint);
                        }

                        private PsiSubstitutor inferTypeArguments(boolean includeReturnConstraint) {
                            if (interfaceMethod == null)
                                return substitutor;
                            final InferenceSession session = new InferenceSession(method.getTypeParameters(), substitutor, reference.getManager(), reference);
                            session.initThrowsConstraints(method);
                            final PsiSubstitutor psiSubstitutor = session.collectApplicabilityConstraints(reference, this, functionalInterfaceType);
                            if (psiSubstitutor != null) {
                                return psiSubstitutor;
                            }
                            if (!session.repeatInferencePhases()) {
                                return substitutor;
                            }
                            if (includeReturnConstraint && !PsiType.VOID.equals(interfaceMethodReturnType) && interfaceMethodReturnType != null) {
                                PsiSubstitutor subst = PsiMethodReferenceCompatibilityConstraint.getSubstitutor(signature, qualifierResolveResult, method, containingClass, reference);
                                final PsiType returnType = method.isConstructor() ? composeReturnType(containingClass, subst) : subst.substitute(method.getReturnType());
                                if (returnType != null) {
                                    session.registerReturnTypeConstraints(returnType, interfaceMethodReturnType);
                                }
                            }
                            return session.infer(method.getParameterList().getParameters(), null, null);
                        }

                        @Override
                        public boolean isApplicable() {
                            if (signature == null)
                                return false;
                            final PsiType[] argTypes = signature.getParameterTypes();
                            boolean hasReceiver = PsiMethodReferenceUtil.isSecondSearchPossible(argTypes, qualifierResolveResult, reference);
                            return MethodReferenceConflictResolver.isApplicableByFirstSearch(this, argTypes, hasReceiver, reference, interfaceMethod.isVarArgs()) != null;
                        }
                    };
                }
            };
            processor.setIsConstructor(isConstructor);
            processor.setName(isConstructor ? containingClass.getName() : element.getText());
            final PsiExpression expression = reference.getQualifierExpression();
            if (expression == null || !(expression.getType() instanceof PsiArrayType)) {
                processor.setAccessClass(containingClass);
            }
            if (qualifierResolveResult.isReferenceTypeQualified() && isLocatedInStaticContext(containingClass, reference)) {
                processor.handleEvent(JavaScopeProcessorEvent.START_STATIC, null);
            }
            ResolveState state = ResolveState.initial().put(PsiSubstitutor.KEY, substitutor);
            containingClass.processDeclarations(processor, state, reference, reference);
            return processor.getResult();
        }
    }
    return JavaResolveResult.EMPTY_ARRAY;
}
Also used : NotNull(org.jetbrains.annotations.NotNull) PsiConflictResolver(com.intellij.psi.scope.PsiConflictResolver) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) ClassCandidateInfo(com.intellij.psi.infos.ClassCandidateInfo) MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) MethodSignature(com.intellij.psi.util.MethodSignature) InferenceSession(com.intellij.psi.impl.source.resolve.graphInference.InferenceSession) ParameterTypeInferencePolicy(com.intellij.psi.impl.source.resolve.ParameterTypeInferencePolicy) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with MethodCandidatesProcessor

use of com.intellij.psi.scope.processor.MethodCandidatesProcessor in project intellij-community by JetBrains.

the class MethodParameterInfoHandler method getCandidates.

private static CandidateInfo[] getCandidates(PsiCallExpression call) {
    final MethodCandidatesProcessor processor = new MethodResolverProcessor(call, call.getContainingFile(), new PsiConflictResolver[0]) {

        @Override
        protected boolean acceptVarargs() {
            return false;
        }
    };
    try {
        PsiScopesUtil.setupAndRunProcessor(processor, call, true);
    } catch (MethodProcessorSetupFailedException e) {
        return CandidateInfo.EMPTY_ARRAY;
    }
    final List<CandidateInfo> results = processor.getResults();
    return results.toArray(new CandidateInfo[results.size()]);
}
Also used : MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) MethodResolverProcessor(com.intellij.psi.scope.processor.MethodResolverProcessor) MethodProcessorSetupFailedException(com.intellij.psi.scope.MethodProcessorSetupFailedException)

Example 5 with MethodCandidatesProcessor

use of com.intellij.psi.scope.processor.MethodCandidatesProcessor in project intellij-community by JetBrains.

the class PsiDiamondTypeImpl method collectStaticFactories.

@Nullable
public static JavaResolveResult[] collectStaticFactories(PsiNewExpression newExpression, final PsiConflictResolver... conflictResolvers) {
    PsiExpressionList argumentList = newExpression.getArgumentList();
    if (argumentList == null) {
        return null;
    }
    final PsiClass psiClass = findClass(newExpression);
    if (psiClass == null) {
        //should not happens: unresolved class reference would be first and inference won't start
        return null;
    }
    final List<CandidateInfo> candidates = new ArrayList<>();
    PsiMethod[] constructors = psiClass.getConstructors();
    if (constructors.length == 0) {
        //default constructor
        constructors = new PsiMethod[] { null };
    }
    final MethodCandidatesProcessor processor = new MethodCandidatesProcessor(argumentList, argumentList.getContainingFile(), conflictResolvers, candidates) {

        @Override
        protected boolean isAccepted(PsiMethod candidate) {
            return true;
        }

        @Override
        protected PsiClass getContainingClass(PsiMethod method) {
            return psiClass;
        }

        @Override
        protected boolean acceptVarargs() {
            return true;
        }
    };
    processor.setArgumentList(argumentList);
    for (PsiMethod constructor : constructors) {
        final PsiTypeParameter[] params = getAllTypeParams(constructor, psiClass);
        final PsiMethod staticFactory = generateStaticFactory(constructor, psiClass, params, newExpression.getClassReference());
        if (staticFactory != null) {
            processor.add(staticFactory, PsiSubstitutor.EMPTY);
        }
    }
    return processor.getResult();
}
Also used : MethodCandidatesProcessor(com.intellij.psi.scope.processor.MethodCandidatesProcessor) CandidateInfo(com.intellij.psi.infos.CandidateInfo) MethodCandidateInfo(com.intellij.psi.infos.MethodCandidateInfo) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

MethodCandidatesProcessor (com.intellij.psi.scope.processor.MethodCandidatesProcessor)5 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)3 NotNull (org.jetbrains.annotations.NotNull)3 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 MethodProcessorSetupFailedException (com.intellij.psi.scope.MethodProcessorSetupFailedException)2 ParameterTypeInferencePolicy (com.intellij.psi.impl.source.resolve.ParameterTypeInferencePolicy)1 InferenceSession (com.intellij.psi.impl.source.resolve.graphInference.InferenceSession)1 ClassCandidateInfo (com.intellij.psi.infos.ClassCandidateInfo)1 PsiConflictResolver (com.intellij.psi.scope.PsiConflictResolver)1 MethodResolverProcessor (com.intellij.psi.scope.processor.MethodResolverProcessor)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 Nullable (org.jetbrains.annotations.Nullable)1