Skip to content

Commit

Permalink
map java types to kotlin types when parsing annotation class referenc…
Browse files Browse the repository at this point in the history
…e values
  • Loading branch information
neetopia committed Jun 5, 2024
1 parent 41914b3 commit 386fe47
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,11 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? {
if (this is FirAnnotation) {
return KtAnnotationApplicationValue(
KtAnnotationApplicationWithArgumentsInfo(
ClassId.fromString((annotationTypeRef.coneType as? ConeLookupTagBasedType)?.lookupTag.toString()),
JavaToKotlinClassMap.mapJavaToKotlinIncludingClassMapping(
ClassId.fromString(
(annotationTypeRef.coneType as? ConeLookupTagBasedType)?.lookupTag.toString()
).asSingleFqName()
),
null,
null,
emptyList(),
Expand All @@ -484,8 +488,9 @@ internal fun KtValueParameterSymbol.getDefaultValue(): KtAnnotationValue? {
}

if (coneType is ConeClassLikeType && coneType !is ConeErrorType) {
val classId = coneType.lookupTag.classId
val type = builder.typeBuilder.buildKtType(coneType)
val classId = JavaToKotlinClassMap
.mapJavaToKotlinIncludingClassMapping(coneType.lookupTag.classId.asSingleFqName())
val type = builder.typeBuilder.buildKtType(coneType).convertToKotlinType()
KtKClassAnnotationValue(type, classId, null, token)
} else {
null
Expand Down
20 changes: 20 additions & 0 deletions kotlin-analysis-api/testData/annotationValue/defaultKClassValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TEST PROCESSOR: DefaultKClassValueProcessor
// EXPECTED:
// kotlin.String
// kotlin.String
// kotlin.String
// kotlin.Int
// END
// MODULE: lib1
// FILE: lib1.kt
annotation class ExampleAnnotation(val value: kotlin.reflect.KClass<*> = java.lang.String::class)

// MODULE: main(lib1)
// FILE: a.kt

@ExampleAnnotation(String::class)
class Example

@ExampleAnnotation(Int::class)
class Example2

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.devtools.ksp.processor

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeImpl
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated

class DefaultKClassValueProcessor : AbstractTestProcessor() {
val results = mutableListOf<String>()

override fun toResult(): List<String> {
return results
}

override fun process(resolver: Resolver): List<KSAnnotated> {
val example1 = resolver.getClassDeclarationByName("Example")!!.annotations.first()
val example2 = resolver.getClassDeclarationByName("Example2")!!.annotations.first()
val arg1 = (example1.arguments.single().value as KSTypeImpl).declaration.qualifiedName!!
val defaultArg1 = (example1.defaultArguments.single().value as KSTypeImpl).declaration.qualifiedName!!
results.add(defaultArg1.asString())
results.add(arg1.asString())
val arg2 = (example2.arguments.single().value as KSTypeImpl).declaration.qualifiedName!!
val defaultArg2 = (example2.defaultArguments.single().value as KSTypeImpl).declaration.qualifiedName!!
results.add(defaultArg2.asString())
results.add(arg2.asString())
return emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class KSPAATest : AbstractKSPAATest() {
runTest("../test-utils/testData/api/annotationWithArbitraryClassValue.kt")
}

@TestMetadata("defaultKClassValue.kt")
@Test
fun testAnnotationValue_defaultKClassValue() {
runTest("../kotlin-analysis-api/testData/annotationValue/defaultKClassValue.kt")
}

@TestMetadata("annotationValue_java.kt")
@Test
fun testAnnotationValue_java() {
Expand Down

0 comments on commit 386fe47

Please sign in to comment.