From da5d9a12e72dc19888b9c1f69b34e73b2b25590c Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 26 Aug 2019 22:46:34 +0200 Subject: [With] updated documentation for Wither/With --- doc/changelog.markdown | 1 + website/extra/htaccess | 1 + website/templates/features/With.html | 53 +++++++++++++++++ .../templates/features/experimental/Wither.html | 66 ---------------------- website/usageExamples/WithExample_post.jpage | 21 +++++++ website/usageExamples/WithExample_pre.jpage | 14 +++++ .../experimental/WitherExample_post.jpage | 21 ------- .../experimental/WitherExample_pre.jpage | 14 ----- 8 files changed, 90 insertions(+), 101 deletions(-) create mode 100644 website/templates/features/With.html delete mode 100644 website/templates/features/experimental/Wither.html create mode 100644 website/usageExamples/WithExample_post.jpage create mode 100644 website/usageExamples/WithExample_pre.jpage delete mode 100644 website/usageExamples/experimental/WitherExample_post.jpage delete mode 100644 website/usageExamples/experimental/WitherExample_pre.jpage diff --git a/doc/changelog.markdown b/doc/changelog.markdown index c684a764..b36c4119 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,6 +2,7 @@ Lombok Changelog ---------------- ### v1.18.9 "Edgy Guinea Pig" +* 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. * 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. * 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) * 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. diff --git a/website/extra/htaccess b/website/extra/htaccess index 48df7772..3539950f 100644 --- a/website/extra/htaccess +++ b/website/extra/htaccess @@ -59,6 +59,7 @@ RewriteRule ^features/${pg?no_esc}(\.html)?/?$ /features/${pg?no_esc} [NC,R=301] RewriteRule ^features/experimental/Builder(\.html)?/?$ /features/Builder [NC,R=301] RewriteRule ^features/experimental/Value(\.html)?/?$ /features/Value [NC,R=301] RewriteRule ^features/experimental/var(\.html)?/?$ /features/var [NC,R=301] +RewriteRule ^features/experimental/Wither(\.html)?/?$ /features/With [NC,R=301] RewriteRule ^features/experimental/all$ /features/experimental/index.html [L,END] RewriteRule ^features/experimental(/all)?/?$ /features/experimental/all [NC,R=301] diff --git a/website/templates/features/With.html b/website/templates/features/With.html new file mode 100644 index 00000000..35c52dab --- /dev/null +++ b/website/templates/features/With.html @@ -0,0 +1,53 @@ +<#import "_features.html" as f> + +<@f.scaffold title="@With" logline="Immutable 'setters' - methods that create a clone but with one changed field."> + <@f.history> +

+ @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. + + + <@f.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. +

+ 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). +

+ If you have a hierarchical immutable data structure, the @WithBy feature might be more suitable than @With + + + <@f.snippets name="With" /> + + <@f.confKeys> +

+ lombok.with.flagUsage = [warning | error] (default: not set) +
+ Lombok will flag any usage of @With as a warning or error if configured. +
+ + + <@f.smallPrint> +

+ With methods cannot be generated for static fields because that makes no sense. +

+ With methods can be generated for abstract classes, but this generates an abstract method with the appropriate signature. +

+ When applying @With to a type, static fields and fields whose name start with a $ are skipped. +

+ 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, with is prefixed. +

+ No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, withX(int x) will not be generated if there's already a method withX(String... x) 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. +

+ For boolean fields that start with is immediately followed by a title-case letter, nothing is prefixed to generate the wither name. +

+ Various well known annotations about nullity cause null checks to be inserted and will be copied to the parameter. See Getter/Setter documentation's small print for more information. +

+ + diff --git a/website/templates/features/experimental/Wither.html b/website/templates/features/experimental/Wither.html deleted file mode 100644 index 00dc10b1..00000000 --- a/website/templates/features/experimental/Wither.html +++ /dev/null @@ -1,66 +0,0 @@ -<#import "../_features.html" as f> - -<@f.scaffold title="@Wither" logline="Immutable 'setters' - methods that create a clone but with one changed field."> - <@f.history> -

- @Wither was introduced as experimental feature in lombok v0.11.4. -

- - - <@f.experimental> - - Current status: neutral - More feedback requires on the items in the above list before promotion to the main package is warranted. - - - <@f.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 @Wither 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. @Wither 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. -

- Like @Setter, you can specify an access level in case you want the generated wither to be something other than public:
@Wither(level = AccessLevel.PROTECTED). Also like @Setter, you can also put a @Wither annotation on a type, which means a 'wither' is generated for each field (even non-final fields). -

- To put annotations on the generated method, you can use onMethod=@__({@AnnotationsHere}); to put annotations on the only parameter of a generated wither method, you can use onParam=@__({@AnnotationsHere}). Be careful though! This is an experimental feature. For more details see the documentation on the onX feature. -

- NEW in lombok v1.12.0: javadoc on the field will now be copied to generated withers. Normally, all text is copied, and @param is moved to the wither, whilst @return lines are stripped from the wither's javadoc. Moved means: Deleted from the field's javadoc. It is also possible to define unique text for the wither's javadoc. To do that, you create a 'section' named WITHER. A section is a line in your javadoc containing 2 or more dashes, then the text 'WITHER', 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). -

- - - <@f.snippets name="experimental/Wither" /> - - <@f.confKeys> -
- lombok.wither.flagUsage = [warning | error] (default: not set) -
- Lombok will flag any usage of @Wither as a warning or error if configured. -
- - - <@f.smallPrint> -

- Withers cannot be generated for static fields because that makes no sense. -

- Withers can be generated for abstract classes, but this generates an abstract method with the appropriate signature. -

- When applying @Wither to a type, static fields and fields whose name start with a $ are skipped. -

- 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, with is prefixed. -

- No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, withX(int x) will not be generated if there's already a method withX(String... x) 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. -

- For boolean fields that start with is immediately followed by a title-case letter, nothing is prefixed to generate the wither name. -

- Various well known annotations about nullity cause null checks to be inserted and will be copied to the parameter. See Getter/Setter documentation's small print for more information. -

- - diff --git a/website/usageExamples/WithExample_post.jpage b/website/usageExamples/WithExample_post.jpage new file mode 100644 index 00000000..a881ed8d --- /dev/null +++ b/website/usageExamples/WithExample_post.jpage @@ -0,0 +1,21 @@ +import lombok.NonNull; + +public class WithExample { + private final int age; + private @NonNull final String name; + + public WithExample(String name, int age) { + if (name == null) throw new NullPointerException(); + this.name = name; + this.age = age; + } + + public WithExample withAge(int age) { + return this.age == age ? this : new WithExample(name, 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); + } +} diff --git a/website/usageExamples/WithExample_pre.jpage b/website/usageExamples/WithExample_pre.jpage new file mode 100644 index 00000000..3c76204e --- /dev/null +++ b/website/usageExamples/WithExample_pre.jpage @@ -0,0 +1,14 @@ +import lombok.AccessLevel; +import lombok.NonNull; +import lombok.With; + +public class WithExample { + @With private final int age; + @With(AccessLevel.PROTECTED) @NonNull private final String name; + + public WithExample(String name, int age) { + if (name == null) throw new NullPointerException(); + this.name = name; + this.age = age; + } +} diff --git a/website/usageExamples/experimental/WitherExample_post.jpage b/website/usageExamples/experimental/WitherExample_post.jpage deleted file mode 100644 index 3447192a..00000000 --- a/website/usageExamples/experimental/WitherExample_post.jpage +++ /dev/null @@ -1,21 +0,0 @@ -import lombok.NonNull; - -public class WitherExample { - private final int age; - private @NonNull final String name; - - public WitherExample(String name, int age) { - if (name == null) throw new NullPointerException(); - this.name = name; - this.age = age; - } - - public WitherExample withAge(int age) { - return this.age == age ? this : new WitherExample(name, age); - } - - protected WitherExample withName(@NonNull String name) { - if (name == null) throw new java.lang.NullPointerException("name"); - return this.name == name ? this : new WitherExample(name, age); - } -} \ No newline at end of file diff --git a/website/usageExamples/experimental/WitherExample_pre.jpage b/website/usageExamples/experimental/WitherExample_pre.jpage deleted file mode 100644 index 5db799fc..00000000 --- a/website/usageExamples/experimental/WitherExample_pre.jpage +++ /dev/null @@ -1,14 +0,0 @@ -import lombok.AccessLevel; -import lombok.NonNull; -import lombok.experimental.Wither; - -public class WitherExample { - @Wither private final int age; - @Wither(AccessLevel.PROTECTED) @NonNull private final String name; - - public WitherExample(String name, int age) { - if (name == null) throw new NullPointerException(); - this.name = name; - this.age = age; - } -} -- cgit