You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Inconsistent use of toBuilder in a class hierarchy that uses @SuperBuilder leads to uninitialized instances. #4041
Describe the bug
In a class hierarchy where all classes are annotated with @SuperBuilder, but some have toBuilder=true set and some do not, the classes missing toBuilder will not be initialized.
Unfortunately, this is a problem lombok cannot solve.
When lombok runs, there is no semantic information available, i.e. lombok simply does not know which class you are referencing in your extends clause. lombok purely relies on the syntax (i.e. the names), so if it sees a extends Derived, lombok only knows that the annotated class extends another class that is named Derived. The semantic reference is determined in later stages of the compilation process by the compiler.
Lombok could try guessing (there are already some cases where lombok does this), but this may not be accurate, and I do not think this is worth it for just adding a warning.
Thanks for the response. You're right, I missed it in the docs "Using toBuilder requires that all superclasses also have toBuilder = true." Maybe a little stronger language in there, though? I read that to mean it won't work, as opposed to it'll compile silently but give you classes that are likely to throw NPEs at run time.
We already use Checkstyle. I'll look into adding a rule to that.
Activity
janrieke commentedon May 22, 2026
Unfortunately, this is a problem lombok cannot solve.
When lombok runs, there is no semantic information available, i.e. lombok simply does not know which class you are referencing in your
extendsclause. lombok purely relies on the syntax (i.e. the names), so if it sees aextends Derived, lombok only knows that the annotated class extends another class that is namedDerived. The semantic reference is determined in later stages of the compilation process by the compiler.Lombok could try guessing (there are already some cases where lombok does this), but this may not be accurate, and I do not think this is worth it for just adding a warning.
So this is a requirement lombok users must be aware of. We documented this requirement here.
What you could do is propose a new rule for your favorite linter tool. Or you could write an ArchUnit rule that checks this requirement.
Jose123456 commentedon May 22, 2026
Thanks for the response. You're right, I missed it in the docs "Using toBuilder requires that all superclasses also have toBuilder = true." Maybe a little stronger language in there, though? I read that to mean it won't work, as opposed to it'll compile silently but give you classes that are likely to throw NPEs at run time.
We already use Checkstyle. I'll look into adding a rule to that.