quickfix adds annotations-java8.jar to classpath if language level permits (IDEA...
authornik <Nikolay.Chashnikov@jetbrains.com>
Thu, 28 May 2015 11:04:42 +0000 (14:04 +0300)
committernik <Nikolay.Chashnikov@jetbrains.com>
Thu, 28 May 2015 12:23:49 +0000 (15:23 +0300)
java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java
java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java

index 589d5176ece3e440e8a2b170ca191b106e725af8..2edfa99762892b661bf31ab7015c4e70e69fc983 100644 (file)
@@ -27,6 +27,7 @@ import com.intellij.openapi.application.PathManager;
 import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.module.EffectiveLanguageLevelUtil;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.DumbService;
 import com.intellij.openapi.project.Project;
@@ -39,6 +40,7 @@ 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.*;
 import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.search.PsiShortNamesCache;
@@ -176,14 +178,12 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
           ApplicationManager.getApplication().invokeLater(new Runnable() {
             @Override
             public void run() {
-              final LocateLibraryDialog dialog = new LocateLibraryDialog(currentModule, PathManager.getLibPath(), "annotations.jar",
-                                                                         QuickFixBundle.message("add.library.annotations.description"));
-              if (dialog.showAndGet()) {
+              final String libraryPath = locateAnnotationsJar(currentModule);
+              if (libraryPath != null) {
                 new WriteCommandAction(project) {
                   @Override
                   protected void run(final Result result) throws Throwable {
-                    addBundledJarToRoots(project, editor, currentModule, reference, "org.jetbrains.annotations." + referenceName,
-                                         dialog.getResultingLibraryPath());
+                    addBundledJarToRoots(project, editor, currentModule, reference, "org.jetbrains.annotations." + referenceName, libraryPath);
                   }
                 }.execute();
               }
@@ -372,14 +372,12 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
   public static boolean ensureAnnotationsJarInPath(final Module module) {
     if (isAnnotationsJarInPath(module)) return true;
     if (module == null) return false;
-    final LocateLibraryDialog dialog = new LocateLibraryDialog(
-      module, PathManager.getLibPath(), "annotations.jar",
-      QuickFixBundle.message("add.library.annotations.description"));
-    if (dialog.showAndGet()) {
+    final String libraryPath = locateAnnotationsJar(module);
+    if (libraryPath != null) {
       new WriteCommandAction(module.getProject()) {
         @Override
         protected void run(final Result result) throws Throwable {
-          addJarToRoots(dialog.getResultingLibraryPath(), module, null);
+          addJarToRoots(libraryPath, module, null);
         }
       }.execute();
       return true;
@@ -387,6 +385,13 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
     return false;
   }
 
+  @Nullable
+  public static String locateAnnotationsJar(@NotNull Module module) {
+    String jarName = EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(module).isAtLeast(LanguageLevel.JDK_1_8) ? "annotations-java8.jar" : "annotations.jar";
+    final LocateLibraryDialog dialog = new LocateLibraryDialog(module, PathManager.getLibPath(), jarName, QuickFixBundle.message("add.library.annotations.description"));
+    return dialog.showAndGet() ? dialog.getResultingLibraryPath() : null;
+  }
+
   public static boolean isAnnotationsJarInPath(Module module) {
     if (module == null) return false;
     return JavaPsiFacade.getInstance(module.getProject())
index 1d5e6a50df3ec9f1b9f0af7dca7d59e2604bdce9..c9714e65326eec29e38f4f260d5cbec96dcd2555 100644 (file)
@@ -22,14 +22,11 @@ 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.QuickFixBundle;
-import com.intellij.codeInsight.daemon.impl.quickfix.LocateLibraryDialog;
 import com.intellij.codeInsight.daemon.impl.quickfix.OrderEntryFix;
 import com.intellij.history.LocalHistory;
 import com.intellij.history.LocalHistoryAction;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.application.PathManager;
 import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.editor.Document;
@@ -160,11 +157,8 @@ public class InferNullityAnnotationsAction extends BaseAnalysisAction {
         ApplicationManager.getApplication().invokeLater(new Runnable() {
           @Override
           public void run() {
-            final LocateLibraryDialog dialog =
-              new LocateLibraryDialog(modulesWithoutAnnotations.iterator().next(), PathManager.getLibPath(), "annotations.jar",
-                                      QuickFixBundle.message("add.library.annotations.description"));
-            if (dialog.showAndGet()) {
-              final String path = dialog.getResultingLibraryPath();
+            final String path = OrderEntryFix.locateAnnotationsJar(modulesWithoutAnnotations.iterator().next());
+            if (path != null) {
               new WriteCommandAction(project) {
                 @Override
                 protected void run(@NotNull final Result result) throws Throwable {