vendredi 21 mai 2021

Kotlin - Instance cannot retrieve it's class annotations via myInstance::class.annotations but can with javaClass.annotations

I've encountered strange behaviour when trying to read Class's annotations from an object.

For example:

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class MyAnnotation(val value: String) {}


@MyAnnotation("HelloWorld")
class MyClass 

// in test
val obj = MyClass()
println(obj::class.annotations) // DOESN'T finds annotation
println(obj::class.java.annotations) // finds annotation
println(obj.javaClass.annotations) // finds annotation

However, it does find it when I make direct reference to a class like so:

println(MyClass::class.annotations) // finds annotation

Anything I'm missing?





Aucun commentaire:

Enregistrer un commentaire