Unlike Java, Kotlin allows me to use an annotation on an expression, e.g.:
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Example
class Worker {
fun work() {
var a = 0
@Example
a += 1
}
}
I have been looking for some time, but can’t work out how to identify the annotation site during annotation processing (with kapt).
Using:
roundEnv.getElementsAnnotatedWith(Example::class.java)
Gives no results, which makes sense as there are no suitable element kinds in Java.
I’ve also seen notes that state that annotations with an EXPRESSION target are unavailable at runtime which again makes sense. But if this is true, then I assume there must be some way to access the annotations at compile time or they’re a bit pointless. What am I missing??
If it helps I simply want to count the uses of an annotation within a specific method and retrieve the attributes from the annotations in the order that they are declared. The method will have a different annotation, so a potentially suitable pattern might be to identify the method element using the round environment and then get a list of all the annotation instances (of my expression annotation) within that element – I just can’t see how to do that.