본문 바로가기

개발

lombok 기능들


@NonNull

NEW in Lombok 0.11.10: You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.

@NonNull을 메소드의 파라미터나 생성자에 사용하면 null-check statement를 만들어줌.

if (param == null) throw new NullPointerException("param"); 와 같은 역할을 함.


@Getter@Setter

You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically.

getter,setter 자동으로 생성.


@ToString


Any class definition may be annotated with @ToString to let lombok generate an implementation of the toString() method. By default, it'll print your class name, along with each field, in order, separated by commas.

어떤 클래스던 @tostring을 쓴다면 lombok이 tostring() 메소드를 만든다. 기본적으로 클래스 이름,  각자의 필드와 더불어서 순서대로 ',' 에 의해 분류된다.

By default, all non-static fields will be printed. If you want to skip some fields, you can name them in the exclude parameter

기본적으로 모든 static이 아닌 필드는 출력된다. 어떤 필드를 스킵하고 싶으면 exclude 파라미터를 사용하면 된다.  

ex> @ToString(exclude="id)

callSuper to true, you can include the output of the superclass implementation of toString to the output. Be aware that the default implementation of toString() in java.lang.Object is pretty much meaningless, so you probably don't want to do this unless you are extending another class.

ex> @ToString(callSuper=true, includeFieldNames=true) n 


@EqualsAndHashCode
Any class definition may be annotated with @EqualsAndHashCode to let lombok generate implementations of the equals(Object other) and hashCode() methods. By default, it'll use all non-static, non-transient fields, but you can exclude more fields by naming them in the optional exclude parameter to the annotation. 

Setting callSuper to true when you don't extend anything (you extend java.lang.Object) is a compile-time error, because it would turn the generated equals() and hashCode() implementations into having the same behaviour as simply inheriting these methods from java.lang.Object: only the same object will be equal to each other and will have the same hashCode.
equals와 hashCode를 생성.
callsuper를 true로 세팅하면 먼지 잘 모르겟음.


@NoArgsConstructor

@NoArgsConstructor will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead. For fields with constraints, such as @NonNull fields, no check or assignment is generated, so be aware that these constraints may then not be fulfilled until those fields are properly initialized later. Certain java constructs, such as hibernate and the Service Provider Interface require a no-args constructor. This annotation is useful primarily in combination with either @Data or one of the other constructor generating annotations.



@Data
@Data is like having implicit @Getter@Setter@ToString@EqualsAndHashCode and @RequiredArgsConstructor @Getter@Setter@ToString@EqualsAndHashCode and @RequiredArgsConstructor 가 모두 포함되어있음.

@Log
@CommonsLog
Creates private static final org.apache.commons.logging.Log log = 
    org.apache.commons.logging.LogFactory.getLog(LogExample.class);

@CommonLog를 쓰면 자동으로 위에 코드가 써지고 log.debug(); 로 코드를 쓸 수 있음.