内容简介:我们有一个学生类:使用@Builder之后会生成一个静态内部类StudentBuilder,编译之后我们可以看到一共存在2个class文件。
前提
我们有一个学生类:
@Builder
public class Student {
// 该字段有一个默认值;
private int status = 1;
}
public class Test {
@Test
public void testBuilder() {
// 生成的student对象并没有使用status的默认值
Student student = Student.builder().build();
}
}
为什么@Builder没有使用到默认值?
使用@Builder之后会生成一个静态内部类StudentBuilder,编译之后我们可以看到一共存在2个class文件。
将Student.class与StudentBuilder.class反编译之后的diamante如下
public class Student {
private int status = 1;
// 注意:此处lombok生成了一个全参的构造函数
Student(int status) {
this.status = status;
}
public static StudentBuilder builder() {
return new StudentBuilder();
}
}
public class Student$StudentBuilder {
private int status;
public Student$StudentBuilder status(int status) {
this.status = status;
return this;
}
public Student build() {
return new Student(this.status);
}
public String toString() {
return "Student.StudentBuilder(status=" + this.status + ")";
}
}
在StudentBuilder代码的build()方法中可以看出来,生成Student对象时的status字段值是StudentBuilder中的status字段值。所以如果不通过status(int status)方法显式的设置status字段的话,最终生成的对象中的status值是 java 中int的默认值0,而不是在Student类中规定的默认值1
如何使默认值生效?
使用@Builder下面的@Default注解。
@Builder
public class Student {
@Default
private int status = 1;
}
编译之后,将Student.class与StudentBuilder.class编译之后的代码如下:
public class Student {
private int status;
// 返回status的默认值1
private static int $default$status() {
return 1;
}
Student(int status) {
this.status = status;
}
public static StudentBuilder builder() {
return new StudentBuilder();
}
// $FF: synthetic method
static int access$000() {
return $default$status();
}
}
public class Student$StudentBuilder {
private boolean status$set;
private int status;
public Student$StudentBuilder status(int status) {
this.status = status;
this.status$set = true;
return this;
}
public Student build() {
int status = this.status;
// 会判断stuStatus是否被显式的set,如果没有被set,则使用默认值。
if(!this.status$set) {
// 获取Student类中status的默认值1
status = Student.access$000();
}
return new Student(status);
}
public String toString() {
return "Student.StudentBuilder(status=" + this.status + ")";
}
}
在上文中可以看到Student类中有一个access$000()的方法,用来返回status字段的默认值1;然后再看StudentBuilder类,在build()方法的时候会判断是否显式的给status字段赋值,如果没有赋值则将status字段设置成默认值。
总结
- 如果想让类中的字段默认值生效,需要使用@Default注解
- @Builder会生成一个全参的构造函数
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Caching
Duane Wessels / O'Reilly Media, Inc. / 2001-6 / 39.95美元
On the World Wide Web, speed and efficiency are vital. Users have little patience for slow web pages, while network administrators want to make the most of their available bandwidth. A properly design......一起来看看 《Web Caching》 这本书的介绍吧!