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

项目:intellij-ce-playground    文件:Src15RepositoryUseTest.java   
public void testAnnotationIndex() throws Exception {
  getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() {
    @Override
    public boolean accept(final VirtualFile file) {
      return !"package-info.java".equals(file.getName());
    }
  }, myTestRootDisposable);

  final PsiClass annotationTypeClass = findClass("annotations.AnnotationType");
  assertTrue(annotationTypeClass.isAnnotationType());

  final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();

  assertEquals(2, all.size());
  Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod"));
  for (PsiMember member : all) {
    assertTrue(correctNames.contains(member.getName()));
  }

  final Collection<PsiPackage> packages =
    AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();
  assertEquals(1, packages.size());
  assertEquals("annotated", packages.iterator().next().getQualifiedName());

  tearDownLoadingFilter();
}
项目:tools-idea    文件:Src15RepositoryUseTest.java   
public void testAnnotationIndex() throws Exception {
  getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() {
    @Override
    public boolean accept(final VirtualFile file) {
      return !"package-info.java".equals(file.getName());
    }
  });

  final PsiClass annotationTypeClass = findClass("annotations.AnnotationType");
  assertTrue(annotationTypeClass.isAnnotationType());

  final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();

  assertEquals(2, all.size());
  Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod"));
  for (PsiMember member : all) {
    assertTrue(correctNames.contains(member.getName()));
  }

  final Collection<PsiPackage> packages =
    AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();
  assertEquals(1, packages.size());
  assertEquals("annotated", packages.iterator().next().getQualifiedName());

  tearDownLoadingFilter();
}
项目:consulo-java    文件:Src15RepositoryUseTest.java   
public void testAnnotationIndex() throws Exception {
  getJavaFacade().setAssertOnFileLoadingFilter(new VirtualFileFilter() {
    @Override
    public boolean accept(final VirtualFile file) {
      return !"package-info.java".equals(file.getName());
    }
  }, null);

  final PsiClass annotationTypeClass = findClass("annotations.AnnotationType");
  assertTrue(annotationTypeClass.isAnnotationType());

  final Collection<PsiMember> all = AnnotatedMembersSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();

  assertEquals(2, all.size());
  Set<String> correctNames = new HashSet<String>(Arrays.asList("AnnotatedClass", "correctMethod"));
  for (PsiMember member : all) {
    assertTrue(correctNames.contains(member.getName()));
  }

  final Collection<PsiJavaPackage> packages =
    AnnotatedPackagesSearch.search(annotationTypeClass, GlobalSearchScope.moduleScope(myModule)).findAll();
  assertEquals(1, packages.size());
  assertEquals("annotated", packages.iterator().next().getQualifiedName());

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

  final String annotationFQN = annClass.getQualifiedName();
  assert annotationFQN != null;

  final PsiManagerImpl psiManager = (PsiManagerImpl)annClass.getManager();
  final SearchScope useScope = p.getScope();

  final String annotationShortName = annClass.getName();
  assert annotationShortName != null;

  final GlobalSearchScope scope = useScope instanceof GlobalSearchScope ? (GlobalSearchScope)useScope : null;

  final Collection<PsiAnnotation> annotations = JavaAnnotationIndex.getInstance().get(annotationShortName, psiManager.getProject(), scope);
  for (PsiAnnotation annotation : annotations) {
    PsiModifierList modlist = (PsiModifierList)annotation.getParent();
    final PsiElement owner = modlist.getParent();
    if (!(owner instanceof PsiClass)) continue;
    PsiClass candidate = (PsiClass)owner;
    if (!"package-info".equals(candidate.getName())) continue;

    LOG.assertTrue(candidate.isValid());

    final PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement();
    if (ref == null) continue;

    if (!psiManager.areElementsEquivalent(ref.resolve(), annClass)) continue;
    if (useScope instanceof GlobalSearchScope &&
        !((GlobalSearchScope)useScope).contains(candidate.getContainingFile().getVirtualFile())) {
      continue;
    }
    final String qname = candidate.getQualifiedName();
    if (qname != null && !consumer.process(JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(
      qname.substring(0, qname.lastIndexOf('.'))))) {
      return false;
    }
  }

  PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(psiManager.getProject());
  final GlobalSearchScope infoFilesFilter = new PackageInfoFilesOnly();

  GlobalSearchScope infoFiles =
    useScope instanceof GlobalSearchScope ? ((GlobalSearchScope)useScope).intersectWith(infoFilesFilter) : infoFilesFilter;

  final boolean[] wantmore = new boolean[]{true};
  helper.processAllFilesWithWord(annotationShortName, infoFiles, new Processor<PsiFile>() {
    @Override
    public boolean process(final PsiFile psiFile) {
      PsiPackageStatement stmt = PsiTreeUtil.getChildOfType(psiFile, PsiPackageStatement.class);
      if (stmt == null) return true;

      final PsiModifierList annotations = stmt.getAnnotationList();
      if (annotations == null) return true;
      final PsiAnnotation ann = annotations.findAnnotation(annotationFQN);
      if (ann == null) return true;

      final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
      if (ref == null) return true;

      if (!psiManager.areElementsEquivalent(ref.resolve(), annClass)) return true;

      wantmore[0] = consumer.process(JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(stmt.getPackageName()));
      return wantmore[0];
    }
  }, true);

  return wantmore[0];
}
项目:consulo-java    文件:AnnotatedPackagesSearcher.java   
@Override
public boolean execute(@NotNull final AnnotatedPackagesSearch.Parameters p, @NotNull final Processor<PsiJavaPackage> consumer) {
  final PsiClass annClass = p.getAnnotationClass();
  assert annClass.isAnnotationType() : "Annotation type should be passed to annotated packages search";

  final String annotationFQN = annClass.getQualifiedName();
  assert annotationFQN != null;

  final PsiManagerImpl psiManager = (PsiManagerImpl)annClass.getManager();
  final SearchScope useScope = p.getScope();

  final String annotationShortName = annClass.getName();
  assert annotationShortName != null;

  final GlobalSearchScope scope = useScope instanceof GlobalSearchScope ? (GlobalSearchScope)useScope : null;

  final Collection<PsiAnnotation> annotations = JavaAnnotationIndex.getInstance().get(annotationShortName, psiManager.getProject(), scope);
  for (PsiAnnotation annotation : annotations) {
    PsiModifierList modlist = (PsiModifierList)annotation.getParent();
    final PsiElement owner = modlist.getParent();
    if (!(owner instanceof PsiClass)) continue;
    PsiClass candidate = (PsiClass)owner;
    if (!"package-info".equals(candidate.getName())) continue;

    LOG.assertTrue(candidate.isValid());

    final PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement();
    if (ref == null) continue;

    if (!psiManager.areElementsEquivalent(ref.resolve(), annClass)) continue;
    if (useScope instanceof GlobalSearchScope &&
        !((GlobalSearchScope)useScope).contains(candidate.getContainingFile().getVirtualFile())) {
      continue;
    }
    final String qname = candidate.getQualifiedName();
    if (qname != null && !consumer.process(JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(
      qname.substring(0, qname.lastIndexOf('.'))))) {
      return false;
    }
  }

  PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(psiManager.getProject());
  final GlobalSearchScope infoFilesFilter = new PackageInfoFilesOnly();

  GlobalSearchScope infoFiles =
    useScope instanceof GlobalSearchScope ? ((GlobalSearchScope)useScope).intersectWith(infoFilesFilter) : infoFilesFilter;

  final boolean[] wantmore = new boolean[]{true};
  helper.processAllFilesWithWord(annotationShortName, infoFiles, new Processor<PsiFile>() {
    @Override
    public boolean process(final PsiFile psiFile) {
      PsiPackageStatement stmt = PsiTreeUtil.getChildOfType(psiFile, PsiPackageStatement.class);
      if (stmt == null) return true;

      final PsiModifierList annotations = stmt.getAnnotationList();
      if (annotations == null) return true;
      final PsiAnnotation ann = annotations.findAnnotation(annotationFQN);
      if (ann == null) return true;

      final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
      if (ref == null) return true;

      if (!psiManager.areElementsEquivalent(ref.resolve(), annClass)) return true;

      wantmore[0] = consumer.process(JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(stmt.getPackageName()));
      return wantmore[0];
    }
  }, true);

  return wantmore[0];
}