포스트

spring lombok SuperBuilder

spring lombok SuperBuilder

이 글은 Tistory에서 이전된 포스팅입니다. 원본은 여기에서 확인할 수 있습니다.

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;
}

부모와 자식 둘 다 붙여야하고 사용했을 때 컴파일 된 코드는 아래와 같다.

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
//그냥 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 + ")";
        }
    }
}

그냥 builder 보다 복잡하게 코드를 생성한다.

바로 정적 클래스를 return하던 builder 와 다르게 추상 클래스가 있고 정적 구현체 class 를 return 한다.

이 기사는 저작권자의 CC BY-NC 4.0 라이센스를 따릅니다.