Today I would like to share with you my new technical companion: projectlombok.
What is lombok
ProjectLombok like Springfuse is a code generator, and I very much like code generators as they make my life easier.
Where springfuse is producing working knowledge spanning many technologies, project lombok produces simple code to bypass java excessive verbosity.
Project lombok is generating :
So you have now only the getter and setter to generate with eclipse and no more need to worry about your equals/hashcode methods.
You should now have this:
This is better, less error prone, but we still still have so many getter/setters that we can’t stand.
Lombok comes and wins
Now look how we do it using lombok:
Isn’t it elegant ?
The @Data does express my intention nicely … and completely as the code is also generated.
After the shock of such a concise POJO, the next though you have is for configuration …
Projectlombok gives you this freedom.
Please note that Lombok is a good citizen as it gives you clear warnings.
How does it work ?
Project lombok is a javac plugin that will use the javac 6 annotation processor.
That means that the generation of the methods is part of the compilation, and no runtime nor java agent is required !
It’s almost like adding behavior with aspectj but with plain java !
Lombok and maven
Using lombok and maven is straightforward:
Lombok and eclipse
Execute the lombok.jar and specify your eclipse folder it will add itself or add manually in eclipse.ini the botclasspath and javaagent like this
Lombok does work only under javac 1.6, but if you want to target 1.5 just configure it in your pom.
But JDK 1.5 has 10 more days before it is no more supported by sun !
It is time to give this info to your boss and make the switch.
The current version 0.8.5 does not work great in this regard, however they fixed it in the trunk.
If you do build your own, you’ll be fine
git clone git://github.com/rzwitserloot/lombok.git
ant ... or wait for the next binary release !
Edit: the 0.9 release is out
It’s in the way
Do not abuse
Lombok generates other methods like @Cleanup, @Synchronized, @SneakyThrows or @Logger.
I am not sure what to think about these, maybe it could go too far but as the intent is clearly described via the annotation, why not…
Anyway I’ll stick with the simple features for now and see how it goes.
I love this tool !
I have to say, that the combination of google collections and project lombok gave me a fresh view of what you can accomplish with java.
It feels like the gap between the language “du jour” and my plain old java has been dramatically reduced.
And you know what ?