Why is Lombok @Builder not compatible with this constructor?

好久不见. 提交于 2019-12-05 05:39:31

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();

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 {
    //...
}

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);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!