0% found this document useful (0 votes)
17 views4 pages

Lombok Tutorial

Project Lombok is a Java library that reduces boilerplate code by generating common methods through annotations. It provides various core and advanced annotations such as @Getter, @Setter, @Builder, and @Value to simplify Java development. The tutorial also covers setup instructions for Maven, Gradle, and IDEs, along with common pitfalls to avoid when using Lombok.

Uploaded by

newsletter
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)
17 views4 pages

Lombok Tutorial

Project Lombok is a Java library that reduces boilerplate code by generating common methods through annotations. It provides various core and advanced annotations such as @Getter, @Setter, @Builder, and @Value to simplify Java development. The tutorial also covers setup instructions for Maven, Gradle, and IDEs, along with common pitfalls to avoid when using Lombok.

Uploaded by

newsletter
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/ 4

Project Lombok Tutorial

What is Lombok?

Lombok is a Java library that helps reduce boilerplate code in Java projects by automatically generating

common methods like getters, setters, constructors, equals(), hashCode(), toString(), and more using simple

annotations.

Lombok Setup

Maven:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

Gradle:

compileOnly 'org.projectlombok:lombok:1.18.30'

annotationProcessor 'org.projectlombok:lombok:1.18.30'

IDE Setup:

- IntelliJ IDEA: Install the Lombok plugin and enable annotation processing.

- Eclipse: Run lombok.jar and integrate it with Eclipse.

Core Annotations

- @Getter / @Setter

Automatically generates getters/setters.

@Getter @Setter
public class User {
private String name;
private int age;
}
Project Lombok Tutorial

- @ToString

Generates toString() method.

@ToString
public class User {
private String name;
private int age;
}

- @EqualsAndHashCode

Generates equals() and hashCode().

@EqualsAndHashCode
public class User {
private String name;
private int age;
}

- @NoArgsConstructor / @AllArgsConstructor / @RequiredArgsConstructor

@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
public class Product {
private final String name;
private double price;
}

- @Data

Combines: @Getter, @Setter, @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor.

@Data
public class Employee {
private final String name;
private int salary;
}

Advanced Annotations

- @Builder

Implements builder pattern.


Project Lombok Tutorial

@Builder
public class Book {
private String title;
private String author;
}

Usage:

Book book = Book.builder()

.title("Clean Code")

.author("Robert C. Martin")

.build();

- @Value

Immutable class (like @Data + final + private + no setters).

@Value
public class Address {
String city;
String state;
}

- @SneakyThrows

Allows throwing checked exceptions without declaring them.

@SneakyThrows
public void readFile() {
Files.readAllLines(Paths.get("file.txt"));
}

- @Cleanup

Automatically calls close() on a resource.

@Cleanup
InputStream in = new FileInputStream("test.txt");

Use with JPA


Project Lombok Tutorial

@Entity
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
@GeneratedValue
private Long id;
private String name;
@ToString.Exclude
private String password;
}

Note: Be cautious with @Data in JPA due to issues with lazy loading and bidirectional relationships.

Common Pitfalls

- IDE issues: Ensure plugin is installed and annotation processing enabled.

- @Data with JPA may cause side effects.

- Avoid using Lombok with Java Records.

Summary

Annotation Purpose

@Getter/@Setter Generate getters/setters

@ToString Generate toString()

@EqualsAndHashCode Generate equals/hashCode

@NoArgsConstructor No-arg constructor

@AllArgsConstructor All-args constructor

@RequiredArgsConstructor Constructor for final/non-null fields

@Data Common annotation combination

@Builder Builder pattern

@Value Immutable object

@SneakyThrows Handle checked exceptions

@Cleanup Auto resource close

You might also like