Useful KClass properties
A KClass fully describes a particular class including its type parameters, superclasses, functions, constructors, annotations, and properties. Let's define a toy class:
class Sandwich<F1, F2>()
Now we can inspect the KClass for this and find out the types of parameters it declares. We do this using the typeParameters
property available on the KClass instance:
val types = Sandwich::class.typeParameters
From here, we can get the label of the type parameter, and the upper bounds, if any have been defined (otherwise Any
):
types.forEach { println("Type ${it.name} has upper bound ${it.upperBounds}") }
In the case of Sandwich
, this would output the following:
Type F1 has upper bound [kotlin.Any?] Type F2 has upper bound [kotlin.Any?]
Next, let's show the superclasses for a given type. Firstly, we need a type that has many parents:
class ManyParents : Serializable, Closeable, java.lang.AutoCloseable
Then, on...