@Builder
@Data
public class TestLombok {
@Tolerate
TestLombok() {
}
......
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
......
}
使用Lombok注解可以极高的简化代码量,比较好用的注解除了@Data之外,还有@Builder这个注解,它可以让你很方便的使用builder模式构建对象,但是今天发现@Builder注解会把对象的默认值清掉。
@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)
只需要在字段上面加上@Builder.Default注解即可
@Builder.Default
private String aa = "zzzz";
我们使用注解的方式,底层本质就是反射帮我们生成了一系列的setter、getter,所以我们直接打开编译后的target包下面的.class文件,上面的所有原因一目了然!
源文件:
@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字节码:
//
// 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并没有默认值,所以会为空!!
public TestLombok.TestLombokBuilder aa(String aa) {
this.aa = aa;
return this;
}
个人觉得如果想要使用@Builder,最简单的方法就是直接写上这4个注解,有默认值的话再加上@Builder.Default直接,正常情况下就没啥问题了!
@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);
}
}
在开发中,如果遇到需要使用canvas同时绘制多张图片,但因为图片大小的不一样,...
windows 下默认的滚动条样式巨丑,项目中又有比较多地方会显示滚动条, 故回头翻...
Dreamweaver网页中的banner图片需要切换,我们可以添加按钮来切换图片,下面我们...
随着网络时代的发展与进步,我们的学习工作和生活早已离不开互联网,智能家居、...
这是一款基于HTML5和JavaScript的进度条应用,这款进度条插件非常有特点,它在进...
1、使用css精灵。 好处是将css中使用的小图片可以合并为一张大图片减少了对服务...
还记得我在《2020 年 JavaScript 状态调研报告小结》中提到的 2020 年全球开发者...
dreamweaver软件: 点此下载 1、熟悉网页设计的网友就知道,调用STYLE的方法很多...
Do not use these html elements in html pages. Presentational elements shoul...
macromedia dreamweaver 8序列号 激活码: wpd800-50438-28032-39991 wpd800-599...