attach bundled library quick fix: cleanup and deduplication
authornik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Jul 2015 11:12:28 +0000 (14:12 +0300)
committernik <Nikolay.Chashnikov@jetbrains.com>
Wed, 15 Jul 2015 11:49:57 +0000 (14:49 +0300)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java
java/java-impl/src/com/intellij/codeInspection/concurrencyAnnotations/JCiPOrderEntryFix.java
java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java
plugins/testng/src/com/theoryinpractice/testng/intention/TestNGOrderEntryFix.java

index 73b067ce25edc69fc37ff98e307bcafc4e2b6e19..4872f946d0b4977aa782be4545dc74571c9d74f2 100644 (file)
@@ -38,7 +38,6 @@ import com.intellij.openapi.roots.libraries.Library;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VfsUtil;
 import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.openapi.vfs.VirtualFileManager;
 import com.intellij.packageDependencies.DependencyValidationManager;
 import com.intellij.pom.java.LanguageLevel;
 import com.intellij.psi.*;
@@ -46,7 +45,6 @@ import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.PsiShortNamesCache;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.psi.util.PsiUtil;
-import com.intellij.psi.util.PsiUtilCore;
 import com.intellij.util.Function;
 import com.intellij.util.PathUtil;
 import com.intellij.util.containers.ContainerUtil;
@@ -62,6 +60,8 @@ import java.util.*;
  * @author cdr
  */
 public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
+  private static final String JUNIT4_LIBRARY_NAME = "JUnit4";
+
   OrderEntryFix() {
   }
 
@@ -123,27 +123,17 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
 
         @Override
         public void invoke(@NotNull Project project, @Nullable Editor editor, PsiFile file) {
+          List<String> jarPaths;
+          String libraryName;
           if (isJunit4) {
-            final VirtualFile location = PsiUtilCore.getVirtualFile(reference.getElement());
-            boolean inTests = location != null && ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(location);
-            DumbService.getInstance(project).setAlternativeResolveEnabled(true);
-            try {
-              addJUnit4Library(inTests, currentModule);
-              final GlobalSearchScope scope = GlobalSearchScope.moduleWithLibrariesScope(currentModule);
-              final PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
-              if (aClass != null && editor != null) {
-                new AddImportAction(project, reference, editor, aClass).execute();
-              }
-            }
-            catch (ClassNotFoundException e) {
-              throw new RuntimeException(e);
-            }
-            finally {
-              DumbService.getInstance(project).setAlternativeResolveEnabled(false);
-            }
-          } else {
-            addBundledJarToRoots(project, editor, currentModule, reference, className, JavaSdkUtil.getJunit3JarPath());
+            jarPaths = getJUnit4JarPaths();
+            libraryName = JUNIT4_LIBRARY_NAME;
+          }
+          else {
+            jarPaths = Collections.singletonList(JavaSdkUtil.getJunit3JarPath());
+            libraryName = null;
           }
+          addJarsToRootsAndImportClass(jarPaths, libraryName, currentModule, editor, reference, className);
         }
       };
       registrar.register(fix);
@@ -183,7 +173,8 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
                 new WriteCommandAction(project) {
                   @Override
                   protected void run(final Result result) throws Throwable {
-                    addBundledJarToRoots(project, editor, currentModule, reference, "org.jetbrains.annotations." + referenceName, libraryPath);
+                    addJarsToRootsAndImportClass(Collections.singletonList(libraryPath), null, currentModule, editor, reference,
+                                                 "org.jetbrains.annotations." + referenceName);
                   }
                 }.execute();
               }
@@ -271,19 +262,20 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
   }
 
   public static void addJUnit4Library(boolean inTests, Module currentModule) throws ClassNotFoundException {
-    final String[] junit4Paths = {JavaSdkUtil.getJunit4JarPath(), 
-                                  PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matcher")), 
-                                  PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matchers"))};
-    ModuleRootModificationUtil.addModuleLibrary(currentModule,
-                                                "JUnit4",
-                                                ContainerUtil.map(junit4Paths,
-                                                                  new Function<String, String>() {
-                                                                    @Override
-                                                                    public String fun(String libPath) {
-                                                                      return convertToLibraryRoot(libPath).getUrl();
-                                                                    }
-                                                                  }),
-                                                Collections.<String>emptyList(), inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
+    final List<String> junit4Paths = getJUnit4JarPaths();
+    addJarsToRoots(junit4Paths, JUNIT4_LIBRARY_NAME, currentModule, null);
+  }
+
+  @NotNull
+  private static List<String> getJUnit4JarPaths() {
+    try {
+      return Arrays.asList(JavaSdkUtil.getJunit4JarPath(),
+                           PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matcher")),
+                           PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matchers")));
+    }
+    catch (ClassNotFoundException e) {
+      throw new RuntimeException(e);
+    }
   }
 
   private static List<PsiClass> filterAllowedDependencies(PsiElement element, PsiClass[] classes) {
@@ -322,29 +314,50 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
     return false;
   }
 
-  public static void addBundledJarToRoots(final Project project,
-                                          @Nullable final Editor editor,
-                                          final Module currentModule,
+  /**
+   * @deprecated use {@link #addJarsToRootsAndImportClass} instead
+   */
+  public static void addBundledJarToRoots(final Project project, @Nullable final Editor editor, final Module currentModule,
                                           @Nullable final PsiReference reference,
                                           @NonNls final String className,
                                           @NonNls final String libVirtFile) {
-    addJarToRoots(libVirtFile, currentModule, reference != null ? reference.getElement() : null);
+    addJarsToRootsAndImportClass(Collections.singletonList(libVirtFile), null, currentModule, editor, reference, className);
+  }
 
-    DumbService.getInstance(project).withAlternativeResolveEnabled(new Runnable() {
-      @Override
-      public void run() {
-        GlobalSearchScope scope = GlobalSearchScope.moduleWithLibrariesScope(currentModule);
-        PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
-        if (aClass != null && editor != null && reference != null) {
-          new AddImportAction(project, reference, editor, aClass).execute();
+  public static void addJarsToRootsAndImportClass(@NotNull List<String> jarPaths,
+                                                  final String libraryName,
+                                                  @NotNull final Module currentModule, @Nullable final Editor editor,
+                                                  @Nullable final PsiReference reference,
+                                                  @NonNls final String className) {
+    addJarsToRoots(jarPaths, libraryName, currentModule, reference != null ? reference.getElement() : null);
+
+    final Project project = currentModule.getProject();
+    if (editor != null && reference != null && className != null) {
+      DumbService.getInstance(project).withAlternativeResolveEnabled(new Runnable() {
+        @Override
+        public void run() {
+          GlobalSearchScope scope = GlobalSearchScope.moduleWithLibrariesScope(currentModule);
+          PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
+          if (aClass != null) {
+            new AddImportAction(project, reference, editor, aClass).execute();
+          }
         }
-      }
-    });
+      });
+    }
   }
 
-  public static void addJarToRoots(String libPath, final Module module, @Nullable PsiElement location) {
-    VirtualFile libVirtFile = convertToLibraryRoot(libPath);
+  public static void addJarToRoots(@NotNull String jarPath, final @NotNull Module module, @Nullable PsiElement location) {
+    addJarsToRoots(Collections.singletonList(jarPath), null, module, location);
+  }
 
+  public static void addJarsToRoots(@NotNull List<String> jarPaths, @Nullable String libraryName,
+                                    @NotNull Module module, @Nullable PsiElement location) {
+    List<String> urls = ContainerUtil.map(jarPaths, new Function<String, String>() {
+      @Override
+      public String fun(String path) {
+        return refreshAndConvertToUrl(path);
+      }
+    });
     boolean inTests = false;
     if (location != null) {
       final VirtualFile vFile = location.getContainingFile().getVirtualFile();
@@ -352,18 +365,15 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
         inTests = true;
       }
     }
-    ModuleRootModificationUtil.addModuleLibrary(module, null, Collections.singletonList(libVirtFile.getUrl()),
-                                                Collections.<String>emptyList(), inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
+    ModuleRootModificationUtil.addModuleLibrary(module, libraryName, urls, Collections.<String>emptyList(),
+                                                inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
   }
 
   @NotNull
-  private static VirtualFile convertToLibraryRoot(String libPath) {
-    final File libraryRoot = new File(libPath);
+  private static String refreshAndConvertToUrl(String jarPath) {
+    final File libraryRoot = new File(jarPath);
     LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libraryRoot);
-    String url = VfsUtil.getUrlForLibraryRoot(libraryRoot);
-    VirtualFile libVirtFile = VirtualFileManager.getInstance().findFileByUrl(url);
-    assert libVirtFile != null : libPath;
-    return libVirtFile;
+    return VfsUtil.getUrlForLibraryRoot(libraryRoot);
   }
 
   public static boolean ensureAnnotationsJarInPath(final Module module) {
index 2cfc9171e873a58bc03134afb36801b47f5f5337..c10ca6330ee389af833a72d03af421aeb1c105d5 100644 (file)
@@ -38,6 +38,8 @@ import net.jcip.annotations.GuardedBy;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 
+import java.util.Collections;
+
 public class JCiPOrderEntryFix implements IntentionAction {
   private static final Logger LOG = Logger.getInstance("#" + JCiPOrderEntryFix.class.getName());
 
@@ -79,8 +81,8 @@ public class JCiPOrderEntryFix implements IntentionAction {
     String jarPath = PathUtil.getJarPathForClass(GuardedBy.class);
     final VirtualFile virtualFile = file.getVirtualFile();
     LOG.assertTrue(virtualFile != null);
-    OrderEntryFix.addBundledJarToRoots(project, editor, ModuleUtil.findModuleForFile(virtualFile, project), reference,
-                                       "net.jcip.annotations." + reference.getReferenceName(), jarPath);
+    OrderEntryFix.addJarsToRootsAndImportClass(Collections.singletonList(jarPath), null, ModuleUtil.findModuleForFile(virtualFile, project), editor,
+                                               reference, "net.jcip.annotations." + reference.getReferenceName());
   }
 
   @Override
index a8fffa555bb2c2a2ddbbaa509d0328b21272a220..4e2321603db5aec3ef0abf824c34daa418af19ea 100644 (file)
@@ -19,7 +19,6 @@ import com.intellij.analysis.AnalysisScope;
 import com.intellij.analysis.AnalysisScopeBundle;
 import com.intellij.analysis.BaseAnalysisAction;
 import com.intellij.analysis.BaseAnalysisActionDialog;
-import com.intellij.codeInsight.AnnotationUtil;
 import com.intellij.codeInsight.FileModificationService;
 import com.intellij.codeInsight.NullableNotNullManager;
 import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
@@ -163,7 +162,7 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
                 @Override
                 protected void run(@NotNull final Result result) throws Throwable {
                   for (Module module : modulesWithoutAnnotations) {
-                    OrderEntryFix.addBundledJarToRoots(project, null, module, null, AnnotationUtil.NOT_NULL, path);
+                    OrderEntryFix.addJarsToRoots(Collections.singletonList(path), null, module, null);
                   }
                 }
               }.execute();
index 328e6e1e1cb9b2b8d76603c9fbdda7d0da773809..29c59ef15c9991625fad15ddf6ce4c6c8c0d1a67 100644 (file)
@@ -39,6 +39,8 @@ import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.testng.annotations.Test;
 
+import java.util.Collections;
+
 public class TestNGOrderEntryFix implements IntentionAction {
   private static final Logger LOG = Logger.getInstance("#" + TestNGOrderEntryFix.class.getName());
 
@@ -77,8 +79,8 @@ public class TestNGOrderEntryFix implements IntentionAction {
     String jarPath = PathUtil.getJarPathForClass(Test.class);
     final VirtualFile virtualFile = file.getVirtualFile();
     LOG.assertTrue(virtualFile != null);
-    OrderEntryFix.addBundledJarToRoots(project, editor, ModuleUtilCore.findModuleForFile(virtualFile, project), reference,
-                                       "org.testng.annotations." + reference.getReferenceName(), jarPath);
+    OrderEntryFix.addJarsToRootsAndImportClass(Collections.singletonList(jarPath), null, ModuleUtilCore.findModuleForFile(virtualFile, project),
+                                    editor, reference, "org.testng.annotations." + reference.getReferenceName());
   }
 
   public boolean startInWriteAction() {