Groovy automatically generates getters and setters, so e.g. when I type:
int someField
I get the field + getter + setter. Now I want to put an annotation on the setter (e.g. @Requires/Ensures from GContracts):
@Ensures({someField >= 0 && someField <= 100})
int someField
And then I get error: Annotation groovy.lang.GrUnit is not allowed on element FIELD - GrUnit and GContracts recognize annotations on methods only. The workaround for this is coding getter explicitly:
@Requires({...})
void setSomeField(int newValue) { ... }
Is there a better solution for this? In Scala there is an elegant solution for this: http://www.scala-lang.org/api/current/scala/annotation/target/package.html
Is there something like that in Groovy? Or alternatively: maybe some workaround for GContracts to allow such annotations?