I am creating a Kotlin compiler plugin in which I need to check if a property of a data class has an annotation:
data class User(
@MyAnnotation
val name: String
)
I override DelegatingClassBuilder.newField
in the following way:
internal class MyClassBuilder(
delegateBuilder: ClassBuilder
) : DelegatingClassBuilder(delegateBuilder) {
override fun newField(
origin: JvmDeclarationOrigin,
access: Int,
name: String,
desc: String,
signature: String?,
value: Any?
): FieldVisitor {
val visitor = super.newField(origin, access, name, desc, signature, value)
val descriptor = origin.descriptor as? PropertyDescriptor ?: return visitor
if (descriptor.annotations.hasAnnotation(FqName("com.example.MyAnnotation"))) {
// I never get here
}
return visitor
}
}
Problem: No matter if my property is annotated or not descriptor.annotations
would not contain my annotation. If I change the annotations use target to anything else I still don't get the annotation.
At the same time, if I annotate a function and override newMethod
, I can get annotations of of this function with pretty similar code.
Question: How to get annotations of a property in a data class?