DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • NullPointerException in Java: Causes and Ways to Avoid It
  • How Does Video Annotation Augment Computer Vision?
  • Role of Data Annotation Services in AI-Powered Manufacturing

Trending

  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  1. DZone
  2. Coding
  3. Languages
  4. Introduction to Project Lombok

Introduction to Project Lombok

Less code means fewer errors.

By 
vikas kasegaonkar user avatar
vikas kasegaonkar
·
Updated Dec. 18, 18 · Presentation
Likes (31)
Comment
Save
Tweet
Share
102.4K Views

Join the DZone community and get the full member experience.

Join For Free

Project Lombok is a boilerplate code remover and space saver that generates code in ".class" file instead of in the source code file. In this article, I will try to explain why Lombok is so popular and its implementation with a step-by-step example.

Introduction

Project Lombok is a Java library tool that is used to minimize boilerplate code and save timeduring development. It is no doubt that Java is a great language, but recently, it is criticized by the community for one important reason — verbosity.

Lombok generates code, for example, getters, setters, and toString, and the IDE does the same thing for us only it generates in our source code while Lombok generates it in the ".class" file directly.

How to Configure Project Lombok to IDE?

Setup With IntelliJ 

Step 1: Go to File > Settings > Plugins

Step 2: Click on Browse repositories

Step 3: Search for Lombok plugin

Step 4: Click on Install Plugin

Step 5: Restart IntelliJ IDEA

Setup With Eclipse

Lombok will operate if we put it in the project classpath, and for that, we need to do the following steps.

Step 1: Download lombok.jar from https://projectlombok.org/downloads/lombok.jar

Step 2: To have library version consistency and avoid version conflicts at the project level, let's use Maven to perform the download on our behalf.

Step 3: Create a Maven project and add the following Maven dependency in pom.xml.

<dependencies>
        <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <version>1.16.16</version>
        </dependency>
</dependencies>


Step 4: After building the project, you will find lombok.jar downloaded in the .m2 repository. Copy this jar and paste it to your Eclipse folder.

Step 5: Open the command prompt window and navigate to the location where lombok.jar is copied. Execute the command "java -jar lombok.jar." It will open an installation wizard, as shown below.
Image title

Step 6: Click on the "Specify location" button and provide a path of "eclipse.exe," as shown in the image below.

Image title

Step 7: After providing the path, click on the "Install/Update" button. This is the last step of installation and there will be a popup with the successful installation completion message.

Image title

Now, our IDE is equipped with the Lombok apparatus.

Lombok Annotations

Lombok provides a set of annotations. Some of the annotations can be used in day-to-day coding. Let's have a look at a few of them.

@Getter and @Setter

The @Getter and @Setter annotations provide getter and setter methods for a field respectively. These annotations can be used at the field and class level.

Encapsulating object properties using the public getter and setter methods are common practices in the Java language. Many frameworks rely on this "bean pattern," and therefore, IDE also supports autogeneration of the getter/setter and constructor code.

Example

Vanilla code (without the Lombok feature):

public class EmployeeWithoutLombok {

private String empId;
private String firstName;
private String lastname;
private Date dob;
private String phoneNo;

public String getEmpId() {
return empId;
}

public void setEmpId(String empId) {
this.empId = empId;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public Date getDob() {
return dob;
}

public void setDob(Date dob) {
this.dob = dob;
}

public String getPhoneNo() {
return phoneNo;
}

public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}

}


Code with Lombok annotations:

@Getter
@Setter
public class EmployeeWithLombok {

 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

        // Code required for getter and setter method, not needed to write.
}


@ToString Annotation

This annotation generates the toString() method in the ".class" file at compile time. There is no need to write explicit code for the toString() method in the POJO class. If we use the @ToString annotation, then we no longer need to maintain the toString() method whenever we enrich POJO properties.

Example

Vanilla code (without the Lombok feature):

public class EmployeeWithoutLombok {

 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

 // toString method
 @Override
 public String toString() {
 return "EmployeeWithoutLombok [empId=" + empId + ", firstName="
 + firstName + ", lastname=" + lastname + ", dob=" + dob
 + ", phoneNo=" + phoneNo + "]";
 }
}


Code with Lombok:

@ToString
public class EmployeeWithoutLombok {

 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

}


@Builder

This annotation is used to generate the code required to have your class be instantiable using the builder pattern.

Example

Builder pattern without the @Builder Lombok annotation:

public class EmployeeWithoutLombok {

 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

 public EmployeeWithoutLombok(String empId, String firstName,
 String lastname, Date dob, String phoneNo) {
 this.empId = empId;
 this.firstName = firstName;
 this.lastname = lastname;
 this.dob = dob;
 this.phoneNo = phoneNo;
 }

 public static class EmployeeBuilder {
 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

 public EmployeeBuilder withEmpId(String empId) {
 this.empId = empId;
 return this;
 }

 public EmployeeBuilder withFirstName(String firstName) {
 this.firstName = firstName;
 return this;
 }

 public EmployeeBuilder withLastName(String lastName) {
 this.lastname = lastName;
 return this;
 }

 public EmployeeBuilder withDob(Date dob) {
 this.dob = dob;
 return this;
 }

 public EmployeeBuilder withPhoneNo(String phoneNo) {
 this.phoneNo = phoneNo;
 return this;
 }

 public EmployeeWithoutLombok build() {
 return new EmployeeWithoutLombok(empId, firstName, lastname, dob,
 phoneNo);
 }
 }

 @Override
 public String toString() {
 return "EmployeeWithoutLombok [empId=" + empId + ", firstName="
 + firstName + ", lastname=" + lastname + ", dob=" + dob
 + ", phoneNo=" + phoneNo + "]";
 }
}


Builder pattern with Lombok:

@Builder
public class EmployeeWithLombok {

 private String empId;
 private String firstName;
 private String lastname;
 private Date dob;
 private String phoneNo;

}


@Log4j, @Slf4j

These annotations are used to generate code related to the logger, as demonstrated in the following example.

Code without Lombok:

public class EmployeeServiceWithoutLombok implements EmployeeService{

 private static final Logger logger = Logger.getLogger(EmployeeServiceWithoutLombok.class);
 @Override
 public void method1() {
  logger.info("In method1 ");
 }
}


Code with Lombok annotations:

@Log4j
public class EmployeeServiceWithLombok implements EmployeeService{

 @Override
 public void method1() {
  log.info("In method1 ");
 }

}


Conclusion

Reducing boilerplate code results in better readability; less code means fewer errors. Project Lombok is becoming common in almost all major organizations and projects.

The examples found in this article are available on GitHub.

Additionally, the official Lombok document can be found here.

Annotation intellij Boilerplate code

Published at DZone with permission of vikas kasegaonkar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • NullPointerException in Java: Causes and Ways to Avoid It
  • How Does Video Annotation Augment Computer Vision?
  • Role of Data Annotation Services in AI-Powered Manufacturing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!