일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
- 소프트웨어공학
- 뉴턴역학
- Spring boot
- 코틀린
- write by chatGPT
- android
- 파이썬
- 인프라
- 시스템
- GIT
- JVM
- lombok
- Java
- 자바
- kotlin
- GPT-4's answer
- 리눅스
- 유닉스
- chatGPT's answer
- python
- write by GPT-4
- 고전역학
- 자바네트워크
- oracle
- NIO
- flet
- Database
- 자바암호
- 역학
- 웹 크롤링
- Today
- Total
Akashic Records
Lombok features - @With 본문
Immutable 'setters' - methods that create a clone but with one changed field.
@Wither was introduced as experimental feature in lombok v0.11.4.
@Wither was renamed to @With, and moved out of experimental and into the core package, in lombok v1.18.10.
Overview
The next best alternative to a setter for an immutable property is to construct a clone of the object, but with a new value for this one field. A method to generate this clone is precisely what @With generates: a withFieldName(newValue) method which produces a clone except for the new value for the associated field.
For example, if you create public class Point { private final int x, y; }, setters make no sense because the fields are final. @With can generate a withX(int newXValue) method for you which will return a new point with the supplied value for x and the same value for y.
The @With relies on a constructor for all fields in order to do its work. If this constructor does not exist, your @With annotation will result in a compile time error message. You can use Lombok's own @AllArgsConstructor, or as Value will automatically produce an all args constructor as well, you can use that too. It's of course also acceptable if you manually write this constructor. It must contain all non-static fields, in the same lexical order.
Like @Setter, you can specify an access level in case you want the generated with method to be something other than public:
@With(level = AccessLevel.PROTECTED). Also like @Setter, you can also put a @With annotation on a type, which means a with method is generated for each field (even non-final fields).
To put annotations on the generated method, you can use onMethod=@__({@AnnotationsHere}). Be careful though! This is an experimental feature. For more details see the documentation on the onX feature.
javadoc on the field will be copied to generated with methods. Normally, all text is copied, and @param is moved to the with method, whilst @return lines are stripped from the with method's javadoc. Moved means: Deleted from the field's javadoc. It is also possible to define unique text for the with method's javadoc. To do that, you create a 'section' named WITH. A section is a line in your javadoc containing 2 or more dashes, then the text 'WITH', followed by 2 or more dashes, and nothing else on the line. If you use sections, @return and @param stripping / copying for that section is no longer done (move the @param line into the section).
With Lombok
import lombok.AccessLevel; import lombok.NonNull; import lombok.With; public class WithExample { @With(AccessLevel.PROTECTED) @NonNull private final String name; @With private final int age; public WithExample(String name, int age) { if (name == null) throw new NullPointerException(); this.name = name; this.age = age; } } |
Vanilla Java
import lombok.NonNull; public class WithExample { private @NonNull final String name; private final int age; public WithExample(String name, int age) { if (name == null) throw new NullPointerException(); this.name = name; this.age = age; } protected WithExample withName(@NonNull String name) { if (name == null) throw new java.lang.NullPointerException("name"); return this.name == name ? this : new WithExample(name, age); } public WithExample withAge(int age) { return this.age == age ? this : new WithExample(name, age); } } |
Supported configuration keys:
lombok.with.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @With as a warning or error if configured.
'Library' 카테고리의 다른 글
Top 5 Java recursion examples (0) | 2021.01.22 |
---|---|
Docker Compose 설치 (0) | 2021.01.07 |
Lombok features - @Synchronized (0) | 2020.12.21 |
Lombok features - @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor (0) | 2020.12.21 |
Lombok features - @EqualsAndHashCode (0) | 2020.12.21 |