@NonNull
변수 앞에 붙이면 자동으로 null-check를 해주며
해당 변수의 value가 null인 경우 NullPointerException
예외를 발생시킨다. 아래 공식 홈페이지 글을 읽어보자~
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}
Lombok 사용 시
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
}
Vanilla Java
fields(변수)
들이final
로 선언된 경우compiler error
가 발생한다. 이럴 경우 @NoArgsConstructor(force = true)로 선언한다. value = 0 / false / null로 초기화된다.
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.NonNull;
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
@NoArgsConstructor
public static class NoArgsExample {
@NonNull private String field;
}
}
With Lombok
public class ConstructorExample<T> {
private int x, y;
@NonNull private T description;
private ConstructorExample(T description) {
if (description == null) throw new NullPointerException("description");
this.description = description;
}
public static <T> ConstructorExample<T> of(T description) {
return new ConstructorExample<T>(description);
}
@java.beans.ConstructorProperties({"x", "y", "description"})
protected ConstructorExample(int x, int y, T description) {
if (description == null) throw new NullPointerException("description");
this.x = x;
this.y = y;
this.description = description;
}
public static class NoArgsExample {
@NonNull private String field;
public NoArgsExample() {
}
}
}
Vanilla Java
*** 해당 lombok어노테이션을 사용하거나 이클립스나 인텔리제이에서 지원하는 단축키를 사용해서 생성할 수도 있다.
아니면 직접 하나하나 코드를 작성하거나 ㅠㅜㅠ