사용자 도구

사이트 도구


java:lombok

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:lombok [2017/07/05 08:51]
kwon37xi
java:lombok [2022/06/23 11:10] (현재)
kwon37xi
줄 4: 줄 4:
   * [[java:lombok:pitfall|Lombok 사용상 주의점(Pitfall)]]   * [[java:lombok:pitfall|Lombok 사용상 주의점(Pitfall)]]
   * [[java:lombok:field_exist_verify|Lombok Field 지정이 올바른지 검사]]   * [[java:lombok:field_exist_verify|Lombok Field 지정이 올바른지 검사]]
 +  * [[java:lombok:upgrade|Lombok Upgrade]]
   * [[http://notatube.blogspot.kr/2010/12/project-lombok-creating-custom.html|Project Lombok: Creating Custom Transformations]] - 자신만의 Lombok 모듈 만들기   * [[http://notatube.blogspot.kr/2010/12/project-lombok-creating-custom.html|Project Lombok: Creating Custom Transformations]] - 자신만의 Lombok 모듈 만들기
 +  * [[https://www.baeldung.com/lombok-custom-annotation|Implementing a Custom Lombok Annotation | Baeldung]]
   * [[https://www.javacodegeeks.com/2016/06/lombok-autovalue-immutables.html|Lombok, AutoValue, and Immutables]]   * [[https://www.javacodegeeks.com/2016/06/lombok-autovalue-immutables.html|Lombok, AutoValue, and Immutables]]
 +
 +===== Gradle-Lombok =====
 +  * [[https://projectlombok.org/setup/gradle|lombok setup gradle]]
 +  * [[https://plugins.gradle.org/plugin/io.freefair.lombok|io.freefair.lombok gradle-lombok plugin]]
 +  * 혹은 다음과 같이 직접 설정<code>
 +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'
 +</code>
  
 ===== javac 메모리 문제 ===== ===== javac 메모리 문제 =====
줄 23: 줄 36:
 ===== Maven 설정 ===== ===== Maven 설정 =====
   * http://projectlombok.org/mavenrepo/index.html 있는 대로 설정하면 된다.   * http://projectlombok.org/mavenrepo/index.html 있는 대로 설정하면 된다.
-  * Java 1.6 이상에서만 작동한다. Java 코드가 1.6임을 명시하라.<code xml> 
-<build> 
-  <plugins> 
-    <plugin> 
-      <artifactId>maven-compiler-plugin</artifactId> 
-      <configuration> 
-        <source>1.6</source> 
-        <target>1.6</target> 
-        <encoding>utf-8</encoding> 
-        <!-- <compilerArgument>-proc:none</compilerArgument> -proc:none 사용금지 --> 
-      </configuration> 
-    </plugin> 
-  </plugins> 
-</build> 
- 
-</code> 
   * 만약 JPA Annotation Processor 때문에 maven-compiler-plugin 의** compilerArgument에 -proc:none을 지정했다면 삭제**하라.   * 만약 JPA Annotation Processor 때문에 maven-compiler-plugin 의** compilerArgument에 -proc:none을 지정했다면 삭제**하라.
  
줄 71: 줄 68:
 </code> </code>
     * 여기서 ''of = { }''를 넣지 않으면, 자식 클래스의 모든 필드가 비교 대상으로 들어가 버리게 된다.     * 여기서 ''of = { }''를 넣지 않으면, 자식 클래스의 모든 필드가 비교 대상으로 들어가 버리게 된다.
 +
 ===== Custom Handler ===== ===== Custom Handler =====
-  * [[http://notatube.blogspot.kr/2010/12/project-lombok-creating-custom.html|Project Lombok: Creating Custom Transformations]]+  *[[http://peichhorn.github.io/lombok-pg/|Lombok PG]] Custom handler 모음 
 +  * [[http://notatube.blogspot.kr/2010/12/project-lombok-creating-custom.html|Project Lombok: Creating Custom Transformations]] 프로젝트 구성에 대해 잘 설명함 
 +  * [[https://stackoverflow.com/questions/41243018/create-custom-annotation-for-lombok|java - Create custom annotation for Lombok]] 
 +  * [[https://binkley.blogspot.kr/2014/12/writing-your-own-lombok-annotation.html|binkley's BLOG: Writing your own lombok annotation]]
  
 +===== Default Value Builder & ETC=====
 +  * [[https://www.baeldung.com/lombok-builder-default-value|Lombok Builder with Default Value | Baeldung]] Builder 생성시 기본값 지정하는 방법
 +  * [[https://www.baeldung.com/lombok-builder-custom-setter|Lombok Builder with Custom Setter | Baeldung]]
  
 ===== Delombok ===== ===== Delombok =====
   * 1.4 이하 Ant 태스크 : ''lombok.delombok.ant.DelombokTask''   * 1.4 이하 Ant 태스크 : ''lombok.delombok.ant.DelombokTask''
   * 1.6 이상 Ant 태스크 : ''lombok.delombok.ant.Tasks$Delombok''   * 1.6 이상 Ant 태스크 : ''lombok.delombok.ant.Tasks$Delombok''
 +
 +==== Boolean 필드 문제 ====
 +  * [[https://www.baeldung.com/lombok-getter-boolean|Using Lombok's @Getter for Boolean Fields | Baeldung]]
 +  * ''boolean isRunning'' -> ''isRunning()'', ''setRunning()''
 +  * ''boolean running'' -> 위와 동일하게 ''isRunning()'', ''setRunning()''
 +  * 따라서 ''boolean isRunning''과 ''boolean running'' 이 둘 다 존재하면 컴파일 에러 발생.
 +  * ''Boolean running'' -> ''**get**Running()'', ''setRunning()''
 +
 +===== Upgrade 주의점 =====
 +  * [[https://projectlombok.org/changelog|lombok changelog]]
 +
 +==== 1.16.20 @ConstructorProperties ====
 +  * 이 시점 이후부터 ''@AllArgsConstructor'', ''@RequiredArgsConstructor'' 등에서 자동으로 생성해주던 ''@ConstructorProperties(필드정보)'' 가 자동으로 생성이 안되게 바뀐다.
 +  * [[https://docs.oracle.com/javase/7/docs/api/java/beans/ConstructorProperties.html|@ConstructorProperties]]
 +  * 기본 생성자가 없을 경우, [[java:jackson|Java Jackson JSON Library]] 등이 어떻게 필드에 값을 주입할 지 알 수 없는 상황이 된다.
 +  * ''lombok.config''에서 ''lombok.anyConstructor.addConstructorProperties=true''를 명시적으로 주면 자동 생성된다. 하지만 ''@XXXArgsConstructor, @Data, @Value''를 모두 없애고 생성자를 직접 만들어주고, 기본 생성자를 추가해주는 게 낫다.
 +
 +> BREAKING CHANGE: lombok config key lombok.anyConstructor.suppressConstructorProperties is now deprecated and defaults to true, that is, by default lombok no longer automatically generates @ConstructorProperties annotations. New config key lombok.anyConstructor.addConstructorProperties now exists; set it to true if you want the old behavior. Oracle more or less broke this annotation with the release of JDK9, necessitating this breaking change.
 +
 +==== 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
 +
 +=== 구버전 ===
 +
 +<code java>
 +@JsonProperty("isXxx")
 +private int isXxx;
 +
 +-> 진짜 생성은
 +
 +@JsonProperty("isXxx")
 +private int isXxx;
 +
 +public void setXxx(int xxx) {
 +    this.isXxx = xxx;
 +}
 +
 +-> 이로 인해서 JSON 에 ''isXxx'', ''xxx'' 두개의 필드가 존재하게 됨.
 +</code>
 +
 +=== 신 버전 Lombok ===
 +<code java>
 +@JsonProperty("isXxx")
 +private int isXxx;
 +
 +-> 진짜 생성은
 +
 +@JsonProperty("isXxx")
 +private int isXxx;
 +
 +// 아래 setter 에도 @JsonProperty 가 붙어버림
 +@JsonProperty("isXxx")
 +public void setXxx(int isXxx) {
 +    this.isXxx = isXxx;
 +}
 +</code>
 +  * 해결책은 getter/setter 자동생성을 막고, 명시적으로 getter/setter 를 생성하고 그 위에 ''@JsonProperty('isXxx')'' 명시해서 추가해줌.
 +  * 여기서 진짜 문제는 Jackson 이 ''isXxx'' 필드와 ''isXxx()'' 를 서로 다른 프라퍼티로 인식하는 것이다.
 +  * Java Bean/Jackson 은 규약상 ''isXxx'' 필드의 getter/setter 는 ''isIsXxx()'', ''setIsXxx()'' 여야하는게 맞기 때문에, ''isXxx()'' 를 ''isXxx'' 필드와 다른 프라퍼티로 인식하는 것이다.
 +  * 그런데 많은 사람들이 필드 이름을 지을 때 ''is로시작'' 하지만 프라퍼티 이름은 is를 빼고 만들고 싶어하는 경향이 있는데, 그게 Lombok 에 반영돼있어서 발생한 문제이다.
 +  * 따라서 이 문제는 **is로 시작하는 boolean 필드** 에서만 발생한다.
 +
java/lombok.1499214088.txt.gz · 마지막으로 수정됨: 2017/07/05 08:51 저자 kwon37xi