Why is Lombok @Builder not compatible with this constructor?

前端 未结 3 1172
挽巷
挽巷 2021-01-04 00:16

I have this simple code:

@Data
@Builder
public class RegistrationInfo {

    private String mail;
    private String password;

    public RegistrationInfo(R         


        
相关标签:
3条回答
  • 2021-01-04 00:33

    Presumably, @Builder generates an all-args constructor iff there are no other constructors defined.

    @Data
    @Builder
    @AllArgsConstructor(access = AccessLevel.PRIVATE)
    class RegistrationInfo {
    
        private String mail;
        private String password;
    
        private RegistrationInfo(RegistrationInfo registrationInfo) {
            this(registrationInfo.mail, registrationInfo.password);
        }
    }
    
    0 讨论(0)
  • 2021-01-04 00:43

    You can either add an @AllArgsConstructor annotation, because

    @Builder generates an all-args constructor iff there are no other constructors defined.

    (Quotting @Andrew Tobilko)

    Or set an attribute to @Builder : @Builder(toBuilder = true) This gives you the functionality of a copy constructor.

    @Builder(toBuilder = true)
    class Foo {
        // fields, etc
    }
    
    Foo foo = getReferenceToFooInstance();
    Foo copy = foo.toBuilder().build();
    
    0 讨论(0)
  • 2021-01-04 00:51

    When you provide your own constructor then Lombok doesn't create a c-tor with all args that @Builder is using. So you should just add annotation @AllArgsConstructor to your class:

    @Data
    @Builder
    @AllArgsConstructor
    public class RegistrationInfo {
        //...
    }
    
    0 讨论(0)
提交回复
热议问题