Hi. I’m wondering why my annotation is not visible...
# announcements
s
Hi. I’m wondering why my annotation is not visible in Field(java) even though I set annotation target as Field and Property.
Copy code
// Annotation below
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
annotation class MyAnnotation

// Class
data class MyClass (
    @MyAnnotation
    var myProperty: String,
    @MyAnnotation
    var maybeICouldBeField: String
)

// Verification Code
val KProperties = MyClass::class.memberProperties
KProperties.forEach() {
    println(it.annotations)
    println(it?.javaField?.annotations)
}
In Verification Code, why I can’t see any annotation from javaField? 🤔
t
@field:MyAnnotation
should help. maybe
r
afaik, the annotation will only choose the most appropriate target; not all of them if you use
@field:MyAnnotation
then it will be visible on Java field, but not in
it.annotations
e
https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets
If there are multiple applicable targets, the first applicable target from the following list is used:
- param
- property
- field
s
Oh, thanks for your answers! Then the applied target is the only first applicable one, property, so java field couldn’t see it.
e
since you didn't specify, and param can apply, your annonation is effectively
@param:
and not
@field:
.
r
*`@property:` no? Since VALUE_PARAMETER is not in targets
2