0

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.

Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38

2 Answers2

2

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

Jan Rieke
  • 7,027
  • 2
  • 20
  • 30
  • Thanks Jan. I was able to resolve above issue with @SuperBuilder annotation introduced as experimental in lombok release 1.18.2. – Amit Kaneria Aug 07 '18 at 14:05
1

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

Mike
  • 812
  • 9
  • 25