This is possible with lombok 1.18.2 (and above) using the new (experimental) annotation @SuperBuilder
. The only restriction is that every class in the hierarchy must have the @SuperBuilder
annotation. There is no way around putting @SuperBuilder
on all subclasses, because Lombok cannot know all subclasses at compile time.
See the lombok documentation for details.
Example:
@SuperBuilder
public abstract class Superclass {
private int field1;
}
@SuperBuilder
public class Subclass extends Superclass {
private int field2;
}
Subclass instance = Subclass.builder().field1(1).field2(2).build();