Lombok

Language: Java

CLI/Utils

Lombok was created to simplify Java development by automating repetitive coding tasks. It uses annotation processing to generate code at compile-time, keeping the source code clean and readable while maintaining full type safety.

Lombok is a Java library that reduces boilerplate code by generating commonly used methods like getters, setters, constructors, `toString`, `equals`, and `hashCode` using annotations.

Installation

maven: Add org.projectlombok:lombok dependency in pom.xml and enable annotation processing in IDE
gradle: Add implementation 'org.projectlombok:lombok:1.18.30' in build.gradle and enable annotation processing

Usage

Lombok provides annotations like `@Getter`, `@Setter`, `@NoArgsConstructor`, `@AllArgsConstructor`, `@Builder`, `@Data`, and more. These annotations automatically generate code during compilation, reducing boilerplate while keeping Java code clean.

Using @Getter and @Setter

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
    private int id;
    private String name;
}

Automatically generates getter and setter methods for `id` and `name` fields.

Using @NoArgsConstructor and @AllArgsConstructor

import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
}

Generates a no-argument constructor and an all-argument constructor automatically.

Using @ToString and @EqualsAndHashCode

import lombok.ToString;
import lombok.EqualsAndHashCode;

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

Automatically generates `toString()`, `equals()`, and `hashCode()` methods.

Using @Builder

import lombok.Builder;

@Builder
public class User {
    private int id;
    private String name;
}

User user = User.builder().id(1).name("Alice").build();

Enables the builder pattern for creating instances of `User`.

Using @Data

import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
}

`@Data` generates getters, setters, `toString()`, `equals()`, `hashCode()`, and required constructors in a single annotation.

Using @Value for immutable objects

import lombok.Value;

@Value
public class User {
    private int id;
    private String name;
}

`@Value` creates immutable classes with final fields, getters, `toString()`, `equals()`, `hashCode()`, and a constructor.

Customizing generated methods

import lombok.Getter;
@Getter(onMethod_ = {@Deprecated})
private int id;

Demonstrates adding custom annotations or behaviors to generated methods using Lombok features.

Error Handling

Compilation errors related to Lombok: Ensure annotation processing is enabled in the IDE and Maven/Gradle plugins are configured correctly.
IDE does not recognize generated methods: Rebuild the project or refresh IDE caches; ensure Lombok plugin is installed.

Best Practices

Use Lombok annotations to reduce boilerplate but avoid overusing `@Data` on entities with sensitive fields.

Enable annotation processing in your IDE for proper code generation.

Combine `@Builder` with `@AllArgsConstructor` for flexible object creation.

Prefer immutable classes (`@Value`) where possible for safer code.

Use `@Getter` and `@Setter` selectively for better control over field access.