사용자 도구

사이트 도구


java:lombok

문서의 이전 판입니다!


Lombok

Gradle-Lombok

  • 혹은 다음과 같이 직접 설정
    compileOnly 'org.projectlombok:lombok:1.18.12'
    annotationProcessor 'org.projectlombok:lombok:1.18.12'
    
    testCompileOnly 'org.projectlombok:lombok:1.18.12'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

javac 메모리 문제

Disable Field

  • AccessLevel.NONE 설정으로 특정 필드 등에 대한 자동 메소드 생성을 막을 수 있다.
    // 모든 필드에 대해 Getter/Setter를 생성하지만 age 필드의 Setter는 생성하지 않는다.
    @Data
    public class Person {
        @Setter(AccessLevel.NONE)
        private int age;
    }

Maven 설정

주의점

  • 상호 참조하는 객체의 경우 toString() 호출시 Stack Overflow 가 발생할 수 있다. 한쪽 객체에서 다른 쪽 객체에 대해 @ToString(excluded={“propertyName”}) 형태로 출력시 제외토록 만들어야 한다.
  • @EqualsAndHashCode(of = {})로 꼭 필요한 필드만 비교하도록 처리한다.

PMD 문제 회피

  • Lombok으로 Getter/Setter를 만들면 PMD에서 Unused private field로 표시가 된다.
    @Data
    @SuppressWarnings("PMD.UnusedPrivateField")
    public class LombokUseClass {
    }
  • Signualr Field로 표시될 때는
    @Data
    @SuppressWarnings("PMD.SingularField")
    public class LombokUsedClass {
        private String field;
    }

Checkstyle 문제 회피

  • HideUtilityClassConstructor
    @SuppressWarnings("checkstyle:HideUtilityClassConstructor")

equals & hashCode Code Coverage

  • 다른 클래스를 상속할 때, 부모 클래스의 equals&hashCode를 그대로 사용하고자 하는 상황에서 Java FindBugs의 경고를 회피하려면 자식 클래스 쪽에 다음과 같이 설정한다.
    @EqualsAndHashCode(callSuper = true, of = {})
    • 여기서 of = { }를 넣지 않으면, 자식 클래스의 모든 필드가 비교 대상으로 들어가 버리게 된다.

Custom Handler

Default Value Builder & ETC

Delombok

  • 1.4 이하 Ant 태스크 : lombok.delombok.ant.DelombokTask
  • 1.6 이상 Ant 태스크 : lombok.delombok.ant.Tasks$Delombok

Upgrade 주의점

1.16.20 @ConstructorProperties

  • 이 시점 이후부터 @AllArgsConstructor, @RequiredArgsConstructor 등에서 자동으로 생성해주던 @ConstructorProperties(필드정보) 가 자동으로 생성이 안되게 바뀐다.
  • 기본 생성자가 없을 경우, Java Jackson JSON Library 등이
  • lombok.config에서 lombok.anyConstructor.addConstructorProperties=true를 명시적으로 주면 자동 생성된다.

1.18.4 field annotation 들이 getter/setter 로 복제됨

  • 1.18.4 이상 버전은 field 에 지정한 @JsonProperty 를 비롯한 여러 annotation이 getter/setter 로 복제가 된다.
  • 문제는 기존에 복제를 안해주는 버전으로 응답을 내려줄때, @JsonProperty값과 getter 의 이름이 다를 경우 두개가 다 외부 응답으로 나갔었는데, getter 의 이름으로 나가던 필드를 사용하는 타 팀이 존재할 경우 값자기 getter 이름쪽 응답이 사라져버린다.
BREAKING CHANGE: Lombok will now always copy specific annotations around (from field to getter, from field to builder 'setter', etcetera): A specific curated list of known annotations where that is the right thing to do (generally, @NonNull style annotations from various libraries), as well as any annotations you explicitly list in the lombok.copyableAnnotations config key in your lombok.config file. Also, lombok is more consistent about copying these annotations. (Previous behaviour: Lombok used to copy any annotation whose simple name was NonNull, Nullable, or CheckForNull). Issue #1570 and Issue #1634

구버전

@JsonProperty("xxx")
private int yyy;
 
-> 진짜 생성은
 
@JsonProperty("xxx")
private int yyy;
 
public void setYyy(int yyy) {
    this.yyy = yyy;
}
 
-> 이로 인해서 JSON 에 XXX, yyy 두개의 필드가 존재하게 됨.

신 버전 Lombok

@JsonProperty("xxx")
private int yyy;
 
-> 진짜 생성은
 
@JsonProperty("xxx")
private int yyy;
 
// 아래 setter 에도 @JsonProperty 가 붙어버림
@JsonProperty("xxx")
public void setYyy(int yyy) {
    this.yyy = yyy;
}
java/lombok.1612259109.txt.gz · 마지막으로 수정됨: 2021/02/02 18:45 저자 kwon37xi