Introduction to Lombok (Speeding-up Java development)

Hi Guys, today I am going to talk about Project Lombok. This blog post is divided into 3 parts:

  1. Introduction
  2. Setup (Using IntelliJ idea)
  3. Lombok Annotations

1. Introduction

Lombok is java library which helps in reducing boilerplate code. So that you are more focused on you actual code. e.g. A Simple POJO class consist 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

  1. Check your Idea build number. Go to Help -> About

    About

  2. Download Lombok plugin for Idea IntelliJ https://plugins.jetbrains.com/plugin/6317 as per your build number.

  3. Goto File -> Settings -> Type Plugins in search text box at top left.

    Plugin Settings

  4. Now click Install plugin from disk. button and select the downloaded Lombok Plugin.
  5. 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 annontations here. In this blog we will discuss following annotations.

  1. @Getter/@Setter
  2. @ToString and @EqualsAndHashCode
  3. @NonNull
  4. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
  5. @Data
  6. @Value
  7. @Builder
  8. @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 familier 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 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 equvalant 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 null check will be added in construtor.

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 equvalant to

5. @Data

@Data annotation can only be used with Class and it covers below annotations:

  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @ToString
  • @EqualsAndHashCode
  • @Value Above code is equvalant to

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. Above code is equvalant to

7. @Builder

@Builder is used to generate builder API pattern. It generates 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 complete collection. Above code is equvalant to

8. @Cleanup

@Cleanup helps in automatically close the resource. This annotation takes one parameter as closing method name. By default its value is close. Above code is equvalant to

Happy Coding 😀😀😀 !!! If you have any feedback please comment down below.