But does it really need to be solved comprehensively? What about just
allowing appropriate annotations to be copied, and let people create
getters/setters by hand if they want something more sophisticated?
Here's what I have in mind:
class Thing {
@MethodExample // binds to fields and methods
@FieldOnlyExample // binds to fields only
@Getter(annotate=true)
int someField;
@ParamAndMethodExample // binds to fields and params and methods
@Setter(annotateMethod=true annotateParam=true)
String otherField;
}
This would translate to:
class Thing {
@MethodExample
public int getSomeField() { ... }
@ParamAndMethodExample
public void setOtherField(@ParamAndMethodExample String value) { ... }
}
The idea being that while it may not be universal, there are many many
annotations that are @Target({ElementType.FIELD, ElementType.METHOD})
and it would be convenient to just copy these annotations wholesale.
For exceptional cases, you make a getter/setter method explicitly - oh
well, not any worse off than we are now. Maybe this can cover 60% of
the cases - that's still a huge win.
This would be especially handy with homebrewed annotations like CDI
@Qualifier annotations. I'm doing something similar right now with
annotations that filter JSON serialization. I believe it would work
well with JAXB annotations too.
Thanks,
Jeff
I think you are right. An automatic decision what to do with annotations on members if @Getter or @Setter seems to be an impossible task, but with some hints from the user it could be done.
An example: If using JAXB and @XmlElement annotation on members in combination with @Getter produces a getter methods with the same annotation and thus JAXB is confused if it should use the member directly or the getter method - because the signature is the same.
For now I use generation of getters/setters via Eclipse to speed up development if I need them.
Am Freitag, 16. September 2011 18:16:45 UTC+2 schrieb Reinier Zwitserloot:
Knowing the nature of any given annotation (its legal targets for example), is not feasible at this point in time.
--
Hi,
I would like to know if this feature (the onMethod parameter) is already implemented/in place in any of the versions of Lombok. I am using lombok along with some JSR330 validation annotations, and for some of them (like @NotNull), if I use lombok, I get the value validated twice (one for the field and one for the getter) and therefore two validation error messages. It is pretty annoying.
My workaround is obviously not to use @Getter for those fields and create the getter by hand.
Thanks for the great lombok!
--