Skip to content

Commit da5d9a1

Browse files
committed
[With] updated documentation for Wither/With
1 parent 461b554 commit da5d9a1

File tree

8 files changed

+90
-101
lines changed

8 files changed

+90
-101
lines changed

doc/changelog.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Lombok Changelog
22
----------------
33

44
### v1.18.9 "Edgy Guinea Pig"
5+
* PROMOTION: `@Wither` has been promoted to the main package, renamed to `@With`. Otherwise, no changes have been made to the annotation. The old experimenetal annotation will remain for a few versions as a deprecated annotation. If you had `lombok.config` configuration for this annotation, the configuration keys for this feature have been renamed.
56
* FEATURE: You can now configure a custom logger framework using the new `@CustomLog` annotation in combination with the `lombok.log.custom.declaration` configuration key. See the [log documentation](https://projectlombok.org/features/Log) for more information. [Pullrequest #2086](https://github.com/rzwitserloot/lombok/pull/2086) with thanks to Adam Juraszek.
67
* ENHANCEMENT: Thanks to Mark Haynes, the `staticConstructor` will now also be generated if a (private) constructor already exists. [Issue #2100](https://github.com/rzwitserloot/lombok/issues/2100)
78
* ENHANCEMENT: `val` is now capable of decoding the type of convoluted expressions (particularly if the right hand side involves lambdas and conditional (ternary) expressions). [Pull Request #2109](https://github.com/rzwitserloot/lombok/pull/2109) and [Pull Request #2138](https://github.com/rzwitserloot/lombok/pull/2138) with thanks to Alexander Bulgakov.

website/extra/htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ RewriteRule ^features/${pg?no_esc}(\.html)?/?$ /features/${pg?no_esc} [NC,R=301]
5959
RewriteRule ^features/experimental/Builder(\.html)?/?$ /features/Builder [NC,R=301]
6060
RewriteRule ^features/experimental/Value(\.html)?/?$ /features/Value [NC,R=301]
6161
RewriteRule ^features/experimental/var(\.html)?/?$ /features/var [NC,R=301]
62+
RewriteRule ^features/experimental/Wither(\.html)?/?$ /features/With [NC,R=301]
6263

6364
RewriteRule ^features/experimental/all$ /features/experimental/index.html [L,END]
6465
RewriteRule ^features/experimental(/all)?/?$ /features/experimental/all [NC,R=301]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<#import "_features.html" as f>
2+
3+
<@f.scaffold title="@With" logline="Immutable 'setters' - methods that create a clone but with one changed field.">
4+
<@f.history>
5+
<p>
6+
<code>@Wither</code> was introduced as experimental feature in lombok v0.11.4.
7+
</p><p>
8+
<code>@Wither</code> was renamed to <code>@With</code>, and moved out of experimental and into the core package, in lombok v1.18.10.
9+
</@f.history>
10+
11+
<@f.overview>
12+
<p>
13+
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 <code>@With</code> generates: a <code>withFieldName(newValue)</code> method which produces a clone except for the new value for the associated field.
14+
</p><p>
15+
For example, if you create <code>public class Point { private final int x, y; }</code>, setters make no sense because the fields are final. <code>@With</code> can generate a <code>withX(int newXValue)</code> method for you which will return a new point with the supplied value for <code>x</code> and the same value for <code>y</code>.
16+
</p><p>
17+
Like <a href="/features/GetterSetter"><code>@Setter</code></a>, you can specify an access level in case you want the generated with method to be something other than <code>public</code>:<br /> <code>@With(level = AccessLevel.PROTECTED)</code>. Also like <a href="/features/GetterSetter"><code>@Setter</code></a>, you can also put a <code>@With</code> annotation on a type, which means a <code>with</code> method is generated for each field (even non-final fields).
18+
</p><p>
19+
To put annotations on the generated method, you can use <code>onMethod=@__({@AnnotationsHere})</code>. Be careful though! This is an experimental feature. For more details see the documentation on the <a href="/features/experimental/onX">onX</a> feature.
20+
</p><p>
21+
javadoc on the field will be copied to generated with methods. Normally, all text is copied, and <code>@param</code> is <em>moved</em> to the with method, whilst <code>@return</code> 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 <code>WITH</code>. 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, <code>@return</code> and <code>@param</code> stripping / copying for that section is no longer done (move the <code>@param</code> line into the section).
22+
</p><p>
23+
If you have a hierarchical immutable data structure, the <a href="/features/experimental/WithBy"><code>@WithBy</code></a> feature might be more suitable than <code>@With</code>
24+
</@f.overview>
25+
26+
<@f.snippets name="With" />
27+
28+
<@f.confKeys>
29+
<dt>
30+
<code>lombok.with.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
31+
</dt><dd>
32+
Lombok will flag any usage of <code>@With</code> as a warning or error if configured.
33+
</dd>
34+
</@f.confKeys>
35+
36+
<@f.smallPrint>
37+
<p>
38+
With methods cannot be generated for static fields because that makes no sense.
39+
</p><p>
40+
With methods can be generated for abstract classes, but this generates an abstract method with the appropriate signature.
41+
</p><p>
42+
When applying <code>@With</code> to a type, static fields and fields whose name start with a $ are skipped.
43+
</p><p>
44+
For generating the method names, the first character of the field, if it is a lowercase character, is title-cased, otherwise, it is left unmodified. Then, <code>with</code> is prefixed.
45+
</p><p>
46+
No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, <code>withX(int x)</code> will not be generated if there's already a method <code>withX(String... x)</code> even though it is technically possible to make the method. This caveat exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters.
47+
</p><p>
48+
For <code>boolean</code> fields that start with <code>is</code> immediately followed by a title-case letter, nothing is prefixed to generate the wither name.
49+
</p><p>
50+
Various well known annotations about nullity cause null checks to be inserted and will be copied to the parameter. See <a href="/features/GetterSetter">Getter/Setter</a> documentation's small print for more information.
51+
</p>
52+
</@f.smallPrint>
53+
</@f.scaffold>

website/templates/features/experimental/Wither.html

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import lombok.NonNull;
2+
3+
public class WithExample {
4+
private final int age;
5+
private @NonNull final String name;
6+
7+
public WithExample(String name, int age) {
8+
if (name == null) throw new NullPointerException();
9+
this.name = name;
10+
this.age = age;
11+
}
12+
13+
public WithExample withAge(int age) {
14+
return this.age == age ? this : new WithExample(name, age);
15+
}
16+
17+
protected WithExample withName(@NonNull String name) {
18+
if (name == null) throw new java.lang.NullPointerException("name");
19+
return this.name == name ? this : new WithExample(name, age);
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import lombok.AccessLevel;
2+
import lombok.NonNull;
3+
import lombok.With;
4+
5+
public class WithExample {
6+
@With private final int age;
7+
@With(AccessLevel.PROTECTED) @NonNull private final String name;
8+
9+
public WithExample(String name, int age) {
10+
if (name == null) throw new NullPointerException();
11+
this.name = name;
12+
this.age = age;
13+
}
14+
}

website/usageExamples/experimental/WitherExample_post.jpage

Lines changed: 0 additions & 21 deletions
This file was deleted.

website/usageExamples/experimental/WitherExample_pre.jpage

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)