Project Lombok is a Java library that helps reduce boilerplate code. Java is a very verbose language where repetitive code like getters, setters, etc. can be avoided. Lombok reduces the boilerplate code with its annotations that get plugged during the build process.
Lombok can easily be added to the project as a dependency.
If the project is a Gradle project, we add the following two lines to the dependencies section of the build.gradle file:
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
If the project is a Maven project, we add the following lines to the dependencies section of the pom.xml file:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
@RequiredArgsConstructor
annotationThe @RequiredArgsConstructor
annotation generates the constructors with one parameter for each field needing special handling.
The final fields annotated with @NonNull
are the parameters of the required constructor because they need special handling.
For example, final fields need to be initialized. So, for classes that have final fields that are not initialized, the annotation generates a constructor for the fields.
However, the annotation will not generate a constructor for the following fields:
The following code shows how the annotation reduces writing the required argument constructor to a single annotation:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
In the ode above, we define two classes:
VanillaJavaPerson
UsingLombokPerson
The VanillaJavaPerson
class indicates how the required argument constructor is written in vanilla Java. It contains a final field called age
that is not initialized during the declaration. Hence, we write a constructor that initializes the age
field.
The UsingLombokPerson
class indicates how the required argument constructor is generated by the annotation. The class contains a final field called age
that is not initialized during the declaration. As the class is annotated with @RequiredArgsConstructor
annotation, Lombok generates the required argument constructor is generated. Hence, there is no need to explicitly write it.
The main method contains code to create the objects of the
VanillaJavaPerson
andUsingLombokPerson
classes.