Lombok Spring Tool Suite

Just recently I was introduced to miracles of Lombok so in this tutorial I'll show you how to add it to your workspace and project in Eclipse/STS and some basic usages of it.

Tool

Lombok is a library used for reducing the boilerplate code from Java source code. Boilerplate code is a typical source code that cannot be omitted by the language specification. Lombok - Tutorial, 2. Enable lombok for Eclipse or Spring Tool Suite. Lombok Introduction and Eclipse Installation Lombok is very handy tool for minimizing the boilerplate code as well as providing lot’s of other features such as lazy loading, thread safety or immutability. This is the reason it becoming very popular among the developer community. A simple java project with Lombok is working fine with no Eclipse's compilation or Maven compilation. I have a project where it has Maven Aspect plugin with maven compiler plugin for the project. The classes that call the methods (Getter/Setters) generated shows compilation errors on the IDE editor.

What is project Lombok?

Lombok is a Java library that makes your coding cleaner and easier. It's automatically plugged into your code editor and build tools so that lot of basic coding could be skipped using annotations.

Suite

Lombok setup

Configure Lombok in Eclipse and Spring Tool Suite. Once downloaded, double click on lombok.jar. This will open up the following window:-Lombok configuration for IDE. Spring Tool Suiteには、Lombokが適用されてはいないようです。 StarterProjectでgradleに指定されているため、アノテーションは問題無いです。 しかし、Contents Assistに表示されないのは非常に困ります。 そのため、STSのLombokを有効化します。.

You can add Lombok to your Eclipse/STS in few quick and easy steps.

Spring Tool Suite Download

Step 1.So, the first thing you need to do is to download Lombok from its official webpage. Run the jar you downloaded and it will scan your filesystem for available IDEs. Check those that you want to have support for Lombok and click install. That's it.

Step 2. You also need to add Lombok to your project as a dependency because you'll have some annotations in code that the compiler should be able to recognize. I'm using Maven, so this is dependency I will add:

Step 3. This one is just to check if annotation processing is enabled on your project. From the main menu, pick Project and then go to Properties -> Java Compiler -> Annotation Processing -> Enable Annotation Processing. Make sure that this option is checked and you're ready to go.

Step 4. If your IDE was started during the installation, restart it.

How to use Lombok in Eclipse?

When you add Lombok to your project, you'll see in class outline that when you add annotations, things are being created. In the example below, you'll see that I have a class with two parameters:

When I saved this class, my outline showed an empty constructor, a constructor with all params and a builder that will help me with my class instantiation.

We'll mention and explain some commonly used Lombok commands.

val and var

They are used for defining variables where their type is determined during variable initialization. The difference between val and var is that val is final, while var is not.

@NonNull

This one does null check on variables and throws NullPointerException in case that annotated variable is null:

Pst to utc converter 24 hour clock. In this example, if the provided parameter name is null, the method will throw NullPointerException.

@Getter and @Setter

As their names say, these annotations replace getters and setters in your class.

Lombok Spring Tool Suite

@ToString

This will override toString method and it will make your logging and debugging much efficient.

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

These will replace few types od constructors - empty constructor, constructor with required (this means that final variables and those annotated with @NonNull will be part of it), and constructor that contains all variables defined in class, ordered as your parameters are in class.

Spring

@Builder

Install Lombok Sts

This will allow you to easily create objects.

Lombok provides many other features, I just wanted to mention ones I use the most. They have practical and great documentation on their features available here.

Lombok is very handy tool for minimizing the boilerplate code as well as providing lot’s of other features such as lazy loading, thread safety or immutability. This is the reason it becoming very popular among the developer community.

In this lombok tutorial, we will learn about project Lombok in detail including its usage with examples.

1. What is Project Lombok

Lombok is a open source library (basically a standalone jar) which is capable of doing magic in automating the boilerplate code generation for any java class. So if Lombok is in classpath, it can easily get rid of all the getters & setters methods, class constructors, hashcode and equals methods and many more by just adding couple of annotations the class.

Is it not a cool feature? Let’s start learning it to use it extensively in your next project.

2. Install Lombok in Eclipse

Though Lombok will work if we put Lombok on the project classpath. But in order to make it work with eclipse, we need to first do couple of steps to install it in eclipse.

  1. Download Lombok Jar File

    First we need to download the Lombok jar. We can directly download it from https://projectlombok.org/downloads/lombok.jar but as we will use maven in future, so let’s maven do the download on our behalf, otherwise we will have multiple version of same jar in the machine which will create problem when we will have to use updated version of it.

    To do this, we will create a maven project in eclipse and add lombok dependency in pom.xml. Latest version of Lombok at the time of writing this article is 1.18.12.

    Add below dependency in your maven project so that it got downloaded first in your local repository.

    Now do a mvn clean install command on the newly created project to get this jar downloaded in local repository.

  2. Start Lombok Installation

    Once the jar downloaded in Local repository, goto the jar location from the command prompt and run the following command java -jar lombok-1.16.18.jar and we should be greeted by Lombok installation window provided by Lombok like this.

  3. Give Lombok Install Path

    Now click on the “Specify Location” button and locate the eclipse.exe path under eclipse installation folder like this.

  4. Finish Lombok Installation

    Now we need to finally install this by clicking the “Install/Update” button and we should finished installing lombok in eclipse and we are ready to use its hidden power. Final screen will look like,

3. Using Lombok in Application

Now let’s see some examples of using Lombok in project source code.

  • Get rid of Setters and Getters

    We all have to generate this java bean pattern heavily in day to day work and it has become so popular that all the IDEs have given feature to generate the Getters and Setters – but once generated by IDE, what’s next? we need to carry those code in whole lifetime of the project and we need to maintain those and it also increases the line of code of the whole project.

    With Lombok, we need to add few annotations in the class and we are done. To generate Setters and Getters we need to just add @Getter and @Setter at the class level like this.

    Look at the outline window on the right, we have all the setters and getters ready to be used.

  • Generating Constructors

    Lombok can easily generate the constructors, both no arguments and with fields. We need to add annotation @NoArgsConstructor to generate the implicit no-argument constructor and @RequiredArgsConstructor to add required field constructor.

    Also, to make a field required, we need to add @NonNull in the field itself like below.

    Also, look at the outline window on the right, Lombok has already added the constructor as we wished.

  • hashCode(), equals() and toString() methods

    We can easily add default implementation of these methods by adding annotations @ToString and @EqualsAndHashCode in the class level.
    Code will now look like

    Also we can see the generated methods by looking at the outline window.

  • Conventional Object Builder Pattern

    We can easily add builder pattern in our code using Lombok. We don’t have to write separate builder class. Lombok will generate the builder along with fluent setter-like methods by simply adding the @Builder annotation in the class level like this.

  • Other Lombok Features

    We have few more annotations, which are also very useful as well. Those are left for you to try and play with. e.g.

    1. @Data
    2. @Delegate
    3. @Synchronized
    4. @Slf4j
    5. @Cleanup

4. Delomboking – Rollback Lombok from Project

Sometimes all great things are not always accepted by all. Think about this scenario where you had decided to take advantage of Lombok and already added lots of annotations and suddenly due to some change in the decision-makers in the project, you have been asked to stop using Lombok and need to go with the old way of doing things!!!

Now it would not be a easy task at all to revert all the annotations that has been added with the boilerplate code. To do that Lombok itself has provided some steps, by which we can easily replace the annotated source code by the Lombok generated classes.

Lombok Jar File

To get the lombok generated classes, lombok has already documented its steps here. This process is called delombok!!!

To use it, all you need to run on the command line is:

Above command will duplicate the contents of the src directory into the src-delomboked directory, which will be created if it doesn’t already exist, but delomboked of course.

Lombok Spring Tool Suite 4

Delombok tries to preserve your code as much as it can, but comments may move around a little bit, especially comments that are in the middle of a syntax node.

5. Summary

Spring Tool Suite Installation Guide

In this Lombok tutorial, we saw how we can use Lombok to assist us to get rid of some repetitive code and also it can do some extra things as well. So you can now use this tool whenever applicable and I will suggest you to start using it in your daily work.

Drop me your questions in comments section.

Lombok Spring Tool Suite 4

Happy learning!!!