Hi Guys, today I am going to talk about Project Lombok. This blog post is divided into 3 parts:
- Introduction
- Setup (Using IntelliJ idea)
- Lombok Annotations
1. Introduction
Lombok is java library which helps in reducing boilerplate code. So that you are more focused on your actual code. e.g. A Simple POJO class consists of properties, getters/setter (, Constructors), so here Lombok will help you in auto-generation of Getter/Setters (and Constructors) by just adding an annotation.
2. Setup
Download Lombok plugin for Idea IntelliJ https://plugins.jetbrains.com/plugin/6317 as per your build number.
Goto File -> Settings -> Type Plugins in search text box at top left.
Now click Install plugin from disk. button and select the downloaded Lombok Plugin.
You are done now
In case you are using eclipse please refer to This Blog Post.
3. Lombok Annotations
Lombok has many different types of annotations for different tasks. You can view the full list of annotations here. In this blog, we will discuss the following annotations.
- @Getter/@setter
- @ToString and @EqualsAndHashCode
- @NonNull
- @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
- @data
- @Value
- @builder
- @Cleanup
At first, you need to add Lombok dependency in your classpath. If you are using maven then add bellow dependency in your pom.xml.
Gradle user will add below dependency in build.gradle file.
1. @Getter/@setter
Everyone is familiar with Getters/Setters in normal POJO. Generating getter/setter is not a big task, these days IDE is smart enough and it can generate those for you, but it increases your LOC and managing them could be a bit cumbersome. Lombok helps you in generating getter/setter by just adding @Getter
and @Setter
. By default generated methods type is public but you can change the type by overriding value property of @Getter/@setter which takes AccessLevel enum type. Available AccessLevel enum values are [PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE, NONE]. If you set the value to AccessLevel.NONE
then no getter/setter will be generated.
You can add these annotations on Class level too. It will generate getters for all fields and setters for all non-final and non-static fields.
Above code is equvalant to
Note: @Setter
will not work on final fields.
2. @ToString and @EqualsAndHashCode
@ToString
and @EqualsAndHashCode
generates toString()
, equals(Object object)
and hashCode()
in our pojo. By default @ToString
includes Classname and all non-static fields. You can specify fields in of
property of @ToString
. You can also exclude fields by exclude
property.
By default @EqualsAndHashCode
include non-static and non-transient fields. You can include or exclude fields by providing in of
and exclude
property (Same as @ToString
). It has extra property called callSuper
which invokes superclass’s implementation of hashCode()
and equals()
. By default, it doesn’t invoke superclass methods. You can override it by setting its value to true
.
3. @NonNull
This annotation will generate null-check for any field. It can be used with Constructor args, fields or method args.
Above code is equivalent to
4. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
@NoArgsConstructor
will generate default constructor. If your class contains final fields, then a compilation error will be generated. So if you want to generate default constructor with default values for final fields set force=true @NoArgsConstructor(force = true)
.
Above code is equvalant to
@RequiredArgsConstructor
will generate constructor, if your class contains final fields or any field marked with @lombok.NotNull
then it’ll generate parameterized constructor and those fields will be added in constructor args and a null check will be added in constructor.
Above code is equvalant to
@AllArgsConstructor
will generate parameterized constructor with all fields as constructor args.
Above code is equvalant to
Note: If you want to generate static factory method with private constructor then set staticName
property of @xxxConstructor.
Above code is equivalent to
5. @data
@Data
annotation can only be used with Class and it covers below annotations:
- @Getter
- @setter
- @RequiredArgsConstructor
- @ToString
- @EqualsAndHashCode
- @Value
6. @Value
@Value
is used to create Immutable POJO. By default class and all fields made final and no setters will be generated. Just like @Data
it also generates toString()
, hashCode()
and equals()
. If you don’t want to make a field final then mark it with @NonFinal
.
7. @builder
@Builder
is used to generate builder API pattern. It generates an inner class called <YourClassName>Builder which expose builder pattern-based setters. @Singular
is used with @Builder
and only valid with java.util.List
types. This annotation will add to adder methods one for single elements and another for the complete collection.
8. @Cleanup
@Cleanup
helps in automatically close the resource. This annotation takes one parameter as the closing method name. By default its value is close.
Happy Coding 😀😀😀 !!! If you have any feedback please comment down below.
Top comments (1)
Thanks for the info. I look forward to incorporating this into my work.