Spring) Lombok SuperBuilder
Spring) Lombok SuperBuilder
This post was migrated from Tistory. You can find the original here.
Generated code for SuperBuilder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@SuperBuilder
@ToString
public class BuilderTestDto {
private String one;
private String two;
}
======================
@SuperBuilder
@ToString(callSuper = true)
public class BuilderTestDto2 extends BuilderTestDto{
private String three;
}
Both the parent and the child need the annotation attached, and here’s what the compiled code looks like once you use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class BuilderTestDto2 extends BuilderTestDto {
private String three;
protected BuilderTestDto2(final BuilderTestDto2Builder<?, ?> b) {
super(b);
this.three = b.three;
}
public static BuilderTestDto2Builder<?, ?> builder() {
return new BuilderTestDto2BuilderImpl();
}
public String toString() {
String var10000 = super.toString();
return "BuilderTestDto2(super=" + var10000 + ", three=" + this.three + ")";
}
public abstract static class BuilderTestDto2Builder<C extends BuilderTestDto2, B extends BuilderTestDto2Builder<C, B>> extends BuilderTestDto.BuilderTestDtoBuilder<C, B> {
private String three;
public BuilderTestDto2Builder() {
}
public B three(final String three) {
this.three = three;
return this.self();
}
protected abstract B self();
public abstract C build();
public String toString() {
String var10000 = super.toString();
return "BuilderTestDto2.BuilderTestDto2Builder(super=" + var10000 + ", three=" + this.three + ")";
}
}
private static final class BuilderTestDto2BuilderImpl extends BuilderTestDto2Builder<BuilderTestDto2, BuilderTestDto2BuilderImpl> {
private BuilderTestDto2BuilderImpl() {
}
protected BuilderTestDto2BuilderImpl self() {
return this;
}
public BuilderTestDto2 build() {
return new BuilderTestDto2(this);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class BuilderTestDto {
private String one;
private String two;
protected BuilderTestDto(final BuilderTestDtoBuilder<?, ?> b) {
this.one = b.one;
this.two = b.two;
}
public static BuilderTestDtoBuilder<?, ?> builder() {
return new BuilderTestDtoBuilderImpl();
}
public String toString() {
return "BuilderTestDto(one=" + this.one + ", two=" + this.two + ")";
}
public abstract static class BuilderTestDtoBuilder<C extends BuilderTestDto, B extends BuilderTestDtoBuilder<C, B>> {
private String one;
private String two;
public BuilderTestDtoBuilder() {
}
public B one(final String one) {
this.one = one;
return this.self();
}
public B two(final String two) {
this.two = two;
return this.self();
}
protected abstract B self();
public abstract C build();
public String toString() {
return "BuilderTestDto.BuilderTestDtoBuilder(one=" + this.one + ", two=" + this.two + ")";
}
}
private static final class BuilderTestDtoBuilderImpl extends BuilderTestDtoBuilder<BuilderTestDto, BuilderTestDtoBuilderImpl> {
private BuilderTestDtoBuilderImpl() {
}
protected BuilderTestDtoBuilderImpl self() {
return this;
}
public BuilderTestDto build() {
return new BuilderTestDto(this);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//plain builder
public class JustBuilder {
private String one;
private String two;
JustBuilder(final String one, final String two) {
this.one = one;
this.two = two;
}
public static JustBuilderBuilder builder() {
return new JustBuilderBuilder();
}
public static class JustBuilderBuilder {
private String one;
private String two;
JustBuilderBuilder() {
}
public JustBuilderBuilder one(final String one) {
this.one = one;
return this;
}
public JustBuilderBuilder two(final String two) {
this.two = two;
return this;
}
public JustBuilder build() {
return new JustBuilder(this.one, this.two);
}
public String toString() {
return "JustBuilder.JustBuilderBuilder(one=" + this.one + ", two=" + this.two + ")";
}
}
}
It generates noticeably more complex code than a plain builder.
Unlike the plain builder, which just returns a static class, this one has an abstract class and returns a static implementation class instead.
This post is licensed under CC BY-NC 4.0 by the author.