BackendBench
Coffee

Unravel › Java concepts › OOP and types

Java language

Lombok annotations

Lombok writes boilerplate for you at compile time.

🔎 Unravel this example

At a glance

Mental model

A stencil for the repetitive parts of a drawing.

What happens, in order
  1. 1@Getter and @Setter generate accessors for fields; @ToString and @EqualsAndHashCode generate those methods from the fields; @Data bundles all of them plus a required-arguments constructor
  2. 2@RequiredArgsConstructor builds a constructor for all final fieldsIn Spring beans this is the usual way to get constructor injection without writing the constructor.
  3. 3@Builder adds a fluent builder: Order.builder().id(1).status("NEW").build(), helpful for classes with many fields
  4. 4@Slf4j creates a log field for the class, which is why code can call log.info(...) without declaring a logger
Remember
Watch out
@Data on JPA entitiesSearching for a getter that does not exist in the sourceUsing @Builder and then constructing objects with missing required valuesMixing Lombok with a changed field type and forgetting that generated code changes too
Example (highlighted lines explain it)
@Service@RequiredArgsConstructor@Slf4jpublic class InvoiceService {    private final InvoiceRepository repository;   // constructor generated by Lombok    private final MailClient mail;    public void send(long id) {        log.info("Sending invoice {}", id);        // 'log' generated by @Slf4j        mail.send(repository.findById(id).orElseThrow());    }}
  1. line 2 uses RequiredArgsConstructor, the core of Lombok annotations
  2. line 3 uses Slf4j, the core of Lombok annotations
  3. line 5 constructor generated by Lombok
  4. line 9 uses Slf4j, the core of Lombok annotations
Read the full explanation (about 2 min)

Lombok writes boilerplate for you at compile time. Annotations such as @Getter, @Builder or @RequiredArgsConstructor generate the methods and constructors you would otherwise type by hand, so the source stays short but the compiled class contains them.

A mental model

A stencil for the repetitive parts of a drawing. You mark where the standard shapes go and the tool fills them in, so you only draw what is special.

How it works

@Getter and @Setter generate accessors for fields; @ToString and @EqualsAndHashCode generate those methods from the fields; @Data bundles all of them plus a required-arguments constructor.

@RequiredArgsConstructor builds a constructor for all final fields. In Spring beans this is the usual way to get constructor injection without writing the constructor.

@Builder adds a fluent builder: Order.builder().id(1).status("NEW").build(), helpful for classes with many fields.

@Slf4j creates a log field for the class, which is why code can call log.info(...) without declaring a logger.

Going deeper

Because the methods are generated, searching the source for getName finds nothing. If you cannot find where something is defined, check for Lombok before assuming the code is missing.

@Data on a JPA entity is a trap: the generated equals, hashCode and toString touch every field, including lazy relations, which can trigger queries or infinite recursion between two entities that point at each other. Use targeted annotations on entities.

@Builder bypasses constructor validation unless you put the checks in the constructor the builder calls, and makes it easy to forget a required field at run time.

Records (Java 16+) now cover much of what @Value and @Data did for plain data classes, with no extra dependency.

Under the hood

Lombok is an annotation processor that hooks into the compiler and edits the syntax tree before bytecode is produced. The class file therefore really contains the generated members, but your source file does not.

Common mistakes

  • @Data on JPA entities.
  • Searching for a getter that does not exist in the source.
  • Using @Builder and then constructing objects with missing required values.
  • Mixing Lombok with a changed field type and forgetting that generated code changes too.

How Unravel recognises it

When you paste code, Unravel looks for these signs of lombok annotations. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Lombok annotations?

Lombok writes boilerplate for you at compile time. Annotations such as @Getter, @Builder or @RequiredArgsConstructor generate the methods and constructors you would otherwise type by hand, so the source stays short but the compiled class contains them.

What are the most common mistakes with Lombok annotations?

@Data on JPA entities. Searching for a getter that does not exist in the source. Using @Builder and then constructing objects with missing required values. Mixing Lombok with a changed field type and forgetting that generated code changes too.

How can I see Lombok annotations in real code?

Paste your own Java into Unravel and it will point out where lombok annotations appears, explain how it works at the detail level you choose, and draw the flow of the code around it.