don't parse non-stubbed java files in library classes
authorpeter <peter@jetbrains.com>
Tue, 14 Aug 2012 18:38:20 +0000 (20:38 +0200)
committerpeter <peter@jetbrains.com>
Tue, 14 Aug 2012 18:38:20 +0000 (20:38 +0200)
java/java-psi-impl/src/com/intellij/core/CoreJavaDirectoryService.java
java/java-tests/testData/libResolve/classesAndSources/classesAndSources.jar [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/psi/resolve/ResolveInLibrariesTest.groovy

index 23ce0c9ce05491c061895cd60e0f88633f9befde..b07f20df3b5a8b32d9e1c7c4bcf4c35c7fa2d825 100644 (file)
@@ -17,8 +17,10 @@ package com.intellij.core;
 
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.roots.FileIndexFacade;
 import com.intellij.pom.java.LanguageLevel;
 import com.intellij.psi.*;
+import com.intellij.psi.impl.compiled.ClsFileImpl;
 import com.intellij.util.IncorrectOperationException;
 import com.intellij.util.containers.ContainerUtil;
 import org.jetbrains.annotations.NotNull;
@@ -42,8 +44,13 @@ public class CoreJavaDirectoryService extends JavaDirectoryService {
   public PsiClass[] getClasses(@NotNull PsiDirectory dir) {
     LOG.assertTrue(dir.isValid());
 
+    boolean onlyCompiled = FileIndexFacade.getInstance(dir.getProject()).isInLibraryClasses(dir.getVirtualFile());
+
     List<PsiClass> classes = null;
     for (PsiFile file : dir.getFiles()) {
+      if (onlyCompiled && !(file instanceof ClsFileImpl)) {
+        continue;
+      }
       if (file instanceof PsiClassOwner && file.getViewProvider().getLanguages().size() == 1) {
         PsiClass[] psiClasses = ((PsiClassOwner)file).getClasses();
         if (psiClasses.length == 0) continue;
diff --git a/java/java-tests/testData/libResolve/classesAndSources/classesAndSources.jar b/java/java-tests/testData/libResolve/classesAndSources/classesAndSources.jar
new file mode 100644 (file)
index 0000000..b4cf119
Binary files /dev/null and b/java/java-tests/testData/libResolve/classesAndSources/classesAndSources.jar differ
index 4d7d3930bbe771517f2b2b09f878ff8bd9bb01ba..2c24ba632bb7456377537688e504e011ad54580c 100644 (file)
 package com.intellij.psi.resolve
 import com.intellij.openapi.application.ex.PathManagerEx
 import com.intellij.openapi.vfs.LocalFileSystem
+import com.intellij.openapi.vfs.VirtualFile
 import com.intellij.psi.JavaPsiFacade
 import com.intellij.psi.PsiClass
+import com.intellij.psi.PsiFile
 import com.intellij.psi.PsiMethod
 import com.intellij.psi.search.GlobalSearchScope
 import com.intellij.psi.search.searches.ClassInheritorsSearch
@@ -132,4 +134,22 @@ class ResolveInLibrariesTest extends JavaCodeInsightFixtureTestCase {
   private PsiMethod fooMethod(PsiClass c) { c.findMethodsByName('foo', false)[0] }
   private Set<PsiMethod> fooInheritors(PsiClass c) { OverridingMethodsSearch.search(fooMethod(c)).findAll() as Set }
 
+  public void "test do not parse not stubbed sources in class jars"() {
+    def lib = LocalFileSystem.getInstance().refreshAndFindFileByPath(PathManagerEx.getTestDataPath() + "/libResolve/classesAndSources")
+    PsiTestUtil.addLibrary(myModule, 'cas', lib.path, ["/classesAndSources.jar!/"] as String[], ["/classesAndSources.jar!/"] as String[])
+
+    def facade = JavaPsiFacade.getInstance(project)
+    def scope = GlobalSearchScope.allScope(project)
+
+    assert facade.findClasses('LibraryClass', scope).size() == 1
+
+    def pkg = facade.findPackage("")
+    assert pkg.classes.size() == 1
+
+    Collection<VirtualFile> pkgDirs = pkg.directories.collect { it.virtualFile }
+    Collection<VirtualFile> pkgChildren = pkgDirs.collect { it.children as List }.flatten()
+    PsiFile javaSrc = psiManager.findFile(pkgChildren.find { it.name == 'LibraryClass.java' })
+    assert !javaSrc.node.parsed
+  }
+
 }