北京中交建设工程咨询有限公司网站,wordpress页面文件夹,网页打不开connection,洛阳瀍河建设局网站现在很多程序员都习惯使用Lombok来使代码更加 “简洁”。但是使用Lombok也会造成很多问题#xff0c;尤其Builder 有个很大的坑#xff0c;已经见过好几次由于使用Builder注解导致默认值失效的问题#xff0c;如果测试时没有在意这个问题#xff0c;就很容易引发线上问题。…现在很多程序员都习惯使用Lombok来使代码更加 “简洁”。但是使用Lombok也会造成很多问题尤其Builder 有个很大的坑已经见过好几次由于使用Builder注解导致默认值失效的问题如果测试时没有在意这个问题就很容易引发线上问题。
问题复现
我们随便定义一个类Config对其中两个属性设置默认值。
import lombok.Builder;
import lombok.Data;Data
Builder
public class Config {private boolean isOpen true;private String name;private int value 20;
}public class LombokDemo {public static void main(String[] args) {Config config Config.builder().name(test).build();System.out.println(config);}
}借助Builder模式创建Config类实例时仅设置name属性然后打印出实例
public class LombokDemo {public static void main(String[] args) {Config config Config.builder().name(test).build();System.out.println(config);}
}输出结果如下。
Config(isOpenfalse, nametest, value0)我们为isOpen及value属性设置的默认值失效了。
原因分析
想了解为什么会这样我们只需要查看使用Lombok的注解后的Config类的 class文件长啥样就明白了。Builder通过Lombok的注解处理器在编译时会自动生成一个静态内部类这个内部类就是所谓的builder类它包含了和被注解的类中的属性一一对应的setter方法并且在build()方法中返回一个被注解的类的对象。这个builder类的代码实现是通过Lombok生成的所以我们不需要手动编写。 public class Config {private boolean isOpen true;private String name;private int value 20;Config(boolean isOpen, String name, int value) {this.isOpen isOpen;this.name name;this.value value;}public static ConfigBuilder builder() {return new ConfigBuilder();}public boolean isOpen() {return this.isOpen;}public String getName() {return this.name;}public int getValue() {return this.value;}public void setOpen(boolean isOpen) {this.isOpen isOpen;}public void setName(String name) {this.name name;}public void setValue(int value) {this.value value;}public boolean equals(Object o) {// 省略}protected boolean canEqual(Object other) {return other instanceof Config;}public int hashCode() {// 省略}public String toString() {return Config(isOpen this.isOpen() , name this.getName() , value this.getValue() );}public static class ConfigBuilder {private boolean isOpen;private String name;private int value;ConfigBuilder() {}public ConfigBuilder isOpen(boolean isOpen) {this.isOpen isOpen;return this;}public ConfigBuilder name(String name) {this.name name;return this;}public ConfigBuilder value(int value) {this.value value;return this;}public Config build() {return new Config(this.isOpen, this.name, this.value);}public String toString() {return Config.ConfigBuilder(isOpen this.isOpen , name this.name , value this.value );}}
}
可以看到ConfigBuilder中isOpen和value属性并没有使用我们想要设置的默认值。调用build方法时 ConfigBuilder会调用全参的构造方法来构造Config 对象。
解决方法
使用Builder.Default注解来标识带默认值的属性
import lombok.Builder;
import lombok.Data;Data
Builder
public class SomeConfig {Builder.Defaultprivate boolean isOpen true;private String name;Builder.Defaultprivate int value 20;
}修改后输出结果如下
Config(isOpentrue, nametest, value20)为什么加了Builder.Default注解就能解决问题呢看一下编译后的class文件就明白了
public class Config {private boolean isOpen;private String name;private int value;private static boolean $default$isOpen() {return true;}private static int $default$value() {return 20;}Config(boolean isOpen, String name, int value) {this.isOpen isOpen;this.name name;this.value value;}public static ConfigBuilder builder() {return new ConfigBuilder();}public boolean isOpen() {return this.isOpen;}public String getName() {return this.name;}public int getValue() {return this.value;}public void setOpen(boolean isOpen) {this.isOpen isOpen;}public void setName(String name) {this.name name;}public void setValue(int value) {this.value value;}public boolean equals(Object o) {// 省略}protected boolean canEqual(Object other) {return other instanceof Config;}public int hashCode() {// 省略}public String toString() {boolean var10000 this.isOpen();return Config(isOpen var10000 , name this.getName() , value this.getValue() );}public static class ConfigBuilder {private boolean isOpen$set;private boolean isOpen$value;private String name;private boolean value$set;private int value$value;ConfigBuilder() {}public ConfigBuilder isOpen(boolean isOpen) {this.isOpen$value isOpen;this.isOpen$set true;return this;}public ConfigBuilder name(String name) {this.name name;return this;}public ConfigBuilder value(int value) {this.value$value value;this.value$set true;return this;}public Config build() {boolean isOpen$value this.isOpen$value;if (!this.isOpen$set) {isOpen$value Config.$default$isOpen();}int value$value this.value$value;if (!this.value$set) {value$value Config.$default$value();}return new Config(isOpen$value, this.name, value$value);}public String toString() {return Config.ConfigBuilder(isOpen$value this.isOpen$value , name this.name , value$value this.value$value );}}
}每个设置默认值的属性都会在Builder中加上是否设置的标记如果没有主动设置值则调用Config中的默认值的静态方法进行赋值然后再调用Config全参构造方法构造该对象。
使用Builder注解的缺点
如果在类上使用了Builder 注解那么你需要手动添加一个无参构造函数否则有些序列化框架需要通过newInstance构造对象时会报错。如果在类上使用了Builder注解就不能再在构造函数或方法上使用 Builder注解否则会导致重复生成构造器类如果在类上使用了Builder 注解想给某个属性设置一个默认值还需要在属性上使用Builder.Default 注解否则默认值会被忽略。如果想让子类继承父类的属性那么你需要在子类的全参构造函数上使用 Builder注解并且在父类上使用AllArgsConstructor注解否则子类的构造器类不会包含父类的属性