https://kotlinlang.org logo
#reflect
Title
# reflect
a

albertgao

01/23/2018, 4:24 AM
I got a
param.type === nz.salect.objJSON.JVMBranchTest.MenuObject?
where
param:KParameter
. But when I tried to get the class definition from
param.type
via
param.type::class
. Instead of getting the above
MenuObject
, I get a
class kotlin.reflect.jvm.internal.KTypeImpl
. How to get my own class , I mean, a
KClass
? Thanks 🙂
Jesus, just need to cast:
param.type.classifier as KClass<*>
u

udalov

01/23/2018, 11:43 AM
Depending on your use case,
param.type.jvmErasure
might be more helpful
👍 1
a

albertgao

01/23/2018, 11:10 PM
@udalov What’s the differences between the
jvmErasure
and
classifier
?
u

udalov

01/24/2018, 11:30 AM
jvmErasure
returns the class that represents the erasure of the given type on JVM. For normal classes, this is always simply that class. For generic type parameters, this is the erasure of the generic bound, or
Any
if there's no bound. For example, erasure of a simple type like
String
is class
String
itself. Erasure of a type
List<String>
is class
List
. Erasure of
T
in
fun <T> foo() {}
is class
Any
. Erasure of
Y
in
fun <X : Set<String>, Y : X> bar() {}
is class
Set
.
👍 1
2 Views