Reduce your boilerplate code in Java classes with project Lombok
Project Lombok is brand new and it promises to make your life with Java classes a piece of cake. Get rid of all that getter and setter methods, hashcode, and equals methods with this new open source library.
It is always a reason to rejoice when new Java tools come out and the one I am exploring today is particularly interesting.
Lombok is a brand new open source library that aims to reduce boilerplate code in Java classes and help you get rid of all the getter and setter methods, hashcode, and equals methods.
How is this achieved, you ask? Well, it is done simply by replacing many of the repetitive pieces of code with simple and concise annotations.
Let’s take a closer look at what Lombok has to offer.
All the annotations you can handle
The features in the current version of Lombok include the following annotations:
@Getter and @Setter – Can be used either at a field or class level. If used at the class level, it will generate getters and setters for all the fields in the class.
@AllArgsConstructor – Generates a public constructor and will construct all the fields declared in your class in the same order as they are defined.
@NoArgsConstructor – Generates a constructor with no arguments. You should note that if the constructor cannot be generated due to the presence of
final fields, an error message will occur.
@RequiredArgsConstructor – Generates a constructor with all the
final fields in the class. Note that
@RequiredArgsConstructor can’t be used together and will throw compile time error if you attempt to do so.
@EqualsAndHashCode – Can be used at the class level which will generate implementations for
equals(Object other) and
hashCode() methods. By default, it will use all the non-static and non-transient fields. If there are certain fields you don’t want to include in the
hashCode methods, then you can exclude those specific fields by using
@ToString – Can be used to generate the
toString() implementation. By default, all non-static fields will be printed. You can specify to include or skip certain fields by using a combination of
@ToString(onlyExplicitlyIncluded = true).
@Value – An immutable variant of @Data and is used exactly for this purpose. By default, all the fields are made final and setters are not created.
@Builder – The Builder pattern is a creational design pattern that is used to help build objects in a step-by-step manner.
@Builder lets you automatically produce the code required for it.
@Log creates a log variable with a
java.util.logging.Logger.getLogger(LogExample.class.getName()) object. Lombok supports other logging frameworks too, which can be used by annotations like
@Slf4j etc. You can find the full list of supported frameworks here.
You have several options to get started with Lombok. More specifically:
Eclipse – Download the Lombok
.jar file from the official website and run the downloaded
lombok.jar file or execute the command in the terminal.
NetBeans – Download the Lombok
.jar and add it to the project libraries. Activate the plugin simply by selecting
Project Properties -> Build - Compiling -> Enable Annotation Processing in Editor.
IntelliJ – Go to
File -> Settings -> Plugins and select
Browse Repositories. Search
Lombok Plugin and click
Install Plugin. Afterwards, just restart the IDE and you’re ready to go.
To find out more about how to install Lombok on other IDEs, head over to the official website and take a look at the ‘Install’ section.