@NotNull
  private List<AnnotationData> doCollect(
      @NotNull PsiModifierListOwner listOwner, boolean onlyWritable) {
    final List<PsiFile> files = findExternalAnnotationsFiles(listOwner);
    if (files == null) {
      return NO_DATA;
    }
    SmartList<AnnotationData> result = new SmartList<AnnotationData>();
    String externalName = getExternalName(listOwner, false);
    if (externalName == null) return NO_DATA;

    for (PsiFile file : files) {
      if (!file.isValid()) continue;
      if (onlyWritable && !file.isWritable()) continue;

      MostlySingularMultiMap<String, AnnotationData> fileData = getDataFromFile(file);

      ContainerUtil.addAll(result, fileData.get(externalName));
    }
    if (result.isEmpty()) {
      return NO_DATA;
    }
    result.trimToSize();
    return result;
  }
 private static AnnotationData findByFQN(
     @NotNull List<AnnotationData> map, @NotNull final String annotationFQN) {
   return ContainerUtil.find(
       map,
       new Condition<AnnotationData>() {
         @Override
         public boolean value(AnnotationData data) {
           return data.annotationClassFqName.equals(annotationFQN);
         }
       });
 }
Exemplo n.º 3
0
 public static void runHighlightingTypeMigration(
     final Project project,
     final Editor editor,
     final TypeMigrationRules rules,
     final PsiElement[] roots,
     final Function<PsiElement, PsiType> migrationTypeFunction,
     final boolean optimizeImports) {
   final Set<PsiFile> containingFiles =
       ContainerUtil.map2Set(roots, element -> element.getContainingFile());
   final TypeMigrationProcessor processor =
       new TypeMigrationProcessor(project, roots, migrationTypeFunction, rules) {
         @Override
         public void performRefactoring(@NotNull final UsageInfo[] usages) {
           super.performRefactoring(usages);
           if (editor != null) {
             ApplicationManager.getApplication()
                 .invokeLater(
                     () -> {
                       final List<PsiElement> result = new ArrayList<>();
                       for (UsageInfo usage : usages) {
                         final PsiElement element = usage.getElement();
                         if (element == null
                             || !containingFiles.contains(element.getContainingFile())) continue;
                         if (element instanceof PsiMethod) {
                           result.add(((PsiMethod) element).getReturnTypeElement());
                         } else if (element instanceof PsiVariable) {
                           result.add(((PsiVariable) element).getTypeElement());
                         } else {
                           result.add(element);
                         }
                       }
                       RefactoringUtil.highlightAllOccurrences(
                           project, PsiUtilCore.toPsiElementArray(result), editor);
                     });
           }
           if (optimizeImports) {
             final JavaCodeStyleManager javaCodeStyleManager =
                 JavaCodeStyleManager.getInstance(myProject);
             final Set<PsiFile> affectedFiles = new THashSet<>();
             for (UsageInfo usage : usages) {
               final PsiFile usageFile = usage.getFile();
               if (usageFile != null) {
                 affectedFiles.add(usageFile);
               }
             }
             for (PsiFile file : affectedFiles) {
               javaCodeStyleManager.optimizeImports(file);
               javaCodeStyleManager.shortenClassReferences(file);
             }
           }
         }
       };
   processor.run();
 }
 @Override
 @Nullable
 public PsiAnnotation[] findExternalAnnotations(@NotNull final PsiModifierListOwner listOwner) {
   final List<AnnotationData> result = collectExternalAnnotations(listOwner);
   return result.isEmpty()
       ? null
       : ContainerUtil.map2Array(
           result,
           PsiAnnotation.EMPTY_ARRAY,
           new Function<AnnotationData, PsiAnnotation>() {
             @Override
             public PsiAnnotation fun(AnnotationData data) {
               return data.getAnnotation(BaseExternalAnnotationsManager.this);
             }
           });
 }