Common Pitfalls When Using Lombok in Java Projects
This article outlines several common pitfalls of the Lombok library in Java, such as StackOverflowError from @ToString, missing no‑arg constructors with @AllArgsConstructor, hash‑based collection issues with @EqualsAndHashCode, and accessor mismatches caused by @Setter/@Getter, providing code examples and recommendations.
Lombok is a popular Java library that generates boilerplate code such as getters, setters, constructors, and toString methods during compilation by extending the annotation processing API.
The article explains several common pitfalls when using Lombok, illustrated with code examples.
Pitfall 1: @ToString can cause StackOverflowError – when applied to recursive data structures (e.g., a Node class with a parent reference), the generated toString method traverses the cycle and overflows the stack. The issue can be avoided by excluding the parent field:
@ToString(exclude = "parent")Example of a recursive Node class that triggers the problem:
@ToString
@Setter
@Getter
public static class Node {
private Object vale;
private Node left;
private Node parent;
private Node right;
}Pitfall 2: Using @AllArgsConstructor without @NoArgsConstructor – the generated class lacks a no‑argument constructor, which many frameworks (e.g., deserialization libraries) require, leading to failures.
Pitfall 3: @EqualsAndHashCode affecting hash‑based collections – if fields participating in equals/hashCode change after insertion into a HashMap or HashSet, the entry becomes unreachable. The article shows a demo where modifying a field after putting the object into a map makes the lookup fail.
package com.example.demo;
import lombok.*;
import java.util.HashMap;
import java.util.Map;
public class Demo {
public static void main(String[] args) throws ReflectiveOperationException {
Map
map = new HashMap<>();
Node node = new Node();
node.setVale("who");
map.put(node, node);
Node node1 = map.get(node);
System.out.println(node1);
node.setVale("are");
node1 = map.get(node);
System.out.println(node1);
}
@Setter
@Getter
@EqualsAndHashCode
@ToString
public static class Node {
private Object vale;
}
}Pitfall 4: @Setter/@Getter on fields whose name starts with a lowercase letter followed by an uppercase letter – Lombok generates accessor methods with a different capitalisation than IDEs or MyBatis expect, causing serialization mismatches. Example:
@Setter
@Getter
@EqualsAndHashCode
@ToString
public static class Node {
private Object nName;
}The generated bytecode shows a getNName method (capital N) while standard tools expect getnName, leading to failures in frameworks like MyBatis.
The article concludes that while Lombok reduces boilerplate, developers should verify the generated bytecode and be aware of these traps; the project’s coding standards even forbid Lombok in favor of IDE‑generated code.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.