Lamberto Basti
01/17/2023, 4:52 PMKClass
out from an annotation argument?
Given the annotation:
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.PROPERTY)
annotation class Reference(val table: KClass<out Table>, val columnName: String)
// usage
@Reference(MyTable::class, "id")
I am trying to preocess it with:
val reference = kSPropertyDeclaration.getAnnotationsByType(Reference::class).first()
But when i access reference.table
it errors with:
[ksp] com.google.devtools.ksp.KSTypeNotPresentException: java.lang.ClassNotFoundException: com.github.lamba92.ktor.restrepositories.tests.Cities
at com.google.devtools.ksp.UtilsKt.asClass(utils.kt:509)
at com.google.devtools.ksp.UtilsKt.createInvocationHandler$lambda$8(utils.kt:385)
at jdk.proxy5/jdk.proxy5.$Proxy29.table(Unknown Source)
I believe the returned class has some kind of lazy behaviour because it errors only when accessing table
, but it is kinda weird.
Also, com.github.lamba92.ktor.restrepositories.tests.Cities
is one of the source files.Lamberto Basti
01/17/2023, 4:54 PMjw
01/17/2023, 5:06 PMjw
01/17/2023, 5:08 PMjw
01/17/2023, 5:09 PMLamberto Basti
01/17/2023, 5:09 PMjw
01/17/2023, 5:12 PMjw
01/17/2023, 5:13 PMdeclaration
is a KSClassDecalaration
it'll have qualifiedName
Lamberto Basti
01/17/2023, 5:29 PMpropertyDeclaration.annotations
.firstOrNull {
it.annotationType.resolve().toClassName() == Reference::class.asClassName()
}
?.arguments
?.first()
?.value // <-- this is type Any?
?.let { it as? KSType }
?.declaration
?.qualifiedName
?.asString()
So basically, somehow, automagically, the KSP runtime fills value
with a KSType
. I had to print out quite some stuff before figuring that out!Jiaxiang
01/17/2023, 6:25 PMKClass
as class literals, KSP will try to convert it to KSType
, we should probably note it in documents.