记lombok@Data和@Builder一起用无法添加无参构造方法的坑

版权声明:本文为博主原创文章,遵循 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 */ }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值