I am trying to implement the builder pattern using Lombok's @Builder but it does not detect any of the record fields:
@Builder(builderMethodName = "internalBuilder")
public record ApiError(String title, Map<String, String> errors) {
public static ApiErrorBuilder builder(String title) {
return internalBuilder().title(title); // Cannot resolve method 'title' in 'ApiErrorBuilder'
}
}
When I turn record to a class, everything works as expected:
@Builder(builderMethodName = "internalBuilder")
public class ApiError {
private final String title;
private final Map<String, String> errors;
public ApiError(String title, Map<String, String> errors) {
this.title = title;
this.errors = errors;
}
public static ApiErrorBuilder builder(String title) {
return internalBuilder().title(title);
}
// getters
}
Is this happening because Lombok currently does not work well with records yet?
I am using IntelliJ and Lombok 1.18.22
It works again!
@Builder
public record MyRecord(String myField) {
}
It was a known IntelliJ bug. There was, however, a workaround:
public record MyRecord(String myField) {
@Builder public MyRecord {}
}
Important: Once you insert the @builder
inside the record, you must remove the @builder
above it