8

I have the following classes:

@SuperBuilder(toBuilder = true)
public abstract class Parent {
    //...
}

@SuperBuilder(toBuilder = true)
public class Child extends Parent {
    //...
}

@SuperBuilder(toBuilder = true)
public class Child2 extends Parent {
    //...
}

Why am I unable to call toBuilder() on an instance of the abstract class (Parent) as shown in the following code?

public copy(Parent parent) {
    parent.toBuilder().build();
}

2 Answers 2

10

In fact, as Hossein Nasr already explained, Lombok cannot know whether there are subclasses that do not have toBuilder=true.

Lombok could require all direct subclasses of an abstract class to also use toBuilder by adding an abstract toBuilder() method on the abstract class. However, there may be use-cases where subclasses should not have a toBuilder (although I haven't seen any good examples of those). Furthermore, indirect subclasses may still lack the toBuilder feature. Therefore, lombok does not enforce toBuilder on subclasses of abstract classes.

Good news is that you can easily work around it in your case (only direct subclasses) by adding the abstract toBuilder() method to your abstract class Parent manually:

@SuperBuilder(toBuilder = true)
public abstract class Parent {
    public abstract ParentBuilder<?, ?> toBuilder();
}

With this change, your copy method compiles and works as expected.

If you also have indirect subclasses, you have to find other means to ensure they also have @SuperBuilder(toBuilder = true). If those would not have this annotation, you may experience strange behavior (e.g. calling copy() will instantiate a different class).

Disclaimer: I implemented the @SuperBuilder feature.

7
  • Would making the direct subclasses final to avoid indirect subclass issues make sense or would that break something that takes place under the covers in Lombok? Commented May 7, 2020 at 7:28
  • You can do that, it won't break anything. However, it's no guarantee, because someone could still inherit from your abstract base class, doing funny things with toBuilder() or something similar.
    – Jan Rieke
    Commented May 7, 2020 at 9:44
  • Could you explain why (or if) this works only for direct subclasses? Commented Apr 26, 2022 at 17:25
  • I think you mean that you'd need to add a method to each abstract class in the hierarchy, which is no big deal. I think all this should be documented. And this answer should be shortened to just say, "declare it yourself." Commented Apr 26, 2022 at 17:36
  • No, you just need to put it on the topmost abstract class. I added a paragraph on indirect subclasses (those will work if they also have @SuperBuilder(toBuilder = true)).
    – Jan Rieke
    Commented Apr 28, 2022 at 7:24
4

It's probably because Lombok can not guarantees that every child class of Parent is also marked as @SuperBuilder(toBuilder=true) and if so, Lombok can not call the toBuilder of that instance;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.