Skip to content

Commit eaf9f7a

Browse files
neetopiaKSP Auto Pick
authored and
KSP Auto Pick
committed
map java types to kotlin types when parsing annotation class reference values
(cherry picked from commit 386fe47)
1 parent 893a784 commit eaf9f7a

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,11 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? {
461461
if (this is FirAnnotation) {
462462
return KtAnnotationApplicationValue(
463463
KtAnnotationApplicationWithArgumentsInfo(
464-
ClassId.fromString((annotationTypeRef.coneType as? ConeLookupTagBasedType)?.lookupTag.toString()),
464+
JavaToKotlinClassMap.mapJavaToKotlinIncludingClassMapping(
465+
ClassId.fromString(
466+
(annotationTypeRef.coneType as? ConeLookupTagBasedType)?.lookupTag.toString()
467+
).asSingleFqName()
468+
),
465469
null,
466470
null,
467471
emptyList(),
@@ -484,8 +488,9 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? {
484488
}
485489

486490
if (coneType is ConeClassLikeType && coneType !is ConeErrorType) {
487-
val classId = coneType.lookupTag.classId
488-
val type = builder.typeBuilder.buildKtType(coneType)
491+
val classId = JavaToKotlinClassMap
492+
.mapJavaToKotlinIncludingClassMapping(coneType.lookupTag.classId.asSingleFqName())
493+
val type = builder.typeBuilder.buildKtType(coneType).convertToKotlinType()
489494
KtKClassAnnotationValue(type, classId, null, token)
490495
} else {
491496
null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TEST PROCESSOR: DefaultKClassValueProcessor
2+
// EXPECTED:
3+
// kotlin.String
4+
// kotlin.String
5+
// kotlin.String
6+
// kotlin.Int
7+
// END
8+
// MODULE: lib1
9+
// FILE: lib1.kt
10+
annotation class ExampleAnnotation(val value: kotlin.reflect.KClass<*> = java.lang.String::class)
11+
12+
// MODULE: main(lib1)
13+
// FILE: a.kt
14+
15+
@ExampleAnnotation(String::class)
16+
class Example
17+
18+
@ExampleAnnotation(Int::class)
19+
class Example2
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.google.devtools.ksp.processor
2+
3+
import com.google.devtools.ksp.getClassDeclarationByName
4+
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeImpl
5+
import com.google.devtools.ksp.processing.Resolver
6+
import com.google.devtools.ksp.symbol.KSAnnotated
7+
8+
class DefaultKClassValueProcessor : AbstractTestProcessor() {
9+
val results = mutableListOf<String>()
10+
11+
override fun toResult(): List<String> {
12+
return results
13+
}
14+
15+
override fun process(resolver: Resolver): List<KSAnnotated> {
16+
val example1 = resolver.getClassDeclarationByName("Example")!!.annotations.first()
17+
val example2 = resolver.getClassDeclarationByName("Example2")!!.annotations.first()
18+
val arg1 = (example1.arguments.single().value as KSTypeImpl).declaration.qualifiedName!!
19+
val defaultArg1 = (example1.defaultArguments.single().value as KSTypeImpl).declaration.qualifiedName!!
20+
results.add(defaultArg1.asString())
21+
results.add(arg1.asString())
22+
val arg2 = (example2.arguments.single().value as KSTypeImpl).declaration.qualifiedName!!
23+
val defaultArg2 = (example2.defaultArguments.single().value as KSTypeImpl).declaration.qualifiedName!!
24+
results.add(defaultArg2.asString())
25+
results.add(arg2.asString())
26+
return emptyList()
27+
}
28+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class KSPAATest : AbstractKSPAATest() {
8484
runTest("../test-utils/testData/api/annotationWithArbitraryClassValue.kt")
8585
}
8686

87+
@TestMetadata("defaultKClassValue.kt")
88+
@Test
89+
fun testAnnotationValue_defaultKClassValue() {
90+
runTest("../kotlin-analysis-api/testData/annotationValue/defaultKClassValue.kt")
91+
}
92+
8793
@TestMetadata("annotationValue_java.kt")
8894
@Test
8995
fun testAnnotationValue_java() {

0 commit comments

Comments
 (0)