Writing code is a responsibility. Every line of code you write is a line of code that needs to be maintained and every line of code you write is an opportunity to introduce a bug.
As a developer you should focus your efforts in writing the important bits of code and avoid writing the kind of code that can be generated for you.
Project lombok greatly eases the pain of writing almost everything related to your POJOs. And it does so in such a natural way that this short story is more than enough to get you up to speed with it.
What kind of sorcery is this?
It’s not sorcery, it’s code generation. It sounds like overhead but you don’t have to worry about runtime performance because Lombok generates code at compile time. You don’t have to worry about IDE integration either: there are plugins for most IDEs. I can vouch for the IntelliJ IDEA one because IntelliJ is my IDE of choice and I have been using the plugin flawlessly for years.
Using Lombok is easy. You just put the right annotations in your classes and Lombok generates code for you. At the end of the day you will have much smaller classes that perform as good as the ones you write and are way easier to maintain.
Let’s see how it looks with an example:
This snippet of code will provide you with a class with:
- a constructor
public LombokExample(String foo, String bar)
- a very readable
- getters for all your fields
But this story is not about the feature set of Lombok. You can see all the features here if you want to know all you can do with Lombok. This story is about how compact and easy to maintain your POJOS can become.
Setting up Lombok
If you use maven you just need to add a dependency:
That’s all you need to start saving time. Let’s see some real life examples.
We want a POJO with two fields, default constructor, setters and getters for everything and nice implementations for
hashCode. We can have that class writing the following code:
NoArgsConstructor generates a default constructor for us and
Data provides us with setters, getters,
With these annotations, Lombok will generate this class:
As you can see Lombok works! All the promised code is generated for you at compile time. And the best part is that you don’t need to actively maintain tedious bits of code like
Let’s say you add now a third field:
When you compile this source, you will get all your
setters and properly implemented
hashCode methods that will use the freshly created field in their implementations. I spare you the generated code this time.
If you, like me, are an immutable person, Lombok have you covered. Let’s say you need an immutable POJO with a big constructor:
After compilation, you will get:
- A constructor receiving all the defined fields
public ImmutablePojo(String firstField, String secondField, Integer thirdField)
Gettersfor every field
- The classics:
Immutable POJO with a Builder
Lombok can add a Builder to your POJO with no effort. You just need to ask nicely:
In the generated code you will have a new static method to get access to your builder:
And an inner class implementing the builder itself:
You can now create your immutable objects using a fluent api:
And you can even get some extra commodities for the collection fields:
If you throw a
Singular annotation into the mix , Lombok will provide you with a spiced up builder that allows you to add single elements to your collection:
Lombok can do much more for you but just the basic stuff is an amazing productivity booster. I know that you can generate almost all this stuff with your IDE, but once generated, the code is yours to maintain and test.
And that is the real advantage of code generation tools like Lombok: you have less code to write and less code to maintain.