These 3 Lombok Annotations Might Skyrocket In Popularity After Java 17 Migration

Emanuel Trandafir
Javarevisited
Published in
4 min readNov 27, 2022

--

Let’s discuss Lombok’s @With, @Builder, and @NonNull annotations and see how we can use them with Java 17 records.

@With

Even though records are immutable objects, sometimes we might need to change the attributes of an object.

To solve this, we can create a new instance of the object and copy all the old values, except for the one that needs to be changed. Our method will return a new instance with the modified field and the old instance will not be affected:

If we add a similar “with” method for one or two other fields, we can already imagine the boilerplate code piling up.

Fortunately enough, we can use Lombok’s @With to generate the these methods:

@Builder

Instantiating bigger record objects with nullable fields might be troublesome: Passing “null” is a known code smell regardless if it is done to call a method…

--

--

Emanuel Trandafir
Javarevisited

Hi, I'm Emanuel Trandafir, a Java developer from Romania. I have a strong passion for clean code, software design, and unit testing.