Search in sources :

Example 1 with MethodResolverProcessor

use of com.intellij.psi.scope.processor.MethodResolverProcessor 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 2 with MethodResolverProcessor

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

the class PsiReferenceExpressionImpl method resolveToMethod.

@NotNull
private JavaResolveResult[] resolveToMethod(@NotNull PsiFile containingFile) {
    final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) getParent();
    final MethodResolverProcessor processor = new MethodResolverProcessor(methodCall, containingFile);
    try {
        PsiScopesUtil.setupAndRunProcessor(processor, methodCall, false);
    } catch (MethodProcessorSetupFailedException e) {
        return JavaResolveResult.EMPTY_ARRAY;
    }
    return processor.getResult();
}
Also used : MethodResolverProcessor(com.intellij.psi.scope.processor.MethodResolverProcessor) MethodProcessorSetupFailedException(com.intellij.psi.scope.MethodProcessorSetupFailedException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MethodResolverProcessor

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

the class PsiClassImplUtil method processCachedMembersByName.

private static boolean processCachedMembersByName(@NotNull final PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable Set<PsiClass> visited, PsiElement last, @NotNull final PsiElement place, final boolean isRaw, @NotNull final PsiSubstitutor substitutor, @NotNull MembersMap value, String name, @NotNull final LanguageLevel languageLevel) {
    Function<PsiMember, PsiSubstitutor> finalSubstitutor = new Function<PsiMember, PsiSubstitutor>() {

        final ScopedClassHierarchy hierarchy = ScopedClassHierarchy.getHierarchy(aClass, place.getResolveScope());

        final PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();

        @Override
        public PsiSubstitutor fun(PsiMember member) {
            PsiClass containingClass = ObjectUtils.assertNotNull(member.getContainingClass());
            PsiSubstitutor superSubstitutor = hierarchy.getSuperMembersSubstitutor(containingClass, languageLevel);
            PsiSubstitutor finalSubstitutor = obtainFinalSubstitutor(containingClass, superSubstitutor == null ? PsiSubstitutor.EMPTY : superSubstitutor, aClass, substitutor, factory, languageLevel);
            return member instanceof PsiMethod ? checkRaw(isRaw, factory, (PsiMethod) member, finalSubstitutor) : finalSubstitutor;
        }
    };
    final ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.FIELD)) {
        final PsiField fieldByName = aClass.findFieldByName(name, false);
        if (fieldByName != null) {
            processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass);
            if (!processor.execute(fieldByName, state))
                return false;
        } else {
            final Map<String, PsiMember[]> allFieldsMap = value.get(MemberType.FIELD);
            final PsiMember[] list = allFieldsMap.get(name);
            if (list != null) {
                boolean resolved = false;
                for (final PsiMember candidateField : list) {
                    PsiClass containingClass = candidateField.getContainingClass();
                    if (containingClass == null) {
                        LOG.error("No class for field " + candidateField.getName() + " of " + candidateField.getClass());
                        continue;
                    }
                    processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, containingClass);
                    if (!processor.execute(candidateField, state.put(PsiSubstitutor.KEY, finalSubstitutor.fun(candidateField)))) {
                        resolved = true;
                    }
                }
                if (resolved)
                    return false;
            }
        }
    }
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.CLASS)) {
        if (last != null && last.getContext() == aClass) {
            if (last instanceof PsiClass) {
                if (!processor.execute(last, state))
                    return false;
            }
            // Parameters
            final PsiTypeParameterList list = aClass.getTypeParameterList();
            if (list != null && !list.processDeclarations(processor, state, last, place))
                return false;
        }
        if (!(last instanceof PsiReferenceList)) {
            final PsiClass classByName = aClass.findInnerClassByName(name, false);
            if (classByName != null) {
                processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass);
                if (!processor.execute(classByName, state))
                    return false;
            } else {
                Map<String, PsiMember[]> allClassesMap = value.get(MemberType.CLASS);
                PsiMember[] list = allClassesMap.get(name);
                if (list != null) {
                    boolean resolved = false;
                    for (final PsiMember inner : list) {
                        PsiClass containingClass = inner.getContainingClass();
                        if (containingClass != null) {
                            processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, containingClass);
                            if (!processor.execute(inner, state.put(PsiSubstitutor.KEY, finalSubstitutor.fun(inner)))) {
                                resolved = true;
                            }
                        }
                    }
                    if (resolved)
                        return false;
                }
            }
        }
    }
    if (classHint == null || classHint.shouldProcess(ElementClassHint.DeclarationKind.METHOD)) {
        if (processor instanceof MethodResolverProcessor) {
            final MethodResolverProcessor methodResolverProcessor = (MethodResolverProcessor) processor;
            if (methodResolverProcessor.isConstructor()) {
                final PsiMethod[] constructors = aClass.getConstructors();
                methodResolverProcessor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, aClass);
                for (PsiMethod constructor : constructors) {
                    if (!methodResolverProcessor.execute(constructor, state))
                        return false;
                }
                return true;
            }
        }
        Map<String, PsiMember[]> allMethodsMap = value.get(MemberType.METHOD);
        PsiMember[] list = allMethodsMap.get(name);
        if (list != null) {
            boolean resolved = false;
            for (final PsiMember candidate : list) {
                ProgressIndicatorProvider.checkCanceled();
                PsiMethod candidateMethod = (PsiMethod) candidate;
                if (processor instanceof MethodResolverProcessor) {
                    if (candidateMethod.isConstructor() != ((MethodResolverProcessor) processor).isConstructor())
                        continue;
                }
                final PsiClass containingClass = candidateMethod.getContainingClass();
                if (containingClass == null || visited != null && visited.contains(containingClass)) {
                    continue;
                }
                processor.handleEvent(PsiScopeProcessor.Event.SET_DECLARATION_HOLDER, containingClass);
                if (!processor.execute(candidateMethod, state.put(PsiSubstitutor.KEY, finalSubstitutor.fun(candidateMethod)))) {
                    resolved = true;
                }
            }
            if (resolved)
                return false;
            if (visited != null) {
                for (PsiMember aList : list) {
                    visited.add(aList.getContainingClass());
                }
            }
        }
    }
    return true;
}
Also used : ElementClassHint(com.intellij.psi.scope.ElementClassHint) MethodResolverProcessor(com.intellij.psi.scope.processor.MethodResolverProcessor)

Example 4 with MethodResolverProcessor

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

the class PsiResolveHelperImpl method multiResolveConstructor.

@Override
@NotNull
public JavaResolveResult[] multiResolveConstructor(@NotNull PsiClassType type, @NotNull PsiExpressionList argumentList, @NotNull PsiElement place) {
    PsiClassType.ClassResolveResult classResolveResult = type.resolveGenerics();
    PsiClass aClass = classResolveResult.getElement();
    if (aClass == null) {
        return JavaResolveResult.EMPTY_ARRAY;
    }
    final MethodResolverProcessor processor;
    PsiSubstitutor substitutor = classResolveResult.getSubstitutor();
    if (argumentList.getParent() instanceof PsiAnonymousClass) {
        final PsiAnonymousClass anonymous = (PsiAnonymousClass) argumentList.getParent();
        processor = new MethodResolverProcessor(anonymous, argumentList, place, place.getContainingFile());
        aClass = anonymous.getBaseClassType().resolve();
        if (aClass == null)
            return JavaResolveResult.EMPTY_ARRAY;
    } else {
        processor = new MethodResolverProcessor(aClass, argumentList, place, place.getContainingFile());
    }
    ResolveState state = ResolveState.initial().put(PsiSubstitutor.KEY, substitutor);
    for (PsiMethod constructor : aClass.getConstructors()) {
        if (!processor.execute(constructor, state))
            break;
    }
    return processor.getResult();
}
Also used : MethodResolverProcessor(com.intellij.psi.scope.processor.MethodResolverProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with MethodResolverProcessor

use of com.intellij.psi.scope.processor.MethodResolverProcessor 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)

Aggregations

MethodResolverProcessor (com.intellij.psi.scope.processor.MethodResolverProcessor)5 MethodProcessorSetupFailedException (com.intellij.psi.scope.MethodProcessorSetupFailedException)3 NotNull (org.jetbrains.annotations.NotNull)3 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 Pair (com.intellij.openapi.util.Pair)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 ElementClassHint (com.intellij.psi.scope.ElementClassHint)1 MethodCandidatesProcessor (com.intellij.psi.scope.processor.MethodCandidatesProcessor)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1