Getter/Setter annotations revisited

631 views
Skip to first unread message

Jeff Schnitzer

unread,
Sep 16, 2011, 5:45:50 AM9/16/11
to project...@googlegroups.com
I've read up on the mailing list traffic related to copying
annotations to getters & setters, and yeah, solving the issue
comprehensively is tough.

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

Reinier Zwitserloot

unread,
Sep 16, 2011, 12:16:45 PM9/16/11
to project...@googlegroups.com
Knowing the nature of any given annotation (its legal targets for example), is not feasible at this point in time. 

Reinier Zwitserloot

unread,
Nov 22, 2012, 8:52:43 AM11/22/12
to project-lombok
We have a solution pending for this. Ugly, but it works. @Getter(onMethod=@_(@XmlElement)). The @_() is the ugly part, but it works around an overzealous javac that concludes no amount of annotation processing could possibly make @Getter(onMethod=@XmlElement) semantically legal java, and thus aborts with an error before even trying annotation processors. They did not realize lombok goes a little further than your average AP :)

I assume "ugly but works" is preferred over "unusably oversimplified because I _NEED_ annotations on my generated code" :)

Still finishing up the javac tests, but so far it seems like we're good for at least javac6 and javac7. We also have to implement this feature for ecj, but I'm not worried about ecj (we're an agent on that side, we can do whatever we want - no amount of overzealous analysis by the compiler can prevent us from meddling with the process there).

 --Reinier Zwitserloot



On Thu, Nov 22, 2012 at 2:27 PM, Daniel Wagner <dan...@wagners.name> wrote:
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. 

--
 
 

Roel Spilker

unread,
Dec 21, 2012, 12:15:51 PM12/21/12
to project...@googlegroups.com
We are close to releasing it, but it probably won't be this year anymore...

On 21-12-2012 11:23, Gonzalo Alvarez wrote:
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!

--
 
 

Reply all
Reply to author
Forward
0 new messages