0% found this document useful (0 votes)
73 views17 pages

Lombok Presentation

The document discusses Project Lombok, which is a Java library that automatically generates boilerplate code like getters, setters, toString methods. It explains how to add Lombok dependencies and annotations like @NoArgsConstructor, @Getter, @Setter that generate specific code. The summary also mentions it generates code in class files instead of source files.

Uploaded by

yunus emre bey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views17 pages

Lombok Presentation

The document discusses Project Lombok, which is a Java library that automatically generates boilerplate code like getters, setters, toString methods. It explains how to add Lombok dependencies and annotations like @NoArgsConstructor, @Getter, @Setter that generate specific code. The summary also mentions it generates code in class files instead of source files.

Uploaded by

yunus emre bey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Project Lombok

Date: 27th January 2022


SPICE UP YOUR JAVA !!!
Boilerplate Code

In computer programming, boilerplate code or just boilerplate are sections of


code that have to be included in many places with little or no alteration.
Examples :
• No-argument constructor
• An all field as argument constructor
• Getter for fields
• Setter for fields
• toString method
• equals() methods
Lombok

It is a java library that automatically plugs into your editor and build tools,
spicing up your java.
It is a boilerplate code remover.
It generated code like getters, setters, toString, etc.
IDE generates in our source code while Lombok generates it in the .class file
directly.
1- Add maven dependency
<dependencies>
SET-UP LOMBOK
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>

2- Install Plugin (IntelliJ -> Preferences -> Plugins -> Search Lombok - > Install)

3- Enable Annotation
Lombok Features

Please check all features : https://projectlombok.org/features/all

@NoArgsConstructor , @RequiredArgsConstructor, @AllArgsConstructor


@Getter, @Setter
@ToString
@Data
@EqualsAndHashCode
@Builder
@NoArgsConstructor

It will generate a no argument / default constructor.

static fields are not initialized within generated constructor for @NoArgsConstructor
@NoArgsConstructor
public class Student { public class Student {

private String name; private String name;


private int studentId; private int studentId;

} public Student() {}

With Lombok Without Lombok


@AllArgsConstructor

It will generate a constructor with one parameter for each field in your class.

It will not generate constructor argument for the statics fields.

It will not generate constructor argument for the final fields if they are initialized.
@AllArgsConstructor
public class Student { public class Student {

private String name; private String name;


private int studentId; private int studentId;
private boolean isRegistered; private boolean isRegistered;
}
public Student(String name, int studentId,
boolean isRegistered) {
this.name = name;
this.studentId = studentId;
this.isRegistered = isRegistered;
}
}

With Lombok Without Lombok


@RequiredArgsConstructor

It will generate a constructor with required arguments.

Required arguments are uninitialized final fields and fields with constrains such as
@NonNull.
@RequiredArgsConstructor
public class Student { public class Student {

private String name; private String name;


private final int studentId; private final int studentId;
private boolean isRegistered; private boolean isRegistered;

public Student(int studentId) {


} this.studentId = studentId;
}
}

With Lombok Without Lombok


@Getter / @Setter

It will generate a the default getter/setter automatically.


public class Student {
@Getter
@Setter private String name;
public class Student { private int studentId;

private String name; public String getName() {


private int studentId; return name;
} }

public void setName(String name) {


this.name = name;
}

public int getStudentId() {


return studentId;
}

public void setStudentId(int studentId) {


this.studentId = studentId;
}

With Lombok } Without Lombok


@Data

It will generate :
• getters for all fields
• toString method
hashCode and equals implementations
• setters for all non-final fields
• constructor(@RequiredArgsConstructor)
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@EqualsAndHashCode @Data
public class Student { public class Student {

private String name; private String name;


private final int studentId; private final int studentId;
private boolean isRegistered; private boolean isRegistered;
} }
@Builder

It produces the code automatically using Builder pattern. It can be applied at class
level, constructor level and method level.

Builder pattern is a commonly used creational design pattern in application


development which solves the instance creation problems.

You might also like