Skip to content

[BUG] Inconsistent use of toBuilder in a class hierarchy that uses @SuperBuilder leads to uninitialized instances. #4041

Open

Description

@Jose123456

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.

To Reproduce

import lombok.ToString;
import lombok.experimental.SuperBuilder;

class TestSuperBuilder
{
    @SuperBuilder(toBuilder = true)
    @ToString
    static class Base
    {
        String foo;
    }

    @SuperBuilder
    @ToString(callSuper = true)
    static class Derived extends Base
    {
        String bar;
    }

    @SuperBuilder(toBuilder = true)
    @ToString(callSuper = true)
    static class Derived2 extends Derived
    {
        String baz;
    }

    public static void main(String[] args) throws Exception
    {
        Derived2 d = Derived2.builder().foo("foo").bar("bar").baz("baz").build();
        System.out.println(d);

        Derived2 d2 = d.toBuilder().baz("baz2").build();
        System.out.println(d2);
    }
}

Output:

TestSuperBuilder.Derived2(super=TestSuperBuilder.Derived(super=TestSuperBuilder.Base(foo=foo), bar=bar), baz=baz)
TestSuperBuilder.Derived2(super=TestSuperBuilder.Derived(super=TestSuperBuilder.Base(foo=foo), bar=null), baz=baz2) 

Expected behavior
At least a warning would be nice. This can cause very tricky bugs.

Version info (please complete the following information):

  • Lombok 1.18.39
  • Eclipse / IntelliJ

Activity

  1. janrieke commented on May 22, 2026

    @janrieke
    Contributor

    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.

    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.

  2. Jose123456 commented on May 22, 2026

    @Jose123456
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions