Satish Patro
Satish Patro

Reputation: 4414

Lombok builder pattern issue

I have added Lombok's JAR file in STS (eclipse). I am using Lombok to create object using builder(). But, I am facing issue in inheritance. If I am using Lombok's builder pattern to create objects it's working in workspace & in executable JAR file. But, If I am using Lombok's builder pattern to create objects which inherit another object, then it's not working.

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class BaseEmp {
    private int a;
    private int b;
}


@Data
@NoArgsConstructor
@Builder
class Emp extends BaseEmp implements Serializable {

    private static final long serialVersionUID = 1L;

    @Builder
    public Emp(int a, int b) {
        super(a, b);
    }

}

Emp emp = Emp.builder.a(ipA).b(ipB).build();

In this one when I am printing object, a and b values are null in JAR and working in STS.

But, when I converted to normal object creation in workspace and JAR, in both places it is working.

Means, upon compile, Lombok processor somehow misses inheritance class field.

Upvotes: 0

Views: 1557

Answers (1)

Jan Rieke
Jan Rieke

Reputation: 8142

If you extend another class, you should really think about using @SuperBuilder. Although it is still experimental, the Lombok maintainers made clear that this is mainly because it is a very young, extremely complex feature that will not receive support/bugfixes as fast as the core features. It is unlikely that @SuperBuilder will be redesigned or dropped in the future.

However, if you want to stick with @Builder, you must not have @Builder annotations on both the class and the constructor. Just put it on the constructor and it should work.

Furthermore, your superclass should also not have @Builder, otherwise you'll get a name clash on the builder() method. (You can work around that by renaming it using the parameter builderMethodName.)

Upvotes: 2

Related Questions