javaClass.superClass not returning null


#1

Hello,

I am using Kotlin reflection to scan for annotations in classes. However, when I get the super class with “javaClass.superclass” of a class it gets stuck in an infinite loop because it sees Java “Object” as a class. The native Java method “getSuperClass” returns null when the super class is of type Object, interface, a primitive type or void (as stated in JavaDoc).

How can I detect in Kotlin if the superclass is not of type Object, interface, a primitive type or void?


#2

Don’t call .javaClass.superclass on variable of type Class. If you do it right, there will be no loop.

var c: Class<*>? = something.javaClass
while (c != null) {
    println(c)
    c = c.superclass
}