fixup! Java facade: add missing ClassId check in findClass
diff --git a/compiler/java-direct/src/org/jetbrains/kotlin/java/direct/JavaPackageIndexer.kt b/compiler/java-direct/src/org/jetbrains/kotlin/java/direct/JavaPackageIndexer.kt index 0d3d447..85f68df 100644 --- a/compiler/java-direct/src/org/jetbrains/kotlin/java/direct/JavaPackageIndexer.kt +++ b/compiler/java-direct/src/org/jetbrains/kotlin/java/direct/JavaPackageIndexer.kt
@@ -91,25 +91,6 @@ } } - // Top-level `.java` files of each directory root that declare a non-root package: register - // them under their declared package so they're discoverable even when the disk path does - // not mirror the package. This covers the test infrastructure case without implementing - // full scan for cases when file path does not match package structure. - for (dirRootEntry in dirRoots) { - val dirRoot = dirRootEntry.root - for (file in dirRoot.listFiles() ?: continue) { - if (file.isDirectory) continue - if (!file.name.endsWith(".java")) continue - if (file.name == "package-info.java") continue - val entry = tryBuildFileEntry(file) ?: continue - if (entry.packageFqName.isRoot) continue - val classesByName = fileRootIndexBuilder.getOrPut(entry.packageFqName) { HashMap() } - for (className in entry.topLevelClassNames) { - classesByName.getOrPut(className) { mutableListOf() }.add(entry) - } - } - } - fileRootIndex = fileRootIndexBuilder }
diff --git a/compiler/java-direct/test/org/jetbrains/kotlin/java/direct/JavaParsingClassFinderTest.kt b/compiler/java-direct/test/org/jetbrains/kotlin/java/direct/JavaParsingClassFinderTest.kt index 300a130..55b0e0b 100644 --- a/compiler/java-direct/test/org/jetbrains/kotlin/java/direct/JavaParsingClassFinderTest.kt +++ b/compiler/java-direct/test/org/jetbrains/kotlin/java/direct/JavaParsingClassFinderTest.kt
@@ -671,10 +671,11 @@ } @Test - fun testWrongPackageFileAtDirectoryRoot(@TempDir tempDir: Path) { - // A.java declares `package foo;` but lives directly under the directory root (not in - // a `foo/` subdirectory). The class should be findable by its declared package ClassId - // but NOT by the root package. + fun testWrongPackageFileAtDirectoryRootIsNotFound(@TempDir tempDir: Path) { + // A.java declares `package foo;` but lives directly under the directory root (not in a + // `foo/` subdirectory). Under a directory root the package is derived from the path, so + // neither ClassId resolves — javac and PSI (the CLI `javaSrcWrongPackage` fixture, KT-11474) + // report the same. val aFile = tempDir.resolve("A.java") aFile.writeText( """ @@ -687,19 +688,44 @@ val finder = JavaClassFinderOverAstImpl(listOf(tempDir.toFile())) - // Findable by declared package (foo.A) + val fooAId = ClassId(FqName("foo"), Name.identifier("A")) + assertFalse(finder.isClassInIndex(fooAId), "foo.A must NOT be in index for a directory root") + assertNull(finder.findClass(JavaClassFinder.Request(fooAId)), "foo.A must NOT be findable for a directory root") + + val rootAId = ClassId(FqName.ROOT, Name.identifier("A")) + assertFalse(finder.isClassInIndex(rootAId), "Root-package A must NOT be in index") + assertNull(finder.findClass(JavaClassFinder.Request(rootAId)), "Root-package A must NOT be findable") + + val nestedId = ClassId(FqName("foo"), FqName("A.Nested"), isLocal = false) + assertNull(finder.findClass(JavaClassFinder.Request(nestedId)), "foo.A.Nested must NOT be findable") + } + + @Test + fun testWrongPackageFileAsSingleFileRootIsFoundByDeclaredPackage(@TempDir tempDir: Path) { + // Same file as above, passed as a single-file source root: there is no path to derive the + // package from, so the declared package wins, as in PSI's `SingleJavaFileRootsIndex`. + val aFile = tempDir.resolve("A.java") + aFile.writeText( + """ + package foo; + public class A { + public static class Nested {} + } + """.trimIndent() + ) + + val finder = JavaClassFinderOverAstImpl(listOf(aFile.toFile())) + val fooAId = ClassId(FqName("foo"), Name.identifier("A")) assertTrue(finder.isClassInIndex(fooAId), "Expected foo.A to be in index") val fooA = finder.findClass(JavaClassFinder.Request(fooAId)) assertNotNull(fooA, "Expected to find foo.A by declared package") assertEquals("A", fooA.name.asString()) - // NOT findable by root package (A without package) val rootAId = ClassId(FqName.ROOT, Name.identifier("A")) assertFalse(finder.isClassInIndex(rootAId), "Root-package A must NOT be in index") assertNull(finder.findClass(JavaClassFinder.Request(rootAId)), "Root-package A must NOT be findable") - // Inner class of foo.A should be findable val nestedId = ClassId(FqName("foo"), FqName("A.Nested"), isLocal = false) val nested = finder.findClass(JavaClassFinder.Request(nestedId)) assertNotNull(nested, "Expected to find foo.A.Nested")
diff --git a/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.kt b/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.kt index 581c3c8e..2da9cb2 100644 --- a/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.kt +++ b/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.kt
@@ -3,7 +3,7 @@ // LANGUAGE: +UnnamedLocalVariables +NameBasedDestructuring // LATEST_LV_DIFFERENCE -// FILE: JavaUtils.java +// FILE: test/JavaUtils.java package test;
diff --git a/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.latestLV.kt b/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.latestLV.kt index 7c6d99d..c1acbe5 100644 --- a/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.latestLV.kt +++ b/compiler/testData/diagnostics/tests/unnamedLocalVariables/withUnitType.latestLV.kt
@@ -3,7 +3,7 @@ // LANGUAGE: +UnnamedLocalVariables +NameBasedDestructuring // LATEST_LV_DIFFERENCE -// FILE: JavaUtils.java +// FILE: test/JavaUtils.java package test;