Java + Lombok

Daniel Padua Ferreira
Daniel Padua Blog
4 min readJun 11, 2019

--

Intro

I always heard comments from other developers things like: “Java is a very verbose language”, or Hello World line count comparison between Java vs Other X Language. And I also think that this is true, when you develop pure Java (without extensions, libs and plugins) compared to other modern languages, the developer ends up writing more to reach the goal. However, since some time ago, we have a lib called lombok that helps you simplify (a lot) the life of those who develop with java.

Features

Lombok is a lib that provides a collection of annotations that eases some repetitive java tasks (such as, getters and setters creation). Besides of being dependency of your project, it connects in your IDE making it understand the annotations, so it requires a certain configuration. In this guide I’ll show how to configure it on eclipse, intellij idea and also vscode. If you don’t know how to work with java with vscode take a look at: https://medium.com/daniel-padua/java-spring-boot-vscode-9ef9b8a263cd.

The github repository with the sample classes can be found at: https://github.com/danielpadua/lombok-examples

Requirements

  • Java JDK 8 or higher
  • Eclipse or IntelliJ IDEA or vscode

IDE configuration

Use the sections below according to the IDE you are using:

Eclipse

Download latest lombok.jar at: https://projectlombok.org/download, store this file in some directory of your choice (e.g.: your user’s home folder). From this moment on, there are two ways of configuring eclipse:

  1. Run the lombok.jar jar. It will open a window that will search in all folders of your machine looking for IDEs and if it finds eclipse it will display a checkbox to enable. So, click install and it will be done, as the image below:
Lombok’s eclipse installation via jar

2. If method 1 didn’t work or didn’t found any IDE, we will do the same work jar does under the hood but manually. Open the eclipse.ini file located under the configuration directory of the eclipse that you use, and edit it as below:

Configuring lombok in eclipse manually

We are just adding the full path of lombok’s jar as eclipse’s initialisation argument, and this will make it understands lombok. Lombok’s jar does exactly the same thing, but it copies the jar inside of eclipse installation folder and writes eclipse.ini automatically.

IntelliJ IDEA

It’s really simple enable Lombok support in IntelliJ IDEA, to do so, open plugin installation section and install the highlighted plugin:

Lombok Plugin for IntelliJ IDEA

Restart IntelliJ IDEA and Lombok support will be enabled.

vscode

Search for lombok at the extensions tab:

Searching for lombok plugin in vscode

Install the Gabriel Basilio Brito’s extension, which is recommended by lombok: https://projectlombok.org/setup/vscode. Restart vscode:

Lombok working in vscode

Project

First we need to create a java spring-boot project, so:

After creating the project, use the sections below depending on the build-tool that you are using:

Maven

Add lombok’s dependency in pom.xml:

You may need to force maven import update on your IDE.

Gradle

Add lombok’s depedency in build.gradle file:

Usage

Now let’s explore what lombok has to offer for us to reduce boilerplate code:

Getters, Setters, Constructors, ToString, Equals e HashCode

To enable getter in all fields of your class, annotate above your class name: @Getter.

To enable setter in all fields of your class, annotate above your class name: @Setter.

To create a constructor with all fields as input parameter, annotate above your class name: @AllArgsConstructor, without parameters: @NoArgsConstrutor, with required fields (or non-null): @RequiredArgsConstructor

To implement default toString() use: @ToString,

To implement default equals() and hashCode() use: @EqualsAndHashCode

The @Data annotation above class is a shortcut to: @ToString, @EqualsAndHashCode, @Getter, @Setter and @RequiredArgsConstructor together.

Without lombok:

With lombok:

Immutable classes

Use the annotation: @Value above the class, to make it immutable

Without lombok:

With lombok:

Builder pattern

Use the annotation: @Builder above the class to create the builder pattern

Without lombok:

With lombok:

Logging

Slf4j is one of the supported logs, take a look at: https://projectlombok.org/features/log to see all the supported

Without lombok:

With lombok:

Conclusion

Despite the setup and configuration time spent, lombok was developed aimed to boost productivity of java developers. I’ve been using this lib for some time and I’m not thinking of stopping using it!

See you soon!

If you found this post useful and would like to support me, you can do so by buying me a coffee using the button above!

Originally published at https://blog.danielpadua.dev on May 5, 2019.

--

--

Daniel Padua Ferreira
Daniel Padua Blog

Microsoft Certified Professional (MCP), Certified Tester Foundation Level (CTFL), Software Engineer, Technology and Cryptocurrencies enthusiast