Introduction to Lombok (Speeding-up Java development)
Tags: java, lombok
Published: 2019-07-24 07:28:42

---
title: Introduction to Lombok (Speeding-up Java development)
published: true
tags: java,JAVA,LOMBOK
canonical_url: https://jsblogs.github.io/java/2016/09/14/Introduction-to-Lombok-(Speeding-up-Java-development)
---

Hi Guys, today I am going to talk about [Project Lombok](https://projectlombok.org/). 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 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

1. Check your Idea build number. Go to **Help** -\> **About** ![About](http://blogs.jbisht.com/images/java/lombok/about.png)

2. Download Lombok plugin for Idea IntelliJ [https://plugins.jetbrains.com/plugin/6317](https://plugins.jetbrains.com/idea/plugin/6317-lombok-plugin) as per your build number.

3. Goto **File** -\> **Settings** -\> Type Plugins in search text box at top left. ![Plugin Settings](http://blogs.jbisht.com/images/java/lombok/settings.png)

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](https://standardofnorms.wordpress.com/2013/05/10/reducing-java-boilerplate-code-with-lombok-with-eclipse-installation/).

### 3. Lombok Annotations

Lombok has many different types of annotations for different tasks. You can view the [full list of annotations here](https://projectlombok.org/features/index.html). In this blog, we will discuss the 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. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=maven_dependency.xml %}

Gradle user will add below dependency in build.gradle file. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=gradle_dependency.gradle %}

#### 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. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=GetterAndSetterLombok.java %}

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. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=GetterAndSetterLombokClassLevel.java %}

Above code is equvalant to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=GetterAndSetterNormal.java %}

**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`. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=ToStringAndEqualsAndHashCode.java %}

#### 3. @NonNull

This annotation will generate null-check for any field. It can be used with Constructor args, fields or method args. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=NonNullCheck.java %}

Above code is equivalent to

{% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=NonNullCheckNormal.java %}
#### 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)`. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=NoArgsConstructor.java %}

Above code is equvalant to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=NoArgsConstructorNormal.java %}

**`@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. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=RequiredArgsConstructor.java %}

Above code is equvalant to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=RequiredArgsConstructorNormal.java %}

**`@AllArgsConstructor`** will generate parameterized constructor with all fields as constructor args. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=AllArgsConstructor.java %}

Above code is equvalant to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=AllArgsConstructorNormal.java %}

**Note:** If you want to generate static factory method with private constructor then set `staticName` property of @xxxConstructor.

{% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=ConstructorWithFactoryMethod.java %}

Above code is equivalent to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=ConstructorWithFactoryMethodNormal.java %}

#### 5. @Data

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

- @Getter
- @Setter
- @RequiredArgsConstructor
- @ToString
- @EqualsAndHashCode
- @Value {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=DataLombok.java %} Above code is equvalant to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=DataLombokNormal.java %}

#### 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`. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=ValueLombok.java %} Above code is equivalent to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=ValueLombokNormal.java %}

#### 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. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=BuilderLombok.java %} Above code is equivalent to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=BuilderLombokNormal.java %}

#### 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**. {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=CleanupLombok.java %} Above code is equivalent to {% gist https://gist.github.com/jeetmp3/fa6d9327b670071f11b417ca9693029f file=CleanupLombokNormal.java %}

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