Skip to content

Commit d27912e

Browse files
ting-yuanKSP Auto Pick
authored and
KSP Auto Pick
committed
KSP2: reimplement Resolver.getDeclarationsFromPackage
(cherry picked from commit bca0c74)
1 parent d96c00d commit d27912e

File tree

4 files changed

+91
-18
lines changed

4 files changed

+91
-18
lines changed

kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt

+9-18
Original file line numberDiff line numberDiff line change
@@ -279,25 +279,16 @@ class ResolverAAImpl(
279279
@KspExperimental
280280
override fun getDeclarationsFromPackage(packageName: String): Sequence<KSDeclaration> {
281281
return analyze {
282-
val packageNames = FqName(packageName).pathSegments().map { it.asString() }
283-
var packages = listOf(useSiteSession.rootPackageSymbol)
284-
for (curName in packageNames) {
285-
packages = packages
286-
.flatMap { it.packageScope.getPackageSymbols { it.asString() == curName } }
287-
.distinct()
288-
}
289-
packages.flatMap {
290-
it.packageScope.declarations.distinct().mapNotNull { symbol ->
291-
when (symbol) {
292-
is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(symbol)
293-
is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(symbol)
294-
is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol)
295-
is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol)
296-
is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol)
297-
else -> null
298-
}
282+
findPackage(FqName(packageName))?.packageScope?.declarations?.distinct()?.mapNotNull { symbol ->
283+
when (symbol) {
284+
is KaNamedClassSymbol -> KSClassDeclarationImpl.getCached(symbol)
285+
is KaFunctionSymbol -> KSFunctionDeclarationImpl.getCached(symbol)
286+
is KaPropertySymbol -> KSPropertyDeclarationImpl.getCached(symbol)
287+
is KaTypeAliasSymbol -> KSTypeAliasImpl.getCached(symbol)
288+
is KaJavaFieldSymbol -> KSPropertyDeclarationJavaImpl.getCached(symbol)
289+
else -> null
299290
}
300-
}.asSequence()
291+
}?.asSequence() ?: emptySequence()
301292
}
302293
}
303294

kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,10 @@ class KSPAATest : AbstractKSPAATest() {
714714
fun testExitCode() {
715715
runTest("../test-utils/testData/api/exitCode.kt")
716716
}
717+
718+
@TestMetadata("packageProviderForGenerated.kt")
719+
@Test
720+
fun testPackageProviderForGenerated() {
721+
runTest("../test-utils/testData/api/packageProviderForGenerated.kt")
722+
}
717723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.google.devtools.ksp.processor
2+
3+
import com.google.devtools.ksp.KspExperimental
4+
import com.google.devtools.ksp.processing.Dependencies
5+
import com.google.devtools.ksp.processing.Resolver
6+
import com.google.devtools.ksp.processing.SymbolProcessor
7+
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
8+
import com.google.devtools.ksp.symbol.KSAnnotated
9+
10+
class PackageProviderForGeneratedProcessor : AbstractTestProcessor() {
11+
val result = mutableListOf<String>()
12+
override fun toResult(): List<String> {
13+
return result
14+
}
15+
16+
var round = 0
17+
18+
@OptIn(KspExperimental::class)
19+
override fun process(resolver: Resolver): List<KSAnnotated> {
20+
if (round == 0) {
21+
createJavaFile("foo.bar", "MyGeneratedJavaClass")
22+
}
23+
result.add("Processing round $round")
24+
val getByName = resolver.getClassDeclarationByName(
25+
resolver.getKSNameFromString("foo.bar.MyGeneratedJavaClass")
26+
)?.qualifiedName?.asString()
27+
result.add("resolver.getClassDeclarationByName: $getByName")
28+
val getByPackage = resolver.getDeclarationsFromPackage("foo.bar").toList().map { it.qualifiedName?.asString() }
29+
result.add("resolver.getDeclarationsFromPackage: $getByPackage")
30+
round++
31+
return emptyList()
32+
}
33+
34+
lateinit var env: SymbolProcessorEnvironment
35+
36+
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
37+
env = environment
38+
return this
39+
}
40+
41+
private fun createJavaFile(packageName: String, className: String) {
42+
val dependencies = Dependencies(aggregating = false, sources = arrayOf())
43+
env.codeGenerator.createNewFile(dependencies, packageName, className, "java").use {
44+
it.write("package foo.bar;\nclass MyGeneratedJavaClass{}".toByteArray())
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// TEST PROCESSOR: PackageProviderForGeneratedProcessor
19+
// EXPECTED:
20+
// Processing round 0
21+
// resolver.getClassDeclarationByName: null
22+
// resolver.getDeclarationsFromPackage: []
23+
// Processing round 1
24+
// resolver.getClassDeclarationByName: foo.bar.MyGeneratedJavaClass
25+
// resolver.getDeclarationsFromPackage: [foo.bar.MyGeneratedJavaClass]
26+
// END
27+
28+
// FILE: Dummy.kt
29+
class Dummy

0 commit comments

Comments
 (0)