I was setting the value of recordId from the child classes using the default constructor and was not using lombok @Builder initially. Eventually i decided to use the Builder here, but the problem now is lombok Builder overrides my default constructor internally hence the value is never set.
How can I put any hook too make lombok @Builder use my default constructor?
Parent class:
@Getter
@Setter
public abstract class Record {
private String recordId;
}
Child class:
@Getter
@Setter
@Builder
@ToString
@AllArgsConstructor
public class SRecord extends Record {
private static final String RECORD_ID = "REC001";
private String street;
private String city;
public SRecord() {
setRecordId(RECORD_ID); //value of recordId being set
}
}
Lombok's
@Buildersimply does not use the default constructor. It passes its values to an all-args constructor so that this constructor can fill the new instance with these values.@Builderdoes not use setters or direct access to the fields to do so. So your default constructor is simply ignored by@Builder.What you can do is write your own all-args constructor. In it, you set your value for
recordIdand assign the rest of the fields from the parameters.