spring lombok SuperBuilder

cornpip
|2023. 10. 8. 18:19

SuperBuilder 생성 코드

@SuperBuilder
@ToString
public class BuilderTestDto {
    private String one;
    private String two;
}

======================

@SuperBuilder
@ToString(callSuper = true)
public class BuilderTestDto2 extends BuilderTestDto{
    private String three;
}

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

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

 

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

 

//그냥 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 한다.

 

'Spring' 카테고리의 다른 글

Spring) Client와 WebSocket Server 사이를 중계하기  (1) 2023.12.04
spring mapstruct  (0) 2023.10.15
spring mongodb repository  (0) 2023.10.01
lombok @Builder  (0) 2023.08.06
Json 직렬/역직렬, @RequestBody, @ModelAttribute  (0) 2023.08.04