Lombok Annotation 의 Delombok 코드 보기 (어노테이션을 사용함으로써 적용될 코드)

반응형
728x90
반응형

lombok

DTO, Entity, Form 등 사용시에 필수가 된 lombok의 어노테이션을 선언함으로써 생성되는 코드들을 자세하게 알아보자.

 

 

build.gradle 의존성 추가

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

testCompileOnly 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'

 

 

TestDto.java 파일

public class TestDto {
    @NonNull
    private String name;
    private String email;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
}

 

 

1) @Getter, @Setter

@Getter
@Setter

 

Delombok 된 코드
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

 

 

 

2) @ToString

@ToString

 

Delombok 된 코드
    @Override // Object 클래스가 상속되어 있으므로, toString 오버라이드
    public String toString() {
        return getClass().getName() + " : " + "name: " + name + ", email: " + email
                + ", createdAt : " + createdAt + ", updatedAt: " + updatedAt;
    }

 

 

 

3) @NoArgsConstructor

@NoArgsConstructor

 

Delombok 된 코드
public User() {}

 

 

 

4) @AllArgsConstructor

@AllArgsConstructor

 

Delombok 된 코드
public User(@NonNull String name, String email, LocalDateTime createdAt, LocalDateTime updatedAt) {
  this.name = name;
  this.email = email;
  this.createdAt = createdAt;
  this.updatedAt = updatedAt;
}

 

 

 

5) @RequiredArgsConstructor

@RequiredArgsConstructor

 

Delombok 된 코드
public User(@NonNull String name) {
	this.name = name;
}

 

 

 

6) @EqualsAndHashCode

@EqualsAndHashCode

 

Delombok 된 코드
    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof User)) return false;
        final User other = (User) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$name = this.getName();
        final Object other$name = other.getName();
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        final Object this$email = this.getEmail();
        final Object other$email = other.getEmail();
        if (this$email == null ? other$email != null : !this$email.equals(other$email)) return false;
        final Object this$createdAt = this.getCreatedAt();
        final Object other$createdAt = other.getCreatedAt();
        if (this$createdAt == null ? other$createdAt != null : !this$createdAt.equals(other$createdAt)) return false;
        final Object this$updatedAt = this.getUpdatedAt();
        final Object other$updatedAt = other.getUpdatedAt();
        if (this$updatedAt == null ? other$updatedAt != null : !this$updatedAt.equals(other$updatedAt)) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $name = this.getName();
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        final Object $email = this.getEmail();
        result = result * PRIME + ($email == null ? 43 : $email.hashCode());
        final Object $createdAt = this.getCreatedAt();
        result = result * PRIME + ($createdAt == null ? 43 : $createdAt.hashCode());
        final Object $updatedAt = this.getUpdatedAt();
        result = result * PRIME + ($updatedAt == null ? 43 : $updatedAt.hashCode());
        return result;
    }

 

 

 

7) @Data

@Data

Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.

 

 

Delombok 된 코드
    public UserData() {
    }

    public String getName() {
        return this.name;
    }

    public String getEmail() {
        return this.email;
    }

    public LocalDateTime getCreatedAt() {
        return this.createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return this.updatedAt;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public boolean equals(final Object o) {
        if (o == this) return true;
        if (!(o instanceof UserData)) return false;
        final UserData other = (UserData) o;
        if (!other.canEqual((Object) this)) return false;
        final Object this$name = this.getName();
        final Object other$name = other.getName();
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        final Object this$email = this.getEmail();
        final Object other$email = other.getEmail();
        if (this$email == null ? other$email != null : !this$email.equals(other$email)) return false;
        final Object this$createdAt = this.getCreatedAt();
        final Object other$createdAt = other.getCreatedAt();
        if (this$createdAt == null ? other$createdAt != null : !this$createdAt.equals(other$createdAt)) return false;
        final Object this$updatedAt = this.getUpdatedAt();
        final Object other$updatedAt = other.getUpdatedAt();
        if (this$updatedAt == null ? other$updatedAt != null : !this$updatedAt.equals(other$updatedAt)) return false;
        return true;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof UserData;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $name = this.getName();
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        final Object $email = this.getEmail();
        result = result * PRIME + ($email == null ? 43 : $email.hashCode());
        final Object $createdAt = this.getCreatedAt();
        result = result * PRIME + ($createdAt == null ? 43 : $createdAt.hashCode());
        final Object $updatedAt = this.getUpdatedAt();
        result = result * PRIME + ($updatedAt == null ? 43 : $updatedAt.hashCode());
        return result;
    }

    public String toString() {
        return "UserData(name=" + this.getName() + ", email=" + this.getEmail() + ", createdAt=" + this.getCreatedAt() + ", updatedAt=" + this.getUpdatedAt() + ")";
    }

 

 

 

8) @Builder

@Builder

 

Delombok 된 코드
    public static UserBuilderBuilder builder() {
        return new UserBuilderBuilder();
    }


    public static class UserBuilderBuilder {
        private @NonNull String name;
        private String email;
        private LocalDateTime createdAt;
        private LocalDateTime updatedAt;

        UserBuilderBuilder() {
        }

        public UserBuilderBuilder name(@NonNull String name) {
            this.name = name;
            return this;
        }

        public UserBuilderBuilder email(String email) {
            this.email = email;
            return this;
        }

        public UserBuilderBuilder createdAt(LocalDateTime createdAt) {
            this.createdAt = createdAt;
            return this;
        }

        public UserBuilderBuilder updatedAt(LocalDateTime updatedAt) {
            this.updatedAt = updatedAt;
            return this;
        }

        public UserBuilder build() {
            return new UserBuilder(name, email, createdAt, updatedAt);
        }

        public String toString() {
            return "UserBuilder.UserBuilderBuilder(name=" + this.name + ", email=" + this.email + ", createdAt=" + this.createdAt + ", updatedAt=" + this.updatedAt + ")";
        }
    }

 

 

 

반응형

Designed by JB FACTORY