Dynamicly added annotations are not visible

I am trying to add support for kotest for a test intelligence tool. To do so, I need to be able to add the Ignored annotation to classes dynamically using a Java agent, but the annotation seems to not be visible to Kotlin.

The annotation is visible when using Java reflection. If I call MySpec::class.java.declaredAnnotations, I can see the annotation that I added. If I however, such as kotest, call MySpec::class.annotations, it is not visible.

If I add any unrelated annotation to MySpec, the annotation does show up, and suddenly my dynamic annotation also becomes visible.

What do I need to do to add an annotation to a Kotlin class and make it visible? What prevents Kotlin reflection to see the annotation that is clearly present on the class file?

You may need to do some Kotlin metadata manipulation I think. See kotlin-metadata

I considered this, but I do not understand where I would need to flip a bit. Metadata contains these bits:

  • is KotlinClassMetadata.Class -> "Class ${kcm.kmClass.name}"
    
  • is KotlinClassMetadata.FileFacade -> "File facade with functions: ${kcm.kmPackage.functions.joinToString { it.name }}"
    
  • is KotlinClassMetadata.SyntheticClass -> kcm.kmLambda?.function?.name?.let { "Lambda $it" } ?: "Synthetic class"
    
  • is KotlinClassMetadata.MultiFileClassFacade -> "Multifile class facade with parts: ${kcm.partClassNames.joinToString()}"
    
  • is KotlinClassMetadata.MultiFileClassPart -> "Multifile class part ${kcm.facadeClassName}"
    
  • is KotlinClassMetadata.Unknown -> "Unknown metadata"