简体   繁体   中英

Java Lombok library : builder annotation issues with Inheritance

Java Lombok library : builder annotation issues with Inheritance

    @Builder
    public class ParentClass {
        private final String a;
        private final String b;
    }

    @Builder
    public class ChildClass extends ParentClass{
        private final String c;
    }

When creating an instance of child class, parent class attributes are not visible with Builder annotations.

Below fails:

     ChildClass.builder().a("testA").b("testB").c("testC").build();

However, below statement is correct:

     ChildClass.builder().c("testC").build();

Seems this issue is open for long time, dont know if any latest release has any fixes.

The latest lombok release 1.18.2 includes the new experimental @SuperBuilder . It was added exactly for this: setting fields from superclasses.

The problem is that when you define ParentClass with @Builder annotation it creates ParentClass(String) constructor and deletes implicit one. Then Child class cannot even be created.

Please look at the following answer: how to Call super constructor in Lombok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM