Search code examples
javalombokbuilder

Change SuperBuilder method with Lombok


I have a class as follows:

@Data
@SuperBuilder
@NoArgsConstructor
@JsonInclude(Include.NON_NULL)
public class CustomObject extends Parent<CustomObject> {

  ...

  @Accessors(
      chain = true,
      prefix = {"_"})
  @Builder.Default
  private Boolean _default = false;
}

This generates a builder with a default() method that is not usable due to it being a reserved word. Is there a way to change this behavior so the builder works?


Solution

  • Unfortunately, there is no nice way in this case. @Accessors affects getters, setters and builders, because this is what you need in the vast majority of the cases. You cannot switch it off only for builders.

    This means the only way out here (apart from renaming the _default field) is to remove @Accessors and implement getters and setters manually:

    @Data
    @SuperBuilder
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class CustomObject {
    
        @Builder.Default
        @Getter(AccessLevel.NONE)
        @Setter(AccessLevel.NONE)
        private Boolean _default = false;
    
        public Boolean getDefault() {
            return _default;
        }
    
        public CustomObject setDefault(Boolean _default) {
            this._default = _default;
            return this;
        }
    }