These 3 Lombok Annotations Might Skyrocket In Popularity After Java 17 Migration
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…