Overriding Builder.Default in subclass

Skip to first unread message

Mat Jaggard

May 11, 2021, 7:57:30 PM5/11/21
to Project Lombok
I have a field called "type" in my parent class "Amendment" which I'd like to be set to a particular value for each subclass. Is it possible to set the Builder.Default value based on which subclass is being used?

I've tried with an abstract method called amendmentType() which is overridden by each subclass, but if I annotate it with @Builder.Default then I get an error because the builder is static and the method is not.

Originally I passed this in through the constructor, but I can't do that now because @Builder.Default doesn't work with custom written constructors.

@SuperBuilder(toBuilder = true) //Warning Builder.Default is not compatible with hand-written constructors
public class Amendment {
    //lots of other fields annotated with @Builder.Default

    //@Builder.Default //Not possible here.
    AmendmentType type = amendmentType();
    abstract AmendmentType amendmentType();

    //public Amendment(AmendmentType type) {
    //    this.type = type;

@SuperBuilder(toBuilder = true)
public class AAmendment extends Amendment {
    //lots of other fields annotated with @Builder.Default

    //public AAmendment() {
    //   super(AmendmentType.A);
// This works for the type field but non of the other fields have their default values populated.

    AmendmentType amendmentType() {
        return AmendmentType.A;
Reply all
Reply to author
0 new messages