@Builder @Data public class TestLombok { @Tolerate TestLombok() { } ...... }
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class TestLombok { ...... }
使用Lombok注解能够极高的简化代码量,比较好用的注解除了@Data以外,还有@Builder这个注解,它可让你很方便的使用builder模式构建对象,可是今天发现@Builder注解会把对象的默认值清掉。java
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class TestLombok { private String aa = "zzzz"; public static void main(String[] args) { TestLombok build = TestLombok.builder().build(); System.out.println(build); }
输出:TestLombok(aa=null)app
只须要在字段上面加上@Builder.Default注解便可ui
@Builder.Default private String aa = "zzzz";
咱们使用注解的方式,底层本质就是反射帮咱们生成了一系列的setter、getter,因此咱们直接打开编译后的target包下面的.class文件,上面的全部缘由一目了然!
源文件:this
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class TestLombok { private String aa = "zzzz"; public static void main(String[] args) { TestLombok build = TestLombok.builder().build(); System.out.println(build); } }
对应的class字节码:code
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.apple.ucar; public class TestLombok { private String aa = "zzzz"; public static void main(String[] args) { TestLombok build = builder().build(); System.out.println(build); } public static TestLombok.TestLombokBuilder builder() { return new TestLombok.TestLombokBuilder(); } public String getAa() { return this.aa; } public void setAa(String aa) { this.aa = aa; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof TestLombok)) { return false; } else { TestLombok other = (TestLombok)o; if (!other.canEqual(this)) { return false; } else { Object this$aa = this.getAa(); Object other$aa = other.getAa(); if (this$aa == null) { if (other$aa != null) { return false; } } else if (!this$aa.equals(other$aa)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof TestLombok; } public int hashCode() { int PRIME = true; int result = 1; Object $aa = this.getAa(); int result = result * 59 + ($aa == null ? 43 : $aa.hashCode()); return result; } public String toString() { return "TestLombok(aa=" + this.getAa() + ")"; } public TestLombok() { } public TestLombok(String aa) { this.aa = aa; } public static class TestLombokBuilder { private String aa; TestLombokBuilder() { } public TestLombok.TestLombokBuilder aa(String aa) { this.aa = aa; return this; } public TestLombok build() { return new TestLombok(this.aa); } public String toString() { return "TestLombok.TestLombokBuilder(aa=" + this.aa + ")"; } } }
咱们想知道@Data、@Builder等注解底层到底作了什么,直接编译当前文件,便可在生成的.class字节码文件查看具体代码便知道了对象
好比上述第二点,采用@Builder的时候,这个aa并无默认值,因此会为空!!get
public TestLombok.TestLombokBuilder aa(String aa) { this.aa = aa; return this; }
我的以为若是想要使用@Builder,最简单的方法就是直接写上这4个注解,有默认值的话再加上@Builder.Default直接,正常状况下就没啥问题了!hash
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class TestLombok { @Builder.Default private String aa = "zzzz"; public static void main(String[] args) { TestLombok build = TestLombok.builder().build(); System.out.println(build); } }