Search in sources :

Example 1 with ParameterTypeInferencePolicy

use of com.intellij.psi.impl.source.resolve.ParameterTypeInferencePolicy 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)

Aggregations

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 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)1 PsiConflictResolver (com.intellij.psi.scope.PsiConflictResolver)1 MethodCandidatesProcessor (com.intellij.psi.scope.processor.MethodCandidatesProcessor)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 NotNull (org.jetbrains.annotations.NotNull)1