'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 02ce2d391cf7eb0a80ff4da6e7e51e7a160734ea..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 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;
 
@@ -39,17 +41,34 @@ public class JetBrainsAnnotationsExternalLibraryResolver extends ExternalLibrary
     @NotNull
     @Override
     public List<String> getLibraryClassesRoots() {
-      return Collections.singletonList(FileUtil.toSystemIndependentName(new File(PathManager.getLibPath(), "annotations.jar").getAbsolutePath()));
+      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() {
-      return Collections.singletonList(FileUtil.toSystemIndependentName(new File(PathManager.getHomePath(), "redist/annotations-java8.jar").getAbsolutePath()));
+      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, @NotNull Module contextModule) {