Java 类com.intellij.psi.search.searches.MethodReferencesSearch 实例源码

项目:intellij-ce-playground    文件:StringExpressionHelper.java   
@NotNull
public static Set<PsiMethodCallExpression> searchMethodCalls(@NotNull final PsiMethod psiMethod, @NotNull SearchScope searchScope) {
  final Set<PsiMethodCallExpression> callExpressions = new com.intellij.util.containers.HashSet<PsiMethodCallExpression>();
  final CommonProcessors.CollectUniquesProcessor<PsiReference> consumer = new CommonProcessors.CollectUniquesProcessor<PsiReference>();

  MethodReferencesSearch.search(psiMethod, searchScope, true).forEach(consumer);

  for (PsiReference psiReference : consumer.getResults()) {
    final PsiMethodCallExpression methodCallExpression =
      PsiTreeUtil.getParentOfType(psiReference.getElement(), PsiMethodCallExpression.class);

    if (methodCallExpression != null) {
      callExpressions.add(methodCallExpression);
    }
  }


  return callExpressions;
}
项目:intellij-ce-playground    文件:JavaFindUsagesHandler.java   
@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull final PsiElement target, @NotNull final SearchScope searchScope) {
  if (target instanceof PsiMethod) {
    final PsiMethod[] superMethods = ((PsiMethod)target).findDeepestSuperMethods();
    if (superMethods.length == 0) {
      return MethodReferencesSearch.search((PsiMethod)target, searchScope, true).findAll();
    }
    final Collection<PsiReference> result = new ArrayList<PsiReference>();
    for (PsiMethod superMethod : superMethods) {
      result.addAll(MethodReferencesSearch.search(superMethod, searchScope, true).findAll());
    }
    return result;
  }
  return super.findReferencesToHighlight(target, searchScope);
}
项目:intellij-ce-playground    文件:JavaInvertBooleanDelegate.java   
public void collectRefsToInvert(PsiElement namedElement, Collection<PsiElement> elementsToInvert) {
  final Query<PsiReference> query = namedElement instanceof PsiMethod ?
                                    MethodReferencesSearch.search((PsiMethod)namedElement) :
                                    ReferencesSearch.search(namedElement);
  final Collection<PsiReference> refs = query.findAll();

  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    PsiElement refElement = getElementToInvert(namedElement, element);
    if (refElement == null) {
      refElement = getForeignElementToInvert(namedElement, element, JavaLanguage.INSTANCE);
    }
    if (refElement != null) {
      elementsToInvert.add(refElement);
    }
  }
}
项目:intellij-ce-playground    文件:JavaMethodNode.java   
@Override
protected List<PsiMethod> computeCallers() {
  final PsiReference[] refs =
    MethodReferencesSearch.search(myMethod, GlobalSearchScope.allScope(myProject), true).toArray(PsiReference.EMPTY_ARRAY);

  List<PsiMethod> result = new ArrayList<PsiMethod>();
  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    if (!(element instanceof PsiReferenceExpression) ||
        !(((PsiReferenceExpression)element).getQualifierExpression() instanceof PsiSuperExpression)) {
      final PsiElement enclosingContext = PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiClass.class);
      if (enclosingContext instanceof PsiMethod && !result.contains(enclosingContext) &&
          !myMethod.equals(enclosingContext) && !myCalled.contains(myMethod)) { //do not add recursive methods
        result.add((PsiMethod)enclosingContext);
      }
      else if (element instanceof PsiClass) {
        final PsiClass aClass = (PsiClass)element;
        final PsiMethod method = JavaPsiFacade.getElementFactory(myProject).createMethodFromText(aClass.getName() + "(){}", aClass);
        if (!result.contains(method)) {
          result.add(method);
        }
      }
    }
  }
  return result;
}
项目:intellij-ce-playground    文件:FindUsagesTest.java   
public void testOverloadConstructors() throws Exception {
  PsiClass aClass = myJavaFacade.findClass("B", GlobalSearchScope.allScope(myProject));
  PsiMethod constructor = aClass.findMethodsByName("B", false)[0];
  PsiMethodCallExpression superCall = (PsiMethodCallExpression) constructor.getBody().getStatements()[0].getFirstChild();
  PsiReferenceExpression superExpr = superCall.getMethodExpression();
  String[] fileNames = {"B.java", "A.java", "A.java", "B.java"};
  int[] starts = {};
  int[] ends = {};
  final ArrayList<PsiFile> filesList = new ArrayList<PsiFile>();
  final IntArrayList startsList = new IntArrayList();
  final IntArrayList endsList = new IntArrayList();
  PsiReference[] refs =
    MethodReferencesSearch.search((PsiMethod)superExpr.resolve(), GlobalSearchScope.projectScope(myProject), false).toArray(PsiReference.EMPTY_ARRAY);
  for (PsiReference ref : refs) {
    addReference(ref, filesList, startsList, endsList);
  }
  checkResult(fileNames, filesList, starts, startsList, ends, endsList);
}
项目:intellij-ce-playground    文件:MultipleExceptionsDeclaredOnTestMethodInspection.java   
@Override
public void visitMethod(PsiMethod method) {
  super.visitMethod(method);
  if (!TestUtils.isJUnitTestMethod(method)) {
    return;
  }
  final PsiReferenceList throwsList = method.getThrowsList();
  final PsiJavaCodeReferenceElement[] referenceElements =
    throwsList.getReferenceElements();
  if (referenceElements.length < 2) {
    return;
  }

  final Query<PsiReference> query =
    MethodReferencesSearch.search(method);
  final PsiReference firstReference = query.findFirst();
  if (firstReference != null) {
    return;
  }
  registerError(throwsList);
}
项目:intellij-ce-playground    文件:StaticMethodOnlyUsedInOneClassInspectionBase.java   
/**
 * @return the class the specified method is used from, or null if it is
 *         used from 0 or more than 1 other classes.
 */
@Nullable
public PsiClass getUsageClass(final PsiMethod method) {
  final ProgressManager progressManager = ProgressManager.getInstance();
  final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject());
  final String name = method.getName();
  final GlobalSearchScope scope = GlobalSearchScope.allScope(method.getProject());
  if (searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator())
      == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
    return null;
  }
  progressManager.runProcess(new Runnable() {
    @Override
    public void run() {
      final Query<PsiReference> query = MethodReferencesSearch.search(method);
      if (!query.forEach(UsageProcessor.this)) {
        foundClass.set(null);
      }
    }
  }, null);
  return foundClass.get();
}
项目:intellij-ce-playground    文件:GrLiteralMethodSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters p, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = p.getMethod();
  final PsiClass aClass = method.getContainingClass();
  if (aClass == null) return;

  final String name = method.getName();
  if (StringUtil.isEmpty(name)) return;

  final boolean strictSignatureSearch = p.isStrictSignatureSearch();
  final PsiMethod[] methods = strictSignatureSearch ? new PsiMethod[]{method} : aClass.findMethodsByName(name, false);

  SearchScope accessScope = GroovyScopeUtil.getEffectiveScope(methods);
  final SearchScope restrictedByAccess = GroovyScopeUtil.restrictScopeToGroovyFiles(p.getEffectiveSearchScope(), accessScope);

  final String textToSearch = findLongestWord(name);

  p.getOptimizer().searchWord(textToSearch, restrictedByAccess, UsageSearchContext.IN_STRINGS, true, method,
                              new MethodTextOccurrenceProcessor(aClass, strictSignatureSearch, methods));
}
项目:intellij-ce-playground    文件:AccessorReferencesSearcher.java   
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiElement element = queryParameters.getElementToSearch();
  if (element instanceof PsiMethod) {
    final String propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod)element);
    if (propertyName == null) return;

    queryParameters.getOptimizer().searchWord(propertyName, GroovyScopeUtil
      .restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope()),
                                              UsageSearchContext.IN_CODE, true, element);
  }
  else if (element instanceof GrField) {
    for (GrAccessorMethod method : ((GrField)element).getGetters()) {
      MethodReferencesSearch.search(method, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
    }

    final GrAccessorMethod setter = ((GrField)element).getSetter();
    if (setter != null) {
      MethodReferencesSearch.search(setter, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
    }
  }
}
项目:intellij-ce-playground    文件:AccessorMethodReferencesSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();

  final String propertyName;
  if (GdkMethodUtil.isCategoryMethod(method, null, null, PsiSubstitutor.EMPTY)) {
    final GrGdkMethod cat = GrGdkMethodImpl.createGdkMethod(method, false, null);
    propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod)cat);
  }
  else {
    propertyName = GroovyPropertyUtils.getPropertyName(method);
  }

  if (propertyName == null) return;

  final SearchScope onlyGroovyFiles = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope(), GroovyScopeUtil.getEffectiveScope(method));

  queryParameters.getOptimizer().searchWord(propertyName, onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);

  if (!GroovyPropertyUtils.isPropertyName(propertyName)) {
    queryParameters.getOptimizer().searchWord(StringUtil.decapitalize(propertyName), onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
  }
}
项目:tools-idea    文件:JavaMethodNode.java   
@Override
protected List<PsiMethod> computeCallers() {
  final PsiReference[] refs =
    MethodReferencesSearch.search(myMethod, GlobalSearchScope.allScope(myProject), true).toArray(PsiReference.EMPTY_ARRAY);

  List<PsiMethod> result = new ArrayList<PsiMethod>();
  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    if (!(element instanceof PsiReferenceExpression) ||
        !(((PsiReferenceExpression)element).getQualifierExpression() instanceof PsiSuperExpression)) {
      final PsiElement enclosingContext = PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiClass.class);
      if (enclosingContext instanceof PsiMethod &&
          !myMethod.equals(enclosingContext) && !myCalled.contains(myMethod)) { //do not add recursive methods
        result.add((PsiMethod)enclosingContext);
      }
      else if (element instanceof PsiClass) {
        final PsiClass aClass = (PsiClass)element;
        result.add(JavaPsiFacade.getElementFactory(myProject).createMethodFromText(aClass.getName() + "(){}", aClass));
      }
    }
  }
  return result;
}
项目:tools-idea    文件:FindUsagesTest.java   
public void testOverloadConstructors() throws Exception {
    PsiClass aClass = myJavaFacade.findClass("B", GlobalSearchScope.allScope(myProject));
    PsiMethod constructor;
//    constructor = myJavaFacade.getElementFactory().createConstructor();
//    constructor = aClass.findMethodBySignature(constructor, false);
    constructor = aClass.findMethodsByName("B", false)[0];
    PsiMethodCallExpression superCall = (PsiMethodCallExpression) constructor.getBody().getStatements()[0].getFirstChild();
    PsiReferenceExpression superExpr = superCall.getMethodExpression();
    String[] fileNames = new String[]{"B.java", "A.java", "A.java", "B.java"};
    int[] starts = new int[]{};
    int[] ends = new int[]{};
    final ArrayList<PsiFile> filesList = new ArrayList<PsiFile>();
    final IntArrayList startsList = new IntArrayList();
    final IntArrayList endsList = new IntArrayList();
    PsiReference[] refs =
      MethodReferencesSearch.search((PsiMethod)superExpr.resolve(), GlobalSearchScope.projectScope(myProject), false).toArray(PsiReference.EMPTY_ARRAY);
    for (PsiReference ref : refs) {
      addReference(ref, filesList, startsList, endsList);
    }
    checkResult(fileNames, filesList, starts, startsList, ends, endsList);
  }
项目:tools-idea    文件:DataProviderSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();

  final PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, DataProvider.class.getName());
  if (annotation == null) return;
  PsiNameValuePair[] values = annotation.getParameterList().getAttributes();
  for (PsiNameValuePair value : values) {
    if ("name".equals(value.getName())) {
      final PsiAnnotationMemberValue dataProviderMethodName = value.getValue();
      if (dataProviderMethodName != null) {
        final String providerName = StringUtil.unquoteString(dataProviderMethodName.getText());
        queryParameters.getOptimizer().searchWord(providerName, queryParameters.getScope(), UsageSearchContext.IN_STRINGS, true, method);
      }
    }
  }
}
项目:tools-idea    文件:MultipleExceptionsDeclaredOnTestMethodInspection.java   
@Override
public void visitMethod(PsiMethod method) {
  super.visitMethod(method);
  if (!TestUtils.isJUnitTestMethod(method)) {
    return;
  }
  final PsiReferenceList throwsList = method.getThrowsList();
  final PsiJavaCodeReferenceElement[] referenceElements =
    throwsList.getReferenceElements();
  if (referenceElements.length < 2) {
    return;
  }

  final Query<PsiReference> query =
    MethodReferencesSearch.search(method);
  final PsiReference firstReference = query.findFirst();
  if (firstReference != null) {
    return;
  }
  registerError(throwsList);
}
项目:tools-idea    文件:StaticMethodOnlyUsedInOneClassInspectionBase.java   
/**
 * @return the class the specified method is used from, or null if it is
 *         used from 0 or more than 1 other classes.
 */
@Nullable
public PsiClass getUsageClass(final PsiMethod method) {
  final ProgressManager progressManager = ProgressManager.getInstance();
  final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(method.getProject());
  final String name = method.getName();
  final GlobalSearchScope scope = GlobalSearchScope.allScope(method.getProject());
  if (searchHelper.isCheapEnoughToSearch(name, scope, null, progressManager.getProgressIndicator())
      == PsiSearchHelper.SearchCostResult.TOO_MANY_OCCURRENCES) {
    return null;
  }
  progressManager.runProcess(new Runnable() {
    @Override
    public void run() {
      final Query<PsiReference> query = MethodReferencesSearch.search(method);
      if (!query.forEach(UsageProcessor.this)) {
        foundClass.set(null);
      }
    }
  }, null);
  return foundClass.get();
}
项目:tools-idea    文件:GrLiteralMethodSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters p, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = p.getMethod();
  final PsiClass aClass = method.getContainingClass();
  if (aClass == null) return;

  final String name = method.getName();
  if (StringUtil.isEmpty(name)) return;

  final boolean strictSignatureSearch = p.isStrictSignatureSearch();
  final PsiMethod[] methods = strictSignatureSearch ? new PsiMethod[]{method} : aClass.findMethodsByName(name, false);

  SearchScope accessScope = GroovyScopeUtil.getEffectiveScope(methods);
  final SearchScope restrictedByAccess = GroovyScopeUtil.restrictScopeToGroovyFiles(p.getScope(), accessScope);

  final String textToSearch = findLongestWord(name);

  p.getOptimizer().searchWord(textToSearch, restrictedByAccess, UsageSearchContext.IN_STRINGS, true,
                              new MethodTextOccurrenceProcessor(aClass, strictSignatureSearch, methods));
}
项目:tools-idea    文件:AccessorReferencesSearcher.java   
@Override
public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiElement element = queryParameters.getElementToSearch();
  if (element instanceof PsiMethod) {
    final String propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod)element);
    if (propertyName == null) return;

    queryParameters.getOptimizer().searchWord(propertyName, GroovyScopeUtil
      .restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope()),
                                              UsageSearchContext.IN_CODE, true, element);
  }
  else if (element instanceof GrField) {
    for (GrAccessorMethod method : ((GrField)element).getGetters()) {
      MethodReferencesSearch.search(method, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
    }

    final GrAccessorMethod setter = ((GrField)element).getSetter();
    if (setter != null) {
      MethodReferencesSearch.search(setter, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
    }
  }
}
项目:tools-idea    文件:AccessorMethodReferencesSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();

  final String propertyName;
  if (GdkMethodUtil.isCategoryMethod(method, null, null, PsiSubstitutor.EMPTY)) {
    final GrGdkMethod cat = GrGdkMethodImpl.createGdkMethod(method, false, null);
    propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod)cat);
  }
  else {
    propertyName = GroovyPropertyUtils.getPropertyName(method);
  }

  if (propertyName == null) return;

  final SearchScope onlyGroovyFiles = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getScope(), GroovyScopeUtil.getEffectiveScope(method));

  queryParameters.getOptimizer().searchWord(propertyName, onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);

  if (!GroovyPropertyUtils.isPropertyName(propertyName)) {
    queryParameters.getOptimizer().searchWord(StringUtil.decapitalize(propertyName), onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
  }
}
项目:consulo-java    文件:InferenceFromSourceUtil.java   
private static boolean isUnusedInAnonymousClass(@NotNull PsiMethod method)
{
    PsiClass containingClass = method.getContainingClass();
    if(!(containingClass instanceof PsiAnonymousClass))
    {
        return false;
    }

    if(containingClass.getParent() instanceof PsiNewExpression && containingClass.getParent().getParent() instanceof PsiVariable && !method.getHierarchicalMethodSignature().getSuperSignatures()
            .isEmpty())
    {
        // references outside anonymous class can still resolve to this method, see com.intellij.psi.scope.util.PsiScopesUtil.setupAndRunProcessor()
        return false;
    }

    return MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null;
}
项目:consulo-java    文件:JavaNullMethodArgumentUtil.java   
private static void processCallsWithNullArguments(@NotNull PsiMethod method, int argumentIdx, @NotNull Processor<PsiExpression> nullArgumentProcessor, Collection<VirtualFile> candidateFiles)
{
    if(candidateFiles.isEmpty())
    {
        return;
    }

    GlobalSearchScope scope = GlobalSearchScope.filesScope(method.getProject(), candidateFiles);
    MethodReferencesSearch.search(method, scope, true).forEach(ref ->
    {
        PsiExpression argument = getCallArgument(ref, argumentIdx);
        if(argument instanceof PsiLiteralExpression && argument.textMatches(PsiKeyword.NULL))
        {
            return nullArgumentProcessor.process(argument);
        }
        return true;
    });
}
项目:consulo-java    文件:JavaFindUsagesHandler.java   
@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull final PsiElement target,
        @NotNull final SearchScope searchScope)
{
    if(target instanceof PsiMethod)
    {
        final PsiMethod[] superMethods = ((PsiMethod) target).findDeepestSuperMethods();
        if(superMethods.length == 0)
        {
            return MethodReferencesSearch.search((PsiMethod) target, searchScope, true).findAll();
        }
        final Collection<PsiReference> result = new ArrayList<PsiReference>();
        for(PsiMethod superMethod : superMethods)
        {
            result.addAll(MethodReferencesSearch.search(superMethod, searchScope, true).findAll());
        }
        return result;
    }
    return super.findReferencesToHighlight(target, searchScope);
}
项目:consulo-java    文件:JavaMethodNode.java   
@Override
protected List<PsiMethod> computeCallers() {
  final PsiReference[] refs =
    MethodReferencesSearch.search(myMethod, GlobalSearchScope.allScope(myProject), true).toArray(PsiReference.EMPTY_ARRAY);

  List<PsiMethod> result = new ArrayList<PsiMethod>();
  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    if (!(element instanceof PsiReferenceExpression) ||
        !(((PsiReferenceExpression)element).getQualifierExpression() instanceof PsiSuperExpression)) {
      final PsiElement enclosingContext = PsiTreeUtil.getParentOfType(element, PsiMethod.class, PsiClass.class);
      if (enclosingContext instanceof PsiMethod &&
          !myMethod.equals(enclosingContext) && !myCalled.contains(myMethod)) { //do not add recursive methods
        result.add((PsiMethod)enclosingContext);
      }
      else if (element instanceof PsiClass) {
        final PsiClass aClass = (PsiClass)element;
        result.add(JavaPsiFacade.getElementFactory(myProject).createMethodFromText(aClass.getName() + "(){}", aClass));
      }
    }
  }
  return result;
}
项目:consulo-java    文件:MultipleExceptionsDeclaredOnTestMethodInspection.java   
@Override
public void visitMethod(PsiMethod method) {
  super.visitMethod(method);
  if (!TestUtils.isJUnitTestMethod(method)) {
    return;
  }
  final PsiReferenceList throwsList = method.getThrowsList();
  final PsiJavaCodeReferenceElement[] referenceElements =
    throwsList.getReferenceElements();
  if (referenceElements.length < 2) {
    return;
  }

  final Query<PsiReference> query =
    MethodReferencesSearch.search(method);
  final PsiReference firstReference = query.findFirst();
  if (firstReference != null) {
    return;
  }
  registerError(throwsList);
}
项目:consulo-java    文件:FindUsagesTest.java   
public void testOverloadConstructors() throws Exception {
    PsiClass aClass = myJavaFacade.findClass("B", GlobalSearchScope.allScope(myProject));
    PsiMethod constructor;
//    constructor = myJavaFacade.getElementFactory().createConstructor();
//    constructor = aClass.findMethodBySignature(constructor, false);
    constructor = aClass.findMethodsByName("B", false)[0];
    PsiMethodCallExpression superCall = (PsiMethodCallExpression) constructor.getBody().getStatements()[0].getFirstChild();
    PsiReferenceExpression superExpr = superCall.getMethodExpression();
    String[] fileNames = new String[]{"B.java", "A.java", "A.java", "B.java"};
    int[] starts = new int[]{};
    int[] ends = new int[]{};
    final ArrayList<PsiFile> filesList = new ArrayList<PsiFile>();
    final IntArrayList startsList = new IntArrayList();
    final IntArrayList endsList = new IntArrayList();
    PsiReference[] refs =
      MethodReferencesSearch.search((PsiMethod)superExpr.resolve(), GlobalSearchScope.projectScope(myProject), false).toArray(PsiReference.EMPTY_ARRAY);
    for (PsiReference ref : refs) {
      addReference(ref, filesList, startsList, endsList);
    }
    checkResult(fileNames, filesList, starts, startsList, ends, endsList);
  }
项目:intellij-ce-playground    文件:InferenceFromSourceUtil.java   
private static boolean isUnusedInAnonymousClass(@NotNull PsiMethod method) {
  PsiClass containingClass = method.getContainingClass();
  if (!(containingClass instanceof PsiAnonymousClass)) {
    return false;
  }

  if (containingClass.getParent() instanceof PsiNewExpression && 
      containingClass.getParent().getParent() instanceof PsiVariable && 
      !method.getHierarchicalMethodSignature().getSuperSignatures().isEmpty()) {
    // references outside anonymous class can still resolve to this method, see com.intellij.psi.scope.util.PsiScopesUtil.setupAndRunProcessor()
    return false;
  }

  return MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null;
}
项目:intellij-ce-playground    文件:ThrowSearchUtil.java   
private static boolean scanCatches(@NotNull PsiElement elem,
                                   @NotNull Processor<UsageInfo> processor,
                                   @NotNull Root root,
                                   @NotNull FindUsagesOptions options,
                                   @NotNull Set<PsiMethod> processed) {
  while (elem != null) {
    final PsiElement parent = elem.getParent();
    if (elem instanceof PsiMethod) {
      final PsiMethod deepestSuperMethod = ((PsiMethod)elem).findDeepestSuperMethod();
      final PsiMethod method = deepestSuperMethod != null ? deepestSuperMethod : (PsiMethod)elem;
      if (!processed.contains(method)) {
        processed.add(method);
        final PsiReference[] refs = MethodReferencesSearch.search(method, options.searchScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (int i = 0; i != refs.length; ++i) {
          if (!scanCatches(refs[i].getElement(), processor, root, options, processed)) return false;
        }
      }
      return true;
    }
    if (elem instanceof PsiTryStatement) {
      final PsiTryStatement aTry = (PsiTryStatement)elem;
      final PsiParameter[] catches = aTry.getCatchBlockParameters();
      for (int i = 0; i != catches.length; ++i) {
        if (!processExn(catches[i], processor, root)) {
          return false;
        }
      }
    }
    else if (parent instanceof PsiTryStatement) {
      final PsiTryStatement tryStmt = (PsiTryStatement)parent;
      if (elem != tryStmt.getTryBlock()) {
        elem = parent.getParent();
        continue;
      }
    }
    elem = parent;
  }
  return true;
}
项目:intellij-ce-playground    文件:MethodReferencesFindUsagesTest.java   
public void testConstructorUsages() throws Exception {
  final String testName = getTestName(false);
  configureByFile(BASE_PATH + testName + ".java");

  final PsiClass aClass = getJavaFacade().findClass(testName);
  assertNotNull(aClass);
  final PsiMethod[] constructors = aClass.getConstructors();
  assertEquals(constructors.length, 1);
  Collection<PsiReference> references = MethodReferencesSearch.search(constructors[0]).findAll();
  assertEquals(1, references.size());
}
项目:intellij-ce-playground    文件:FindUsages15Test.java   
public void testGenericMethodOverriderUsages () throws Exception {
  final PsiClass baseClass = myJavaFacade.findClass("pack.GenericClass", GlobalSearchScope.moduleScope(myModule));
  assertNotNull(baseClass);
  final PsiMethod method = baseClass.getMethods()[0];
  PsiReference[] references =
    MethodReferencesSearch.search(method, GlobalSearchScope.moduleScope(myModule), false).toArray(PsiReference.EMPTY_ARRAY);
  assertEquals(1, references.length);
  final PsiElement element = references[0].getElement();
  final PsiClass refClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
  assertEquals(refClass.getName(), "GenericClassDerived");
}
项目:intellij-ce-playground    文件:FindUsages15Test.java   
public void testGenericOverride() throws Exception {
  final PsiClass baseClass = myJavaFacade.findClass("pack.Gen", GlobalSearchScope.moduleScope(myModule));
  assertNotNull(baseClass);
  final PsiMethod method = baseClass.getMethods()[0];
  PsiReference[] references =
    MethodReferencesSearch.search(method, GlobalSearchScope.projectScope(getProject()), true).toArray(PsiReference.EMPTY_ARRAY);

  assertEquals(1, references.length);

  PsiClass refClass = PsiTreeUtil.getParentOfType(references[0].getElement(), PsiClass.class);
  assertEquals("X2", refClass.getName());
}
项目:intellij-ce-playground    文件:ChangeSignaturePropagationTest.java   
private static HashSet<PsiMethod> collectNonPhysicalMethodsToPropagate(PsiMethod method) {
  final HashSet<PsiMethod> methodsToPropagate = new HashSet<>();
  final PsiReference[] references =
    MethodReferencesSearch.search(method, GlobalSearchScope.allScope(getProject()), true).toArray(PsiReference.EMPTY_ARRAY);
  for (PsiReference reference : references) {
    final PsiElement element = reference.getElement();
    Assert.assertTrue(element instanceof PsiClass);
    PsiClass containingClass = (PsiClass)element;
    methodsToPropagate.add(JavaPsiFacade.getElementFactory(getProject()).createMethodFromText(containingClass.getName() + "(){}", containingClass));
  }
  return methodsToPropagate;
}
项目:intellij-ce-playground    文件:DataProviderSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();

  final PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, DataProvider.class.getName());
  if (annotation == null) return;
  final PsiAnnotationMemberValue dataProviderMethodName = annotation.findDeclaredAttributeValue("name");
  if (dataProviderMethodName != null) {
    final String providerName = StringUtil.unquoteString(dataProviderMethodName.getText());
    queryParameters.getOptimizer().searchWord(providerName, queryParameters.getEffectiveSearchScope(), UsageSearchContext.IN_STRINGS, true, method);
  }
}
项目:intellij-ce-playground    文件:SingletonUtil.java   
private static boolean newOnlyAssignsToStaticSelfInstance(
  PsiMethod method, final PsiField field) {
  final Query<PsiReference> search =
    MethodReferencesSearch.search(method, field.getUseScope(),
                                  false);
  final NewOnlyAssignedToFieldProcessor processor =
    new NewOnlyAssignedToFieldProcessor(field);
  search.forEach(processor);
  return processor.isNewOnlyAssignedToField();
}
项目:intellij-ce-playground    文件:MethodLateBoundReferencesSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();
  SearchScope searchScope = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope()).intersectWith(
    getUseScope(method));
  orderSearching(searchScope, method.getName(), method, queryParameters.getOptimizer(), method.getParameterList().getParametersCount());

  final String propName = PropertyUtil.getPropertyName(method);
  if (propName != null) {
    orderSearching(searchScope, propName, method, queryParameters.getOptimizer(), -1);
  }
}
项目:intellij-ce-playground    文件:GroovyReflectedMethodReferenceSearcher.java   
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
  final PsiMethod method = queryParameters.getMethod();
  if (method instanceof GrMethod) {
    for (GrReflectedMethod reflectedMethod : ((GrMethod)method).getReflectedMethods()) {
      MethodReferencesSearch.search(reflectedMethod, queryParameters.getEffectiveSearchScope(), true).forEach(consumer);
    }
  }
}
项目:intellij-ce-playground    文件:GrAliasImportIntention.java   
private static List<UsageInfo> findUsages(PsiMember member, GroovyFileBase file) {
  LocalSearchScope scope = new LocalSearchScope(file);

  final ArrayList<UsageInfo> infos = new ArrayList<UsageInfo>();
  final HashSet<Object> usedRefs = ContainerUtil.newHashSet();

  final Processor<PsiReference> consumer = new Processor<PsiReference>() {
    @Override
    public boolean process(PsiReference reference) {
      if (usedRefs.add(reference)) {
        infos.add(new UsageInfo(reference));
      }

      return true;
    }
  };


  if (member instanceof PsiMethod) {
    MethodReferencesSearch.search((PsiMethod)member, scope, false).forEach(consumer);
  }
  else {
    ReferencesSearch.search(member, scope).forEach(consumer);
    if (member instanceof PsiField) {
      final PsiMethod getter = GroovyPropertyUtils.findGetterForField((PsiField)member);
      if (getter != null) {
        MethodReferencesSearch.search(getter, scope, false).forEach(consumer);
      }
      final PsiMethod setter = GroovyPropertyUtils.findSetterForField((PsiField)member);
      if (setter != null) {
        MethodReferencesSearch.search(setter, scope, false).forEach(consumer);
      }
    }
  }

  return infos;
}
项目:intellij-ce-playground    文件:ExtractClosureFromMethodProcessor.java   
@NotNull
@Override
protected UsageInfo[] findUsages() {
  List<UsageInfo> result = new ArrayList<UsageInfo>();

  final PsiMethod toSearchFor = (PsiMethod)myHelper.getToSearchFor();

  for (PsiReference ref1 : MethodReferencesSearch.search(toSearchFor, GlobalSearchScope.projectScope(myProject), true)) {
    PsiElement ref = ref1.getElement();
    if (ref.getLanguage() != GroovyLanguage.INSTANCE) {
      result.add(new OtherLanguageUsageInfo(ref1));
      continue;
    }

    if (ref instanceof PsiMethod && ((PsiMethod)ref).isConstructor()) {
      DefaultConstructorImplicitUsageInfo implicitUsageInfo =
        new DefaultConstructorImplicitUsageInfo((PsiMethod)ref, ((PsiMethod)ref).getContainingClass(), toSearchFor);
      result.add(implicitUsageInfo);
    }
    else if (ref instanceof PsiClass) {
      result.add(new NoConstructorClassUsageInfo((PsiClass)ref));
    }
    else if (!PsiTreeUtil.isAncestor(myMethod, ref, false)) {
      result.add(new ExternalUsageInfo(ref));
    }
    else {
      result.add(new ChangedMethodCallInfo(ref));
    }
  }

  Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(toSearchFor, true).findAll();

  for (PsiMethod overridingMethod : overridingMethods) {
    result.add(new UsageInfo(overridingMethod));
  }

  final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
  return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
项目:defrac-plugin-intellij    文件:MacroMethodReferencesSearch.java   
@NotNull
@Override
protected SearchScope getSearchScope(@NotNull final MethodReferencesSearch.SearchParameters queryParameter,
                                     @NotNull final PsiMethod element,
                                     @NotNull final DefracFacet facet) {
  final SearchScope defaultScope = queryParameter.getEffectiveSearchScope();

  if(useProvidedSearchScope(defaultScope)) {
    return defaultScope;
  }

  return super.getSearchScope(queryParameter, element, facet);
}
项目:tools-idea    文件:ThrowSearchUtil.java   
private static boolean scanCatches(@NotNull PsiElement elem,
                                   @NotNull Processor<UsageInfo> processor,
                                   @NotNull Root root,
                                   @NotNull FindUsagesOptions options,
                                   @NotNull Set<PsiMethod> processed) {
  while (elem != null) {
    final PsiElement parent = elem.getParent();
    if (elem instanceof PsiMethod) {
      final PsiMethod deepestSuperMethod = ((PsiMethod)elem).findDeepestSuperMethod();
      final PsiMethod method = deepestSuperMethod != null ? deepestSuperMethod : (PsiMethod)elem;
      if (!processed.contains(method)) {
        processed.add(method);
        final PsiReference[] refs = MethodReferencesSearch.search(method, options.searchScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (int i = 0; i != refs.length; ++i) {
          if (!scanCatches(refs[i].getElement(), processor, root, options, processed)) return false;
        }
      }
      return true;
    }
    if (elem instanceof PsiTryStatement) {
      final PsiTryStatement aTry = (PsiTryStatement)elem;
      final PsiParameter[] catches = aTry.getCatchBlockParameters();
      for (int i = 0; i != catches.length; ++i) {
        if (!processExn(catches[i], processor, root)) {
          return false;
        }
      }
    }
    else if (parent instanceof PsiTryStatement) {
      final PsiTryStatement tryStmt = (PsiTryStatement)parent;
      if (elem != tryStmt.getTryBlock()) {
        elem = parent.getParent();
        continue;
      }
    }
    elem = parent;
  }
  return true;
}
项目:tools-idea    文件:JavaFindUsagesHandler.java   
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull final PsiElement target, @NotNull final SearchScope searchScope) {
  if (target instanceof PsiMethod) {
    final PsiMethod[] superMethods = ((PsiMethod)target).findDeepestSuperMethods();
    if (superMethods.length == 0) {
      return MethodReferencesSearch.search((PsiMethod)target, searchScope, true).findAll();
    }
    final Collection<PsiReference> result = new ArrayList<PsiReference>();
    for (PsiMethod superMethod : superMethods) {
      result.addAll(MethodReferencesSearch.search(superMethod, searchScope, true).findAll());
    }
    return result;
  }
  return super.findReferencesToHighlight(target, searchScope);
}
项目:tools-idea    文件:InvertBooleanProcessor.java   
private void addRefsToInvert(final List<SmartPsiElementPointer> toInvert, final PsiNamedElement namedElement) {
  final Query<PsiReference> query = namedElement instanceof PsiMethod ?
                                    MethodReferencesSearch.search((PsiMethod)namedElement) :
                                    ReferencesSearch.search(namedElement);
  final Collection<PsiReference> refs = query.findAll();

  for (PsiReference ref : refs) {
    final PsiElement element = ref.getElement();
    if (element instanceof PsiReferenceExpression) {
      final PsiReferenceExpression refExpr = (PsiReferenceExpression)element;
      PsiElement parent = refExpr.getParent();
      if (parent instanceof PsiAssignmentExpression && refExpr.equals(((PsiAssignmentExpression)parent).getLExpression())) {
        toInvert.add(mySmartPointerManager.createSmartPsiElementPointer(((PsiAssignmentExpression)parent).getRExpression()));
      }
      else {
        if (namedElement instanceof PsiParameter) { //filter usages in super method calls
          if (refExpr.getParent().getParent() instanceof PsiMethodCallExpression) {
            final PsiReferenceExpression methodExpression = ((PsiMethodCallExpression)refExpr.getParent().getParent()).getMethodExpression();
            if (methodExpression.getQualifier() != null && "super".equals(methodExpression.getQualifierExpression().getText())) {
              continue;
            }
          }
        }

        toInvert.add(mySmartPointerManager.createSmartPsiElementPointer(refExpr));
      }
    }
  }

  if (namedElement instanceof PsiVariable) {
    final PsiExpression initializer = ((PsiVariable)namedElement).getInitializer();
    if (initializer != null) {
      toInvert.add(mySmartPointerManager.createSmartPsiElementPointer(initializer));
    }
  }
}