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

项目:intellij-plugin    文件:CoffigResolver.java   
Stream<PsiClass> classStream() {
    if (context.isValid()) {
        Stream<PsiClass> stream = StreamSupport.stream(
                AnnotatedElementsSearch.searchPsiClasses(
                        context.getConfigAnnotationClass(),
                        GlobalSearchScope.allScope(context.getProject())).spliterator(),
                false);
        Predicate<PsiClass> filter = context.getFilter();
        if (filter != null) {
            stream = stream.filter(filter);
        }
        return stream;
    } else {
        return Stream.empty();
    }
}
项目:intellij-plugin    文件:AnnotatedInterfaceNode.java   
@Override
protected MultiMap<PsiFile, T> computeChildren(@Nullable PsiFile psiFile) {
    MultiMap<PsiFile, T> children = new MultiMap<>();
    Project project = getProject();
    if (project != null) {
        JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
        PsiClass serviceAnnotation = javaPsiFacade.findClass(getAnnotationQName(), GlobalSearchScope.allScope(project));
        if (serviceAnnotation != null) {
            AnnotatedElementsSearch.searchPsiClasses(serviceAnnotation, GlobalSearchScope.allScope(project)).forEach(psiClass -> {
                if (psiClass.isInterface() && isSatisfying(psiClass)) {
                    children.putValue(psiClass.getContainingFile(), createChild(psiClass));
                }
                return true;
            });
        }
    }
    return children;
}
项目:intellij-plugin    文件:ResourcesNode.java   
@Override
public MultiMap<PsiFile, ResourceNode> computeChildren(PsiFile psiFile) {
    Project project = getProject();
    MultiMap<PsiFile, ResourceNode> children = new MultiMap<>();
    if (project != null) {
        JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
        PsiClass pathAnnotation = javaPsiFacade.findClass(PATH_ANNOTATION, GlobalSearchScope.allScope(project));
        if (pathAnnotation != null) {
            AnnotatedElementsSearch.searchPsiClasses(pathAnnotation, GlobalSearchScope.allScope(project)).forEach(psiClass -> {
                if (!psiClass.isInterface() && !NavigatorUtil.isAbstract(psiClass)) {
                    children.putValue(psiClass.getContainingFile(), new ResourceNode(ResourcesNode.this, pathAnnotation, psiClass));
                }
                return true;
            });
        }
    }
    return children;
}
项目:tools-idea    文件:GroovyConstructorUsagesSearcher.java   
private static GlobalSearchScope calcGppScope(Project project) {
  final GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
  final GlobalSearchScope maximal = GlobalSearchScope.getScopeRestrictedByFileTypes(allScope, GroovyFileType.GROOVY_FILE_TYPE);
  GlobalSearchScope gppExtensions = new DelegatingGlobalSearchScope(maximal, "groovy.gpp") {
    @Override
    public boolean contains(VirtualFile file) {
      return super.contains(file) && GppTypeConverter.isGppExtension(file.getExtension());
    }
  };
  final PsiClass typed = JavaPsiFacade.getInstance(project).findClass(GppTypeConverter.GROOVY_LANG_TYPED, allScope);
  if (typed != null) {
    final Set<VirtualFile> files = new HashSet<VirtualFile>();
    AnnotatedElementsSearch.searchElements(typed, maximal, PsiModifierListOwner.class).forEach(new Processor<PsiModifierListOwner>() {
      @Override
      public boolean process(PsiModifierListOwner occurrence) {
        ContainerUtil.addIfNotNull(occurrence.getContainingFile().getVirtualFile(), files);
        return true;
      }
    });

    GlobalSearchScope withTypedAnno = GlobalSearchScope.filesScope(project, files);
    return withTypedAnno.union(gppExtensions);
  }

  return gppExtensions;
}
项目:consulo-java    文件:MetaAnnotationUtil.java   
public static Set<PsiClass> getChildren(@NotNull PsiClass psiClass, @NotNull GlobalSearchScope scope)
{
    if(AnnotationTargetUtil.findAnnotationTarget(psiClass, PsiAnnotation.TargetType.ANNOTATION_TYPE, PsiAnnotation.TargetType.TYPE) == null)
    {
        return Collections.emptySet();
    }

    String name = psiClass.getQualifiedName();
    if(name == null)
    {
        return Collections.emptySet();
    }

    Set<PsiClass> result = new THashSet<>(HASHING_STRATEGY);

    AnnotatedElementsSearch.searchPsiClasses(psiClass, scope).forEach(processorResult ->
    {
        if(processorResult.isAnnotationType())
        {
            result.add(processorResult);
        }
        return true;
    });

    return result;
}
项目:consulo-java    文件:MetaAnnotationUtil.java   
public static Set<PsiClass> getChildren(final PsiClass psiClass, final GlobalSearchScope scope)
{
    if(AnnotationTargetUtil.findAnnotationTarget(psiClass, PsiAnnotation.TargetType.ANNOTATION_TYPE, PsiAnnotation.TargetType.TYPE) == null)
    {
        return Collections.emptySet();
    }

    final String name = psiClass.getQualifiedName();
    if(name == null)
    {
        return Collections.emptySet();
    }

    final Set<PsiClass> result = new THashSet<>(HASHING_STRATEGY);

    AnnotatedElementsSearch.searchPsiClasses(psiClass, scope).forEach(processorResult ->
    {
        if(processorResult.isAnnotationType())
        {
            result.add(processorResult);
        }
        return true;
    });

    return result;
}
项目:intellij-ce-playground    文件:ClassesWithAnnotatedMembersSearcher.java   
@Override
public void processQuery(@NotNull ClassesWithAnnotatedMembersSearch.Parameters queryParameters,
                         @NotNull final Processor<PsiClass> consumer) {
  SearchScope scope = queryParameters.getScope();
  for (QueryExecutor executor : Extensions.getExtensions(ClassesWithAnnotatedMembersSearch.EP_NAME)) {
    if (executor instanceof ScopedQueryExecutor) {
      scope = scope.intersectWith(GlobalSearchScope.notScope(((ScopedQueryExecutor) executor).getScope(queryParameters)));
    }
  }

  final Set<PsiClass> processed = new HashSet<PsiClass>();
  AnnotatedElementsSearch.searchPsiMembers(queryParameters.getAnnotationClass(), scope).forEach(new Processor<PsiMember>() {
    @Override
    public boolean process(PsiMember member) {
      PsiClass psiClass;
      AccessToken token = ReadAction.start();
      try {
        psiClass = member instanceof PsiClass ? (PsiClass)member : member.getContainingClass();
      }
      finally {
        token.finish();
      }

      if (psiClass != null && processed.add(psiClass)) {
        consumer.process(psiClass);
      }

      return true;
    }
  });
}
项目:intellij-ce-playground    文件:DataBindingUtil.java   
private Set<String> collectVariableNamesFromBindables() {
  JavaPsiFacade facade = JavaPsiFacade.getInstance(myFacet.getModule().getProject());
  PsiClass aClass = facade.findClass(BINDABLE_QUALIFIED_NAME, myFacet.getModule().getModuleWithDependenciesAndLibrariesScope(false));
  if (aClass == null) {
    return null;
  }
  //noinspection unchecked
  final Collection<? extends PsiModifierListOwner> psiElements =
    AnnotatedElementsSearch.searchElements(aClass, myFacet.getModule().getModuleScope(), PsiMethod.class, PsiField.class).findAll();
  return BrUtil.collectIds(psiElements);
}
项目:robot-intellij-plugin    文件:RobotJavaPsiUtil.java   
public static void findAllJavaRobotKeywords(Project project, List<PsiElement> results, boolean wrapPsiMethods) {
    // Search for all methods annotated with @RobotKeyword
    GlobalSearchScope allScope = ProjectScope.getAllScope(project);
    PsiClass robotKeywordAnnotation = getRobotKeywordPsiClass(project);
    if (robotKeywordAnnotation == null) {
        return; // If @RobotKeyword isn't on the classpath, just return.
    }
    Query<PsiMethod> query = AnnotatedElementsSearch.searchPsiMethods(robotKeywordAnnotation, allScope);
    Processor<PsiMethod> methodProcessor = new RobotJavaMethodProcessor(results, SearchType.FIND_ALL, Optional.<String>absent(), wrapPsiMethods);
    query.forEach(methodProcessor);
}
项目:robot-intellij-plugin    文件:RobotJavaPsiUtil.java   
public static void findAllJavaRobotKeywordsStartingWith(Project project, List<PsiElement> results, String startsWith, boolean wrapPsiMethods) {
    // Search for all methods annotated with @RobotKeyword that start with the given text
    GlobalSearchScope allScope = ProjectScope.getAllScope(project);
    PsiClass robotKeywordAnnotation = getRobotKeywordPsiClass(project);
    if (robotKeywordAnnotation == null) {
        // If @RobotKeyword isn't on the classpath, fall back to using word index.
        findAllJavaKeywordsStartingWithUsingWordIndex(project, results, startsWith, wrapPsiMethods);
        return;
    }
    Query<PsiMethod> query = AnnotatedElementsSearch.searchPsiMethods(robotKeywordAnnotation, allScope);
    Processor<PsiMethod> methodProcessor = new RobotJavaMethodProcessor(results, SearchType.STARTS_WITH, Optional.of(startsWith), wrapPsiMethods);
    query.forEach(methodProcessor);
}
项目:Intellij-Plugin    文件:StepUtil.java   
public static Collection<PsiMethod> getStepMethods(Module module) {
    final PsiClass step = JavaPsiFacade.getInstance(module.getProject()).findClass(STEP_ANNOTATION_QUALIFIER, GlobalSearchScope.allScope(module.getProject()));
    if (step != null) {
        Collection<PsiMethod> methods = new ArrayList<>();
        for (Module m : Gauge.getSubModules(module))
            methods.addAll(AnnotatedElementsSearch.searchPsiMethods(step, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(m, true)).findAll());
        return methods;
    }
    return new ArrayList<>();
}
项目:nuxeo-intellij    文件:NuxeoHotReloader.java   
private Collection<PsiClass> findSeamClasses(PsiClass seamAnnotationClass,
        Module module) {
    GlobalSearchScope moduleContentRootSearchScope = ModuleContentRootSearchScope.moduleScope(module);
    Query<PsiClass> query = AnnotatedElementsSearch.searchPsiClasses(
            seamAnnotationClass, moduleContentRootSearchScope);
    return query.findAll();
}
项目:consulo-javaee    文件:JamCommonUtil.java   
public static <T extends PsiModifierListOwner> void findAnnotatedElements(Class<T> elementClass, String annotationClass, PsiManager psiManager, GlobalSearchScope scope, Processor<T> processor)
{
    final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(annotationClass, GlobalSearchScope.allScope(psiManager.getProject()));
    if(aClass != null)
    {
        AnnotatedElementsSearch.searchElements(aClass, scope, elementClass).forEach(processor);
    }
}
项目:consulo-java    文件:ClassesWithAnnotatedMembersSearcher.java   
@Override
public void processQuery(@NotNull ClassesWithAnnotatedMembersSearch.Parameters queryParameters, @NotNull final Processor<PsiClass> consumer)
{
    SearchScope scope = queryParameters.getScope();
    for(QueryExecutor executor : Extensions.getExtensions(ClassesWithAnnotatedMembersSearch.EP_NAME))
    {
        if(executor instanceof ScopedQueryExecutor)
        {
            scope = scope.intersectWith(GlobalSearchScope.notScope(((ScopedQueryExecutor) executor).getScope(queryParameters)));
        }
    }

    final Set<PsiClass> processed = new HashSet<>();
    AnnotatedElementsSearch.searchPsiMembers(queryParameters.getAnnotationClass(), scope).forEach(member ->
    {
        PsiClass psiClass;
        AccessToken token = ReadAction.start();
        try
        {
            psiClass = member instanceof PsiClass ? (PsiClass) member : member.getContainingClass();
        }
        finally
        {
            token.finish();
        }

        if(psiClass != null && processed.add(psiClass))
        {
            consumer.process(psiClass);
        }

        return true;
    });
}
项目:jgiven-intellij-plugin    文件:ScenarioStateReferenceProvider.java   
private void findPsiFields(Project project, GlobalSearchScope scope, Processor<PsiField> processor) {
    getScenarioStateClasses(project)
            .forEach(a -> AnnotatedElementsSearch.searchPsiFields(a, scope).forEach(processor));
}
项目:intellij-ce-playground    文件:AnnotatedElementsSearcher.java   
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
  final PsiClass annClass = p.getAnnotationClass();
  assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";

  final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
    @Override
    public String compute() {
      return annClass.getQualifiedName();
    }
  });
  assert annotationFQN != null;

  final PsiManager psiManager = ApplicationManager.getApplication().runReadAction(new Computable<PsiManager>() {
    @Override
    public PsiManager compute() {
      return annClass.getManager();
    }
  });

  final SearchScope useScope = p.getScope();
  final Class<? extends PsiModifierListOwner>[] types = p.getTypes();

  for (final PsiAnnotation ann : getAnnotationCandidates(annClass, useScope)) {
    final PsiModifierListOwner candidate = ApplicationManager.getApplication().runReadAction(new Computable<PsiModifierListOwner>() {
      @Override
      public PsiModifierListOwner compute() {
        PsiElement parent = ann.getParent();
        if (!(parent instanceof PsiModifierList)) {
          return null; // Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
        }

        final PsiElement owner = parent.getParent();
        if (!isInstanceof(owner, types)) {
          return null;
        }

        final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
        if (ref == null || !psiManager.areElementsEquivalent(ref.resolve(), annClass)) {
          return null;
        }

        return (PsiModifierListOwner)owner;
      }
    });

    if (candidate != null && !consumer.process(candidate)) {
      return false;
    }
  }

  return true;
}
项目:intellij-ce-playground    文件:AnnotatedMembersSearcher.java   
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
  final PsiClass annClass = p.getAnnotationClass();
  assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";

  final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
    @Override
    public String compute() {
      return annClass.getQualifiedName();
    }
  });
  assert annotationFQN != null;

  final SearchScope scope = p.getScope();

  final List<PsiModifierListOwner> candidates;
  if (scope instanceof GlobalSearchScope) {
    candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope)scope));
  }
  else {
    candidates = new ArrayList<PsiModifierListOwner>();
    for (final PsiElement element : ((LocalSearchScope)scope).getScope()) {
      ApplicationManager.getApplication().runReadAction(new Runnable() {
        @Override
        public void run() {
          if (element instanceof GroovyPsiElement) {
            ((GroovyPsiElement)element).accept(new GroovyRecursiveElementVisitor() {
              @Override
              public void visitMethod(GrMethod method) {
                candidates.add(method);
              }

              @Override
              public void visitField(GrField field) {
                candidates.add(field);
              }
            });
          }
        }
      });
    }
  }

  for (final PsiModifierListOwner candidate : candidates) {
    boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>(){
      @Override
      public Boolean compute() {
        if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
          PsiModifierList list = candidate.getModifierList();
          if (list != null) {
            for (PsiAnnotation annotation : list.getAnnotations()) {
              if (annotationFQN.equals(annotation.getQualifiedName()) && !consumer.process(candidate)) {
                return false;
              }
            }
          }
        }
        return true;
      }
    });
    if (!accepted) return false;
  }

  return true;
}
项目:tools-idea    文件:AnnotatedElementsSearcher.java   
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
  final PsiClass annClass = p.getAnnotationClass();
  assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";

  final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
    @Override
    public String compute() {
      return annClass.getQualifiedName();
    }
  });
  assert annotationFQN != null;

  final PsiManagerImpl psiManager = (PsiManagerImpl)annClass.getManager();

  final SearchScope useScope = p.getScope();
  final Class<? extends PsiModifierListOwner>[] types = p.getTypes();

  for (final PsiAnnotation ann : getAnnotationCandidates(annClass, useScope)) {
    final PsiModifierListOwner candidate = ApplicationManager.getApplication().runReadAction(new Computable<PsiModifierListOwner>() {
      @Override
      public PsiModifierListOwner compute() {
        PsiElement parent = ann.getParent();
        if (!(parent instanceof PsiModifierList)) {
          return null; // Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
        }

        final PsiElement owner = parent.getParent();
        if (!isInstanceof(owner, types)) {
          return null;
        }

        final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
        if (ref == null || !psiManager.areElementsEquivalent(ref.resolve(), annClass)) {
          return null;
        }

        return (PsiModifierListOwner)owner;
      }
    });

    if (candidate != null && !consumer.process(candidate)) {
      return false;
    }
  }

  return true;
}
项目:tools-idea    文件:AnnotatedMembersSearcher.java   
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
  final PsiClass annClass = p.getAnnotationClass();
  assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";

  AccessToken token = ReadAction.start();
  final String annotationFQN;
  try {
    annotationFQN = annClass.getQualifiedName();
  }
  finally {
    token.finish();
  }
  assert annotationFQN != null;

  final SearchScope scope = p.getScope();

  final List<PsiModifierListOwner> candidates;
  if (scope instanceof GlobalSearchScope) {
    candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope)scope));
  } else {
    candidates = new ArrayList<PsiModifierListOwner>();
    for (PsiElement element : ((LocalSearchScope)scope).getScope()) {
      if (element instanceof GroovyPsiElement) {
        ((GroovyPsiElement)element).accept(new GroovyRecursiveElementVisitor() {
          public void visitMethod(GrMethod method) {
            candidates.add(method);
          }

          public void visitField(GrField field) {
            candidates.add(field);
          }
        });
      }
    }
  }

  for (PsiModifierListOwner candidate : candidates) {
    token = ReadAction.start();
    try {
      if (!AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
        continue;
      }

      PsiModifierList list = candidate.getModifierList();
      if (list != null) {
        for (PsiAnnotation annotation : list.getAnnotations()) {
          if (annotationFQN.equals(annotation.getQualifiedName()) && !consumer.process(candidate)) {
            return false;
          }
        }
      }
    }
    finally {
      token.finish();
    }
  }

  return true;
}
项目:consulo-java    文件:AnnotatedElementsSearcher.java   
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer)
{
    final PsiClass annClass = p.getAnnotationClass();
    assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";

    final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>()
    {
        @Override
        public String compute()
        {
            return annClass.getQualifiedName();
        }
    });
    assert annotationFQN != null;

    final PsiManager psiManager = ApplicationManager.getApplication().runReadAction(new Computable<PsiManager>()
    {
        @Override
        public PsiManager compute()
        {
            return annClass.getManager();
        }
    });

    final SearchScope useScope = p.getScope();
    final Class<? extends PsiModifierListOwner>[] types = p.getTypes();

    for(final PsiAnnotation ann : getAnnotationCandidates(annClass, useScope))
    {
        final PsiModifierListOwner candidate = ApplicationManager.getApplication().runReadAction(new Computable<PsiModifierListOwner>()
        {
            @Override
            public PsiModifierListOwner compute()
            {
                PsiElement parent = ann.getParent();
                if(!(parent instanceof PsiModifierList))
                {
                    return null; // Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
                }

                final PsiElement owner = parent.getParent();
                if(!isInstanceof(owner, types))
                {
                    return null;
                }

                final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
                if(ref == null || !psiManager.areElementsEquivalent(ref.resolve(), annClass))
                {
                    return null;
                }

                return (PsiModifierListOwner) owner;
            }
        });

        if(candidate != null && !consumer.process(candidate))
        {
            return false;
        }
    }

    return true;
}