Hello Team, I have a doubt about annotation parame...
# ksp
j
Hello Team, I have a doubt about annotation parameters. I have an annotation
@Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE)
annotation class GenerateDI( val moduleType: ModuleType, val superType: KClass<*> = Unit::class)
@GenerateDI(ModuleType.ACTIVITY, BaseClass::class)
class SampleClass : BaseClass
(moduleType is a enum class) generateDIAnnotation?.arguments?.get(0)?.value.toString() I used this line of code to get the param value as string. But is there any other way to type cast value? When i tried to type cast it shows an error
val newType = generateDIAnnotation?.arguments?.get(0)?.value as ModuleType
[ksp]java.lang.ClassCastException: class com.google.devtools.ksp.symbol.impl.kotlin.KSTypeImpl cannot be cast to class com.xxx.yyy.util.ModuleType
Is there any another way to get argument values. or getting as string values is the only option.
d
does this Stack Overflow answer help? https://stackoverflow.com/a/73840958/5241933
j
Yes, it helped somewhat, but the problem still persists.
listedClasses._map_ *{ it: KSAnnotated ->*
val moduleType = *it*._getAnnotationsByType_(GenerateDI::class)._first_().moduleType
val superType = try {
*it*._getAnnotationsByType_(GenerateDI::class)._first_().superType
null
} catch (e : KSTypeNotPresentException) {
e.ksType.declaration as KSClassDeclaration
}
}
The
moduleType
property, which is an enum class, can be accessed without any error. However, when attempting to access
superType
, which is of type
KClass<*>
, it doesn’t work properly. When I tried to retrieve the supertype using
it.getAnnotationsByType(GenerateDI::class).first().superType
, I encountered this error.
[ksp] com.google.devtools.ksp.KSTypeNotPresentException:
java.lang.ClassNotFoundException: com.xxx.yyy.db.BaseClass at com.google.devtools.ksp.UtilsKt.asClass(utils.kt:509)
at com.google.devtools.ksp.UtilsKt.createInvocationHandler$lambda-8(utils.kt:385)
All i need is just Super Type Name with its package. Currently i can get name of the class by this line of code
e.ksType.declaration as KSClassDeclaration
But i can’t get the package of that Super Type. when i tried to get
ksType.declaration.packageName
I got like this com.google.devtools.ksp.symbol.impl.kotlin.KSNameImpl@48f08bbe
superType.qualifiedName!!.asString()
😄 This line of code solved my problem. Earlier, I used
toString()
instead of
asString()
. Thanks for replying, @David Rawson