Quite often, when I use annotations from Java libraries in Kotlin code, I have to specify target in order to specify what element in the compilled code has to be annotated:
data class User(
val id: String,
@get:Email
val email: String
)
Instead of specifying @get:Email
, I would love to be able to use simply @Email
, especially if it occurs in many places.
Question: Is there a way to hint Kotlin compiler to use a different target in all places so that if I use @Email
it will handle it as if it was @get:Email
? Or is there any other way to omit specifying target? How can it be achieved? May it be done on the compilation stage via annotation processing (like Lombok does)?
I would appreciate any ideas even if they don't answer my question directly.
You can't override it. Maybe create annotation that will target
@Email
annotation and provide the property getter when it tries to call target.Specifying target is dictated by the annotation target. When the target of the annotation is specified to jvm use-targets then you must use
@get:
to specify that the annotation targets the getter of the property.In order to use simply
@Email
you must use kotlin target types for your annotation, probablyPROPERTY
. Note that,PROPERTY
target doesn't work with Java.