Welcome to ahex technologies

Lombok Installation And Usage In Java

Lombok Installation And Usage In Java
  • January 4, 2018
  • Sanjay
  • 0

What is 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.

Install Lombok in Eclipse

First we need to download the Lombok jar. We can directly download it from lombok site 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 machine which will create problem when we will have to use updated version of the it.

Create you are maven project in eclipse and below depedency in pom.xml file.

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.16.18</version>
</dependency>

Start Lombok Installation

Once the jar downloaded in System , goto the jar location from 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.

lombok

Give Lombok Install Path

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

lombok2

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,

lombok3

Lombok Examples

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.

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

@Getter
@Setter
public class LombokModel {

   private @NonNull String name;
   private @NonNull String age;
   private @NonNull String address;

   // No Getters and Setters, hashCode, Equal-- even though we can refer it from client code.
   // This is how we are taking help from Lombok to get rid of boiler plate code.
}  

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.

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@RequiredArgsConstructor
public class LombokModel {

   private @NonNull String name;
   private @NonNull String age;
   private @NonNull String address;

}

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
  6. @EqualsAndHashCode
  7. @ToString
Tags: