在实际开发中,有时候需要对子类使用lombok的 @Builder注解来使用builder模式构造该子类对象。
父类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Parent { private Long id; private String name; }
子类
import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @EqualsAndHashCode(callSuper = true) @Data @NoArgsConstructor @Builder public class Child extends Parent{ }
此时虽然在子类上添加了@Builder注解,但是由于子类没有属性,如下图所示,无法使用builder模式。
二、分析
通过阅读 lombok.Builder的源码,可知 @Builder 注解不仅可以用在类上,还可以用在构造函数上。
因此尝试如下写法:
@EqualsAndHashCode(callSuper = true) @Data @NoArgsConstructor @Builder public class Child extends Parent { @Builder private Child(Long id, String name) { super(id, name); } }
再次运行上面的单元测试,发现支持了 builder 模式,但是奇怪的是,单测不通过。
java.lang.AssertionError:
Expected :1024
Actual :null
因此我们观察一下 Child.class 反编译后的代码:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.chujianyun.libs.lombok; public class Child extends Parent { private Child(Long id, String name) { super(id, name); } public static Child.ChildBuilder builder() { return new Child.ChildBuilder(); } public boolean equals(final Object o) { if (o == this) { return true; } else if (!(o instanceof Child)) { return false; } else { Child other = (Child)o; if (!other.canEqual(this)) { return false; } else { return super.equals(o); } } } protected boolean canEqual(final Object other) { return other instanceof Child; } public int hashCode() { int result = super.hashCode(); return result; } public String toString() { return "Child()"; } public Child() { } public static class ChildBuilder { private Long id; private String name; ChildBuilder() { } public Child build() { return new Child(); } public String toString() { return "Child.ChildBuilder()"; } public Child.ChildBuilder id(final Long id) { this.id = id; return this; } public Child.ChildBuilder name(final String name) { this.name = name; return this; } } }
找到了原因,同时在子类和全参数的构造函数使用 @Builder 注解,会有 BUG,即最终的 build() 函数只是返回了空参的构造函数创建了一个 Child 对象,因此属性“采用 builder 方式设置的 id 和 name” 最终“丢失”。
那么如何解决这个问题呢?
我们再次回到@Builder 源码的注释上:
If a member is annotated, it must be either a constructor or a method. If a class is annotated, * then a private constructor is generated with all fields as arguments * (as if {@code @AllArgsConstructor(access = AccessLevel.PRIVATE)} is present * on the class), and it is as if this constructor has been annotated with {@code @Builder} instead.
可知,将其加到类上,相当于包含所有属性的私有构造方法,且构造方法上加上 @Builder 注解。
因此我们写的代码可能有冲突,我们修改如下:
import lombok.Builder; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; @EqualsAndHashCode(callSuper = true) @Data @NoArgsConstructor public class Child extends Parent { @Builder private Child(Long id, String name) { super(id, name); } }
最终单测通过
我们观察一下此时编译后的代码:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.chujianyun.libs.lombok; public class Child extends Parent { private Child(Long id, String name) { super(id, name); } public static Child.ChildBuilder builder() { return new Child.ChildBuilder(); } public boolean equals(final Object o) { if (o == this) { return true; } else if (!(o instanceof Child)) { return false; } else { Child other = (Child)o; if (!other.canEqual(this)) { return false; } else { return super.equals(o); } } } protected boolean canEqual(final Object other) { return other instanceof Child; } public int hashCode() { int result = super.hashCode(); return result; } public String toString() { return "Child()"; } public Child() { } public static class ChildBuilder { private Long id; private String name; ChildBuilder() { } public Child.ChildBuilder id(final Long id) { this.id = id; return this; } public Child.ChildBuilder name(final String name) { this.name = name; return this; } public Child build() { return new Child(this.id, this.name); } public String toString() { return "Child.ChildBuilder(id=" + this.id + ", name=" + this.name + ")"; } } }
此时的build() 函数才是我们需要的状态。
从编译后的代码我们可以清晰地看出 lombok 通过@Builder 实现的 builder模式的核心逻辑。
即构造内部类,在内部类赋值属性,build时调用含有所有属性的构造方法创建对象。
更多细节可以仔细查看 @Builder 注解的源码和注释,查看官方的手册 https://projectlombok.org/features/Builder
三、总结遇到诡异的问题一定不要轻易放过。
分析问题要有步骤,比如可以看源码中是否有说明,也可以看编译后的代码,还可以通过反汇编等,观察注解对类文件作出了哪些影响。还可以去看官方手册。
-----------------------------------------------------