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);
}
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();
}
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;
}
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();
}
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()]);
}
Aggregations