Java API
If you have a java.lang.Class
object, then you can use Class::isAssignableFrom(Class)
.
val type: Class<*> = ...
val isSubtype = InterfaceType::class.java.isAssignableFrom(type)
This will obviously only work on Kotlin/JVM.
Kotlin/JVM API
If you have a kotlin.reflect.KClass
object, then you can use KClass::isSubclassOf(KClass)
or KClass::isSuperclassOf(KClass)
.
val type: KClass<*> = ...
val isSubtype = type.isSubclassOf(InterfaceType::class)
Though note those two functions are from kotlin-reflect
. In other words, you will need to add the full reflection dependency to use those functions and they are only available on Kotlin/JVM.
Kotlin/JS, Kotlin/Wasm, & Kotlin/Native API
I'm not aware of any reflection-based API for testing if a KClass
is a subclass of another that's available on these three platforms.