public static void addElementUsages( final PsiElement element, final Processor<UsageInfo> result, final FindUsagesOptions options) { final SearchScope searchScope = options.searchScope; if (element instanceof PsiMethod && ApplicationManager.getApplication() .runReadAction( new Computable<Boolean>() { @Override public Boolean compute() { return ((PsiMethod) element).isConstructor(); } })) { PsiMethod method = (PsiMethod) element; final PsiClass parentClass = method.getContainingClass(); if (parentClass != null) { MethodReferencesSearch.search( new MethodReferencesSearch.SearchParameters( method, searchScope, options instanceof JavaMethodFindUsagesOptions ? !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages : true, options.fastTrack)) .forEach( new ReadActionProcessor<PsiReference>() { @Override public boolean processInReadAction(final PsiReference ref) { return addResult(result, ref, options); } }); } return; } final ReadActionProcessor<PsiReference> consumer = new ReadActionProcessor<PsiReference>() { @Override public boolean processInReadAction(final PsiReference ref) { return addResult(result, ref, options); } }; if (element instanceof PsiMethod) { final boolean strictSignatureSearch = !(options instanceof JavaMethodFindUsagesOptions) || // field with getter !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages; MethodReferencesSearch.search( new MethodReferencesSearch.SearchParameters( (PsiMethod) element, searchScope, strictSignatureSearch, options.fastTrack)) .forEach(consumer); } else { ReferencesSearch.search( new ReferencesSearch.SearchParameters(element, searchScope, false, options.fastTrack)) .forEach(consumer); } }
private static PsiElement[] getParameterElementsToSearch(final PsiParameter parameter) { final PsiMethod method = (PsiMethod) parameter.getDeclarationScope(); PsiMethod[] overrides = OverridingMethodsSearch.search(method, true).toArray(PsiMethod.EMPTY_ARRAY); for (int i = 0; i < overrides.length; i++) { overrides[i] = (PsiMethod) overrides[i].getNavigationElement(); } List<PsiElement> elementsToSearch = new ArrayList<PsiElement>(overrides.length + 1); elementsToSearch.add(parameter); int idx = method.getParameterList().getParameterIndex(parameter); for (PsiMethod override : overrides) { final PsiParameter[] parameters = override.getParameterList().getParameters(); if (idx < parameters.length) { elementsToSearch.add(parameters[idx]); } } return elementsToSearch.toArray(new PsiElement[elementsToSearch.size()]); }
private static void addMethodsUsages( final PsiClass aClass, final Processor<UsageInfo> results, final JavaClassFindUsagesOptions options) { if (options.isIncludeInherited) { final PsiManager manager = aClass.getManager(); PsiMethod[] methods = aClass.getAllMethods(); MethodsLoop: for (int i = 0; i < methods.length; i++) { final PsiMethod method = methods[i]; // filter overriden methods MethodSignature methodSignature = method.getSignature(PsiSubstitutor.EMPTY); for (int j = 0; j < i; j++) { if (methodSignature.equals(methods[j].getSignature(PsiSubstitutor.EMPTY))) continue MethodsLoop; } final PsiClass methodClass = method.getContainingClass(); if (methodClass != null && manager.areElementsEquivalent(methodClass, aClass)) { addElementUsages(methods[i], results, options); } else { MethodReferencesSearch.search( new MethodReferencesSearch.SearchParameters( method, options.searchScope, true, options.fastTrack)) .forEach( new PsiReferenceProcessorAdapter( new PsiReferenceProcessor() { @Override public boolean execute(PsiReference reference) { addResultFromReference( reference, methodClass, manager, aClass, results, options); return true; } })); } } } else { for (PsiMethod method : aClass.getMethods()) { addElementUsages(method, results, options); } } }
@Override @NotNull public PsiElement[] getPrimaryElements() { final PsiElement element = getPsiElement(); if (element instanceof PsiParameter) { final PsiParameter parameter = (PsiParameter) element; final PsiElement scope = parameter.getDeclarationScope(); if (scope instanceof PsiMethod) { final PsiMethod method = (PsiMethod) scope; if (PsiUtil.canBeOverriden(method)) { final PsiClass aClass = method.getContainingClass(); LOG.assertTrue(aClass != null); // Otherwise can not be overriden boolean hasOverridden = OverridingMethodsSearch.search(method).findFirst() != null; if (hasOverridden && askWhetherShouldSearchForParameterInOverridingMethods(element, parameter)) { return getParameterElementsToSearch(parameter); } } } } return myElementsToSearch.length == 0 ? new PsiElement[] {element} : myElementsToSearch; }