版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/w605283073/article/details/89221853
lombok@Data和@Builder一起用无法添加无参构造方法。
很多框架都是同反射等调用无参数构造方法来创建实例的,需要注意。
单独使用@Data注解,编译后的类文件会生成无参数构造方法(这并不是@Data提供的特性,而是Java自带的特性)。
public class Dog {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
public Dog() { /* compiled code */ }
public java.lang.String getName() { /* compiled code */ }
public java.lang.Integer getAge() { /* compiled code */ }
public java.lang.String getNickName() { /* compiled code */ }
public java.lang.String getTest() { /* compiled code */ }
public void setName(java.lang.String name) { /* compiled code */ }
public void setAge(java.lang.Integer age) { /* compiled code */ }
public void setNickName(java.lang.String nickName) { /* compiled code */ }
public void setTest(java.lang.String test) { /* compiled code */ }
public boolean equals(java.lang.Object o) { /* compiled code */ }
protected boolean canEqual(java.lang.Object other) { /* compiled code */ }
public int hashCode() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
}
单独使用@Builder注解,发现生成了全属性的构造方法。
public class Dog {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
Dog(java.lang.String name, java.lang.Integer age, java.lang.String nickName, java.lang.String test) { /* compiled code */ }
public static com.chujianyun.util.Dog.DogBuilder builder() { /* compiled code */ }
public static class DogBuilder {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
DogBuilder() { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder name(java.lang.String name) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder age(java.lang.Integer age) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder nickName(java.lang.String nickName) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder test(java.lang.String test) { /* compiled code */ }
public com.chujianyun.util.Dog build() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
}
}
@Data和@Builder一起用
@Data
@Builder
public class Dog {
private String name;
private Integer age;
private String nickName;
private String test;
}
效果
public class Dog {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
Dog(java.lang.String name, java.lang.Integer age, java.lang.String nickName, java.lang.String test) { /* compiled code */ }
public static com.chujianyun.util.Dog.DogBuilder builder() { /* compiled code */ }
public java.lang.String getName() { /* compiled code */ }
public java.lang.Integer getAge() { /* compiled code */ }
public java.lang.String getNickName() { /* compiled code */ }
public java.lang.String getTest() { /* compiled code */ }
public void setName(java.lang.String name) { /* compiled code */ }
public void setAge(java.lang.Integer age) { /* compiled code */ }
public void setNickName(java.lang.String nickName) { /* compiled code */ }
public void setTest(java.lang.String test) { /* compiled code */ }
public boolean equals(java.lang.Object o) { /* compiled code */ }
protected boolean canEqual(java.lang.Object other) { /* compiled code */ }
public int hashCode() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public static class DogBuilder {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
DogBuilder() { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder name(java.lang.String name) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder age(java.lang.Integer age) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder nickName(java.lang.String nickName) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder test(java.lang.String test) { /* compiled code */ }
public com.chujianyun.util.Dog build() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
}
}
我们发现外层的Dog类没有了默认的构造方法。
如果手动添加无参数构造方法或者用@NoArgsConstructor注解都会报错!
@Builder
@Data
@NoArgsConstructor
public class Dog {
private String name;
private Integer age;
private String nickName;
private String test;
}
正确写法如下:
@Builder
@Data
public class Dog {
private String name;
private Integer age;
private String nickName;
private String test;
@Tolerate
Dog() {}
}
解释如下
添加函数或者构造方法,让lombok假装它不存在(不感知)。
效果如下:
public class Dog {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
Dog() { /* compiled code */ }
Dog(java.lang.String name, java.lang.Integer age, java.lang.String nickName, java.lang.String test) { /* compiled code */ }
public static com.chujianyun.util.Dog.DogBuilder builder() { /* compiled code */ }
public java.lang.String getName() { /* compiled code */ }
public java.lang.Integer getAge() { /* compiled code */ }
public java.lang.String getNickName() { /* compiled code */ }
public java.lang.String getTest() { /* compiled code */ }
public void setName(java.lang.String name) { /* compiled code */ }
public void setAge(java.lang.Integer age) { /* compiled code */ }
public void setNickName(java.lang.String nickName) { /* compiled code */ }
public void setTest(java.lang.String test) { /* compiled code */ }
public boolean equals(java.lang.Object o) { /* compiled code */ }
protected boolean canEqual(java.lang.Object other) { /* compiled code */ }
public int hashCode() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public static class DogBuilder {
private java.lang.String name;
private java.lang.Integer age;
private java.lang.String nickName;
private java.lang.String test;
DogBuilder() { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder name(java.lang.String name) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder age(java.lang.Integer age) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder nickName(java.lang.String nickName) { /* compiled code */ }
public com.chujianyun.util.Dog.DogBuilder test(java.lang.String test) { /* compiled code */ }
public com.chujianyun.util.Dog build() { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
}
}