How can I create a Lombok builder for a third party class (i.e. I cannot modify its source code)?
I have an existing class that I cannot change:
public class ThirdPartyPojo {
// one of many properties
private String name;
public ThirdPartyPojo() {
// default no-args constructor
}
String getName() {
return this.name;
}
void setName(String name) {
this.name = name;
}
// many more getters and setters
}
Now I would like to create a @Builder so that I get a fluent builder API to simplify instantiation of ThirdPartyPojo with default values.
This is what I tried:
@Builder
public class ThirdPartyPojoBuilder extends ThirdPartyPojo {
@Default
private String name = "default name";
// many more default values for other properties
}
The code compiles and I am able to reference the builder, e.g.
ThirdPartyPojo pojoWithDefaultName = ThirdPartyPojoBuilder.builder().build();
ThirdPartyPojo pojoWithCustomName = ThirdPartyPojoBuilder.builder().name("custom name").build();
System.out.println(pojoWithDefaultName.getName());
System.out.println(pojoWithCustomName.getName());
However, this does not work as getName() returns null for both pojoWithDefaultName and pojoWithCustomName.