Lombok is a Java library that can generate known patterns of code for us, allowing us to reduce the boilerplate code.
This annotation can be used to validate a constructor or a method parameter. Additionally, if we annotate a field with @NonNull, the validation will be added to the constructor and setter method. Let’s compare the plain-java solution with Lombok’s @NonNull:
Perhaps the most popular Lombok annotations, @Getter and @Setter can be used to automatically generate getters and setters for all the fields.
This annotation is going to override the toString() method for easy debugging. As a result, the current state of the object will be returned as a String when toString() will be called.
If we need to override the equals method of a class and compare the fields instead of the reference, we can do it by adding the @EqualsAndHashCode annotation. This will also override the hashcode() method accordingly.
We can easily generate the class constructors with Lombok: if we want a constructor that receives all the fields, we only need to annotate the class with @AllArgsConstuructor. On the other hand, if we need the default constructor with no arguments, we need to add @NoArgsConstructor as well.
Though, we might only need to define constructors for the fields that are final — after all, the non-final fields can be set later. If this is the case, we should add the @RequreidArgsConstructor annotation at a class level.
If we have a class that exposes all its fields through getters and setters, we can annotate it with @Data. This will be the equivalent of @Getters, @Setters, @ToString, @EqualsAndHashCode, and @ToString.
If we want to work with immutable classes, we can annotate them with @Value. As a result, all the fields will be declared private and final, and will be required by the constructor. Additionally, the toString(), hascode() and equals() methods will be overridden. Though, if you are using Java17, the record type might be a better option.
We can annotate a class with many fields with @Builder and Lombok will inject an implementation of the builder design pattern for us. This can be extremely useful sometimes, especially if some of the fields are nullable.
One of my favorite Lombok annotations is @SneakyThrows. If we annotate a method with this, the code will be surrounded by a try-catch block and the checked exception will be wrapped in a RuntimeException. This can be useful when working with Java streams where checked exceptions are not allowed.
If we need to synchronize method calls, we can use Lombok’s annotation instead of the synchronized Java keyword for a safer implementation.
If we use immutable classes, we can annotate their fields with @With and the equivalent of a setter will be generated. The difference is that the with-er method will return a completely new instance of the object, with one of the fields updated. This annotation can be extremely useful for Java 17’s records.
We can add an annotation at the class level and Lombok will create a log instance for us. Depending on what logging library we use, there are different implementations available, some popular ones are @Slf4j and @Log4j2.
This is, perhaps, one of the least used Lombok annotations. This can be used on the classes that do implement the Closable interface and it will be similar to the try-with-resources block. Let’s compare all three solutions