[Proposal] Lombok constructor generation for subclasses extending @RequiredArgsConstructor superclasses

14 views
Skip to first unread message

Robert Fišer

unread,
Jul 31, 2025, 7:31:03 PMJul 31
to Project Lombok

Hello Lombok team,

In many Spring Boot projects, we follow a common pattern:

  • We define a @Component or @Service as an implementation of an interface

  • It often extends an abstract base class that contains shared logic

  • That base class has final dependencies injected via constructor (annotated with @RequiredArgsConstructor)

Example:

@RequiredArgsConstructor
public abstract class AbstractHandler {
    private final SomeService someService;
    private final AnotherDependency anotherDependency;
}


Now we implement the concrete class:
@Service
public class ConcreteHandler extends AbstractHandler {
    private final ExtraDependency extra;

    // Lombok can't help here — we need to write the constructor manually
    public ConcreteHandler(SomeService someService,
                           AnotherDependency anotherDependency,
                           ExtraDependency extra) {
        super(someService, anotherDependency);
        this.extra = extra;
    }
}


Problem

We repeat this pattern frequently. But Lombok currently can’t help us generate constructors that:

  • call the superclass constructor with its required fields

  • assign subclass final fields

This results in a significant amount of repetitive and error-prone boilerplate code in subclass constructors.

Proposal

Introduce support for automatic constructor generation in subclasses, such as:

  • @RequiredArgsConstructor(callSuper = true)

  • or a new annotation like @SuperRequiredArgsConstructor

This would instruct Lombok to:

  • include all final fields from the subclass

  • call the only constructor of the superclass (if and only if it has exactly one constructor)

Constraint

To avoid ambiguity:

This feature would only be available when the superclass has exactly one constructor.
In other cases, Lombok would fail compilation and require explicit constructor definition by the developer.

Benefit

This small enhancement would be a big productivity boost in Spring-based codebases that heavily rely on constructor injection and inheritance-based composition. It enables clean, consistent, and DRY code.

Would this be feasible to implement?

Thanks for your excellent work — Lombok is indispensable in modern Java projects.

Best regards,
Robert Fišer

Reply all
Reply to author
Forward
0 new messages