'Add annotations to classpath' quickfix supported when IDEA is running from sources
[idea/community.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / JetBrainsAnnotationsExternalLibraryResolver.java
index e5b6a3c09e7dd442ea524a98b71e6c193d50cd60..28d031196eaa9355346b5a8b950eca7cb5c2a71e 100644 (file)
 package com.intellij.codeInsight.daemon.impl.quickfix;
 
 import com.intellij.codeInsight.AnnotationUtil;
-import com.intellij.codeInsight.daemon.quickFix.ExternalLibraryDescriptor;
 import com.intellij.codeInsight.daemon.quickFix.ExternalLibraryResolver;
+import com.intellij.openapi.application.PathManager;
+import com.intellij.openapi.module.EffectiveLanguageLevelUtil;
 import com.intellij.openapi.module.Module;
+import com.intellij.openapi.roots.ExternalLibraryDescriptor;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.pom.java.LanguageLevel;
 import com.intellij.util.ThreeState;
-import com.intellij.util.containers.ContainerUtil;
+import org.intellij.lang.annotations.Flow;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 /**
  * @author nik
  */
 public class JetBrainsAnnotationsExternalLibraryResolver extends ExternalLibraryResolver {
-  private static final ExternalLibraryDescriptor ANNOTATIONS = new ExternalLibraryDescriptor("com.intellij", "annotations", null) {
+  private static final ExternalLibraryDescriptor JAVA5 = new JetBrainsAnnotationsLibraryDescriptor() {
     @NotNull
     @Override
-    public List<String> locateLibraryClassesRoots(@NotNull Module contextModule) {
-      return ContainerUtil.createMaybeSingletonList(OrderEntryFix.locateAnnotationsJar(contextModule));
+    public List<String> getLibraryClassesRoots() {
+      File annotationsJar = new File(PathManager.getLibPath(), "annotations.jar");
+      if (annotationsJar.exists()) {
+        return Collections.singletonList(FileUtil.toSystemIndependentName(annotationsJar.getAbsolutePath()));
+      }
+      return getPathsToAnnotationsDirectoriesInDevelopmentMode("annotations");
     }
   };
 
+  private static final ExternalLibraryDescriptor JAVA8 = new JetBrainsAnnotationsLibraryDescriptor() {
+    @NotNull
+    @Override
+    public List<String> getLibraryClassesRoots() {
+      File annotationsJar = new File(PathManager.getHomePath(), "redist/annotations-java8.jar");
+      if (annotationsJar.exists()) {
+        return Collections.singletonList(FileUtil.toSystemIndependentName(annotationsJar.getAbsolutePath()));
+      }
+      return getPathsToAnnotationsDirectoriesInDevelopmentMode("annotations-java8");
+    }
+  };
+
+  @NotNull
+  private static List<String> getPathsToAnnotationsDirectoriesInDevelopmentMode(final String moduleName) {
+    final String annotationsRoot = PathManager.getJarPathForClass(Flow.class);
+    if (annotationsRoot == null) return Collections.emptyList();
+    return Arrays.asList(annotationsRoot, FileUtil.toSystemIndependentName(new File(new File(annotationsRoot).getParentFile(),
+                                                                                    moduleName).getAbsolutePath()));
+  }
+
   @Nullable
   @Override
-  public ExternalClassResolveResult resolveClass(@NotNull String shortClassName, @NotNull ThreeState isAnnotation) {
+  public ExternalClassResolveResult resolveClass(@NotNull String shortClassName, @NotNull ThreeState isAnnotation, @NotNull Module contextModule) {
     if (AnnotationUtil.isJetbrainsAnnotation(shortClassName)) {
-      return new ExternalClassResolveResult("org.jetbrains.annotations." + shortClassName, ANNOTATIONS);
+      ExternalLibraryDescriptor libraryDescriptor = getAnnotationsLibraryDescriptor(contextModule);
+      return new ExternalClassResolveResult("org.jetbrains.annotations." + shortClassName, libraryDescriptor);
     }
     return null;
   }
+
+  @NotNull
+  public static ExternalLibraryDescriptor getAnnotationsLibraryDescriptor(@NotNull Module contextModule) {
+    boolean java8 = EffectiveLanguageLevelUtil.getEffectiveLanguageLevel(contextModule).isAtLeast(LanguageLevel.JDK_1_8);
+    return java8 ? JAVA8 : JAVA5;
+  }
+
+  private static abstract class JetBrainsAnnotationsLibraryDescriptor extends ExternalLibraryDescriptor {
+    public JetBrainsAnnotationsLibraryDescriptor() {
+      super("com.intellij", "annotations", null);
+    }
+  }
 }