일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- oracle
- write by GPT-4
- 시스템
- 유닉스
- kotlin
- flet
- 웹 크롤링
- write by chatGPT
- GPT-4's answer
- 코틀린
- chatGPT's answer
- jpa
- JVM
- 데이터베이스
- NIO
- 소프트웨어공학
- 리눅스
- 자바네트워크
- spring integration
- python
- 인프라
- 자바암호
- 파이썬
- 신재생에너지 발전설비 기사
- Java
- 역학
- spring data jpa
- 자바
- 고전역학
- Database
- Today
- Total
기억을 지배하는 기록
Lombok features - @NonNull 본문
@NonNull
or: How I learned to stop worrying and love the NullPointerException.
@NonNull was introduced in lombok v0.11.10.
Overview
You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.
Lombok has always treated various annotations generally named @NonNull on a field as a signal to generate a null-check if lombok generates an entire method or constructor for you, via for example @Data. Now, however, using lombok's own @lombok.NonNull on a parameter results in the insertion of just the null-check statement inside your own method or constructor.
The null-check looks like if (param == null) throw new NullPointerException("param is marked @NonNull but is null"); and will be inserted at the very top of your method. For constructors, the null-check will be inserted immediately following any explicit this() or super() calls.
If a null-check is already present at the top, no additional null-check will be generated.
With Lombok
import lombok.NonNull; public class NonNullExample extends Something { private String name; public NonNullExample(@NonNull Person person) { super("Hello"); this.name = person.getName(); } } |
Vanilla Java
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(); } } |
'Library' 카테고리의 다른 글
Lombok features - @EqualsAndHashCode (0) | 2020.12.21 |
---|---|
Lombok features - @ToString (0) | 2020.12.21 |
Lombok features - @Getter and @Setter (0) | 2020.12.21 |
Lombok features - @Cleanup (0) | 2020.12.09 |
Lombok features - val & var (0) | 2020.12.09 |