Using reflection, I am examining the constructor parameters of a Kotlin class. I need to determine the class (KClass
) of each parameter and to take appropriate action:
for(consP in primaryConstructor.parameters) {
when(consP.type) {
is kotlin.String -> // do String thing;
is kotlin.Int -> // do Int thing }
}
Unfortunately, the closest I’ve found uses jvmErasure
and gives me just the qualified name of the parameter:
when(consP.type.jvmErasure.qualifiedName) {
is "kotlin.String" -> // do String thing;
is "kotlin.Int" -> // do Int thing;
}
Is it possible to do what I want without comparing strings? I’m particularly worried about what happens when the class contains Java parameter types (like java.lang.String
), and don’t want to have to deal with every possible qualified name for Kotlin and Java types.
Edit: I’ve found consP.type.jvmErasure.java
, which returns the underlying Java class or primitive. But I’m still looking for the KClass