From 2340260ddac8cc1e5e2093ff4e6d7bf5ccbae134 Mon Sep 17 00:00:00 2001 From: nik Date: Thu, 28 May 2015 14:04:42 +0300 Subject: [PATCH] quickfix adds annotations-java8.jar to classpath if language level permits (IDEA-123048) --- .../daemon/impl/quickfix/OrderEntryFix.java | 25 +++++++++++-------- .../InferNullityAnnotationsAction.java | 10 ++------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java index 589d5176ece3..2edfa9976289 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java @@ -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()) diff --git a/java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java b/java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java index 1d5e6a50df3e..c9714e65326e 100644 --- a/java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java +++ b/java/java-impl/src/com/intellij/codeInspection/inferNullity/InferNullityAnnotationsAction.java @@ -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 {