onMethod, onConstructor, and onParam re-introduced!

430 views
Skip to first unread message

Reinier Zwitserloot

unread,
Feb 11, 2013, 5:10:03 PM2/11/13
to project...@googlegroups.com
In lombok 0.10.0 beta releases, briefly, you could write something like:

@RequiredArgsConstructor(onConstructor=@Inject)

However, the way we implemented this feature simply did not work, and couldn't be made to work, on javacs from JDK7. We removed the feature before any official 0.10.0 release ever shipped. Shame, because JPA and dependency frameworks benefit quite a bit from being able to add custom annotations to generated methods and constructors.

Good news, everyone!

We found a new (unfortunately uglier) way to support this feature that DOES work on all javacs, and even allows for some code completion in eclipse. It looks like:

@RequiredArgsConstructor(onConstructor=@_(@Inject))

The additional indirection introduced by the non-existent @_ type is the magic that makes this work on JDK7. So, yes, ugly, but required.

We've made some streamlining changes to the codebase of Getter/Setter/Wither/Constructor too, though those should be refactors that have no effect on how lombok works. Still, for now at least, this feature is only available in edge builds.

The website has been updated with complete documentation on this feature though, if you want to have a look: http://projectlombok.org/features/experimental/onX.html

edge release can be downloaded from: http://projectlombok.org/download-edge.html


Feel free to discuss the feature (especially if you find any bugs!) here.

Reinier Zwitserloot

unread,
Feb 24, 2013, 7:41:16 PM2/24/13
to project...@googlegroups.com
So, has nobody played much with this yet, or is it all according to expectations?

If yes, we're going to consider releasing 0.11.8 with this feature included.

eric.giese

unread,
Mar 18, 2013, 4:09:54 AM3/18/13
to Project Lombok
As a heavy guice user, I would heavily appreciate such a feature to
cut off the @Inject-Constructor boilerplate. However, we lombok users
will have a hard time to justify an enhancement in a real project
which (correctly) identifies itself as basically a workaround until a
good syntax is available.

Until the java compilers support a clean way to implement this, I will
rather stay with hardcoding. :-/

Or, better, I implement or use a lombok-extension which generates
@Inject-RequiredArgsConstructors.

eric.giese

unread,
May 8, 2013, 5:48:10 AM5/8/13
to project...@googlegroups.com, eric...@googlemail.com
I was just soooooo wrong.
Just started to introduce the new feature in most of my guice classes:
@RequiredArgsConstructor(onConstructor = @_(@Inject), access = AccessLevel.PACKAGE)
Works really like a charm an makes the code much more manageable.

Yes, I might need to adapt it in some unforeseen future.
Yes, it might vanish in the future of whatever java version I could not think of.
But for now I'm the king of the (code) mountain.

It saves a plentora of read and write, especially during refactor sessions when dependencies are moved around.

Reinier Zwitserloot

unread,
May 8, 2013, 9:10:18 AM5/8/13
to project...@googlegroups.com, eric...@googlemail.com
Glad to hear it. If we ever find a nicer way of doing this we'll get right on that, but I doubt we can get any closer than this. For what its worth, this shouldn't break in future JDKs because the compiler cannot guarantee that this code (onConstructor = @_(@Inject)) is 'illegal' until annotation rounds are completed, and lombok gets to fix things before that happens.
Reply all
Reply to author
Forward
0 new messages