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.
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.
Lombok setup
You can add Lombok to your Eclipse/STS in few quick and easy steps.
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:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
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:
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class CustomerDTO {
private UUID id;
private String name;
}
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.
val a = new ArrayList<String>(); <=> final ArrayList<String> a = new ArrayList<String>();
var b = new ArrrayList<String>(); <=> ArrayList<String> a = new ArrayList<String>();
@NonNull
This one does null check on variables and throws NullPointerException in case that annotated variable is null:
public void someMethod(@NonNull String name) {
this.name = name;
}
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.
@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.
@Builder
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.