Skip to content

Commit f03230f

Browse files
committed
If a ClassIndex ignores a root due to missing index, make sure the ClassIndex is rebuilt when the index is created.
1 parent 1ddc32f commit f03230f

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

java/java.source.base/src/org/netbeans/api/java/source/ClassIndex.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ private void createQueriesForRoots (final ClassPath cp, final boolean sources, f
830830
if (ci != null) {
831831
ci.addClassIndexImplListener(spiListener);
832832
queries.add (ci);
833+
} else {
834+
spiListener.attachClassIndexManagerListener();
833835
}
834836
}
835837
}

java/java.source.base/test/unit/src/org/netbeans/api/java/source/ClassIndexTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import javax.lang.model.element.ElementKind;
3232
import javax.lang.model.element.TypeElement;
3333
import javax.swing.event.ChangeListener;
34+
import javax.tools.JavaFileObject;
35+
import javax.tools.ToolProvider;
3436
import org.netbeans.api.java.classpath.ClassPath;
3537
import org.netbeans.api.java.classpath.GlobalPathRegistry;
3638
import org.netbeans.api.java.platform.JavaPlatformManager;
@@ -92,6 +94,7 @@ public static NbTestSuite suite() {
9294
suite.addTest(new ClassIndexTest("testPackageUsages")); //NOI18N
9395
suite.addTest(new ClassIndexTest("testNullRootPassedToClassIndexEvent")); //NOI18N
9496
suite.addTest(new ClassIndexTest("testFindSymbols")); //NOI18N
97+
suite.addTest(new ClassIndexTest("testQueryIndexRefreshQueryAgain")); //NOI18N
9598
return suite;
9699
}
97100

@@ -576,6 +579,55 @@ public void testFindSymbols() throws Exception {
576579
assertEquals(new HashSet<String>(Arrays.asList("test.foo:[foo]", "test.Test:[foo]")), actualResult);
577580
}
578581

582+
public void testQueryIndexRefreshQueryAgain() throws Exception {
583+
final FileObject wd = FileUtil.toFileObject(getWorkDir());
584+
final FileObject root = FileUtil.createFolder(wd,"src"); //NOI18N
585+
final FileObject classes = FileUtil.createFolder(wd,"classes"); //NOI18N
586+
sourcePath = ClassPathSupport.createClassPath(root);
587+
final FileObject t1 = createJavaFile(
588+
root,
589+
"org.me.test", //NOI18N
590+
"T1", //NOI18N
591+
"package org.me.test;\n"+ //NOI18N
592+
"public class T1 extends java.util.ArrayList {}"); //NOI18N
593+
//compile binary dependency:
594+
JavaFileObject libraryJFO =
595+
FileObjects.memoryFileObject("lib",
596+
"TestLib.java",
597+
"""
598+
package lib;
599+
public class TestLib {}
600+
""");
601+
ToolProvider.getSystemJavaCompiler()
602+
.getTask(null,
603+
null,
604+
null,
605+
List.of("-d",
606+
FileUtil.toFile(classes).getAbsolutePath()),
607+
null,
608+
List.of(libraryJFO))
609+
.call();
610+
611+
compilePath = ClassPathSupport.createClassPath(classes);
612+
bootPath = JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries();
613+
614+
final ClassIndex ci = ClasspathInfo.create(bootPath, compilePath, sourcePath).getClassIndex();
615+
Set<ElementHandle<TypeElement>> result;
616+
result = ci.getDeclaredTypes("TestLib", NameKind.PREFIX, Set.of(ClassIndex.SearchScope.DEPENDENCIES));
617+
assertElementHandles(new String[] {}, result);
618+
619+
GlobalPathRegistry.getDefault().register(ClassPath.BOOT, new ClassPath[] {bootPath});
620+
GlobalPathRegistry.getDefault().register(ClassPath.COMPILE, new ClassPath[] {compilePath});
621+
GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, new ClassPath[] {sourcePath});
622+
623+
IndexingManager.getDefault().refreshAllIndices(true, true, root);
624+
SourceUtils.waitScanFinished();
625+
626+
result = ci.getDeclaredTypes("TestLib", NameKind.PREFIX, Set.of(ClassIndex.SearchScope.DEPENDENCIES));
627+
assertNotNull(result);
628+
assertElementHandles(new String[] {"lib.TestLib"}, result);
629+
}
630+
579631
private FileObject createJavaFile (
580632
final FileObject root,
581633
final String pkg,

0 commit comments

Comments
 (0)