admin管理员组

文章数量:1401673

I am working with a Java agent that is adding an annotation dynamically to a class file. When this class file is a Kotlin class, and if it is investigated by Kotlin reflection, the annotation does not become visible. If another previous annotation is added to the class, the added annotation is visible. So for the case:

@Qux
class Bar

@Retention(AnnotationRetention.RUNTIME)
annotation class Qux

everything works when I dynamically add @MyAnnotation using the agent. But for only

class Bar

the added @MyAnnotation cannot be seen by Kotlin reflection (but can be seen by Java reflection).

My suspicion is that this is related to kotlin.Metadata, and indeed, the data1 property changes when an annotation is added during compilation through Kotlin. It adds two characters:

\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002**\b\u0003**\b\u0007\u0018\u00002\u00020\u0001B\u0007\u00a2\u0006\u0004\b\u0002\u0010\u0003\u00a8\u0006\u0004

The added signs are \b\u0003. I also tried Kotlin Metadata and the one change I found in the read property is that the returned instance of KotlinClassMetadata$Class changes the contained kotlin.metadata.KmClass's flags from 7 to 6 when the annotation is removed during compilation.

Why would this flag change make the tool-added annotation visible or not visible. Is this a bug in Kotlin reflection? To me, this should not make a difference.

本文标签: Dynamically added annotation is not visible to Kotlin reflectionStack Overflow