咱们有一个学生类:java
@Builder public class Student { // 该字段有一个默认值; private int status = 1; } public class Test { @Test public void testBuilder() { // 生成的student对象并无使用status的默认值 Student student = Student.builder().build(); } }
使用@Builder以后会生成一个静态内部类StudentBuilder,编译以后咱们能够看到一共存在2个class文件。函数
将Student.class与StudentBuilder.class反编译以后的diamante以下ui
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类中规定的默认值1this
使用@Builder下面的@Default注解。spa
@Builder public class Student { @Default private int status = 1; }
编译以后,将Student.class与StudentBuilder.class编译以后的代码以下:code
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字段设置成默认值。对象