From a24bf3194477a841c905827ef625e19b0fd53b2a Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
Any class definition may be annotated with
- By setting
- Setting
NEW in Lombok 0.10: Unless your class is
@@ -22,6 +22,10 @@
@EqualsAndHashCode
to let lombok generate implementations of the equals(Object other)
and hashCode()
methods. By default, it'll use all non-static, non-transient fields, but you can exclude more fields by naming them in the optional exclude
parameter to the annotation. Alternatively, you can specify exactly which fields you wish to be used by naming them in the of
parameter.
callSuper
to true, you can include the equals
and hashCode
methods of your superclass in the generated methods. For hashCode
, the result of super.hashCode()
is included in the hash algorithm, and for equals
, the generated method will return false if the super implementation thinks it is not equal to the passed in object. Be aware that not all equals
implementations handle this situation properly. However, lombok-generated equals
implementations do handle this situation properly, so you can safely call your superclass equals if it, too, has a lombok-generated equals
method.
+ If applying @EqualsAndHashCode
to a class that extends another, this feature gets a bit trickier. Normally, auto-generating an equals
and hashCode
method for such classes is a bad idea, as the superclass also defines fields, which also need equals/hashCode code but this code will not be generated. By setting callSuper
to true, you can include the equals
and hashCode
methods of your superclass in the generated methods. For hashCode
, the result of super.hashCode()
is included in the hash algorithm, and forequals
, the generated method will return false if the super implementation thinks it is not equal to the passed in object. Be aware that not all equals
implementations handle this situation properly. However, lombok-generated equals
implementations do handle this situation properly, so you can safely call your superclass equals if it, too, has a lombok-generated equals
method. If you have an explicit superclass you are forced to supply some value for callSuper
to acknowledge that you've considered it; failure to do so results in a warning.
callSuper
to true when you don't extend anything (you extend java.lang.Object
) is a compile-time error, because it would turn the generated equals()
and hashCode()
implementations into having the same behaviour as simply inheriting these methods from java.lang.Object
: only the same object will be equal to each other and will have the same hashCode. Not setting callSuper
to true when you extend another class generates a warning, because unless the superclass has no (equality-important) fields, lombok cannot generate an implementation for you that takes into account the fields declared by your superclasses. You'll need to write your own implementations, or rely on the callSuper
chaining facility.
+ Setting callSuper
to true when you don't extend anything (you extend java.lang.Object
) is a compile-time error, because it would turn the generated equals()
and hashCode()
implementations into having the same behaviour as simply inheriting these methods from java.lang.Object
: only the same object will be equal to each other and will have the same hashCode. Not setting callSuper
to true when you extend another class generates a warning, because unless the superclass has no (equality-important) fields, lombok cannot generate an implementation for you that takes into account the fields declared by your superclasses. You'll need to write your own implementations, or rely on the callSuper
chaining facility. You can also use the lombok.equalsAndHashCode.callSuper
config key.
final
and extends java.lang.Object
, lombok generates a canEqual
method which means JPA proxies can still be equal to their base class, but subclasses that add new state don't break the equals contract. The complicated reasons for why such a method is necessary are explained in this paper: How to Write an Equality Method in Java. If all classes in a hierarchy are a mix of scala case classes and classes with lombok-generated equals methods, all equality will 'just work'. If you need to write your own equals methods, you should always override canEqual
if you change equals
and hashCode
.
lombok.equalsAndHashCode.doNotUseGetters
= [true
| false
] (default: false)
true
, lombok will access fields directly instead of using getters (if available) when generating equals
and hashCode
methods. The annotation parameter 'doNotUseGetters
', if explicitly specified, takes precedence over this setting.
+ lombok.equalsAndHashCode.callSuper
= [call
| skip
| warn
] (default: warn)
+ call
, lombok will generate calls to the superclass implementation of hashCode
and equals
if your class extends something. If set to skip
no such calls are generated. The default behaviour is like skip
, with an additional warning.
lombok.equalsAndHashCode.flagUsage
= [warning
| error
] (default: not set)
@Data
and other method-generating lombok annotations will trigger on any annotation named @NonNull
regardless of casing or package name, this feature only triggers on lombok's own @NonNull
annotation from the lombok
package.
A @NonNull
on a primitive parameter results in a warning. No null-check will be generated.
+
+ A @NonNull
on a parameter of an abstract method used to generate a warning; starting with version 1.16.8, this is no longer the case, to acknowledge the notion that @NonNull
also has a documentary role. For the same reason, you can annotate a method as @NonNull
; this is allowed, generates no warning, and does not generate any code.
@Builder
was introduced as experimental feature in lombok v0.12.0.
@Builder
gained @Singular
support and was promoted to the main lombok
package since lombok v1.16.0.
+
+ @Builder
with @Singular
adds a clear method since lombok v1.16.8.
@Builder
lets you automatically produce the code required to have your class be instantiable with code such as:Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
- @Builder
can be placed on a class, or on a constructor, or on a static method. While the "on a class" and "on a constructor" mode are the most common use-case, @Builder
is most easily explained with the "static method" use-case.
+ @Builder
can be placed on a class, or on a constructor, or on a method. While the "on a class" and "on a constructor" mode are the most common use-case, @Builder
is most easily explained with the "method" use-case.
- A static method annotated with @Builder
(from now on called the target) causes the following 7 things to be generated:
+ A method annotated with @Builder
(from now on called the target) causes the following 7 things to be generated:
FooBuilder
, with the same type arguments as the static method (called the builder).
@@ -29,7 +31,7 @@
build()
method which calls the static method, passing in each field. It returns the same type that the target returns.
+ In the builder: A build()
method which calls the method, passing in each field. It returns the same type that the target returns.
toString()
implementation.
@Builder
can generate so-called 'singular' methods for collection parameters/fields. These take 1 element instead of an entire list, and add the element to the list. For example: Person.builder().job("Mythbusters").job("Unchained Reaction").build();
would result in the List<String> jobs
field to have 2 strings in it. To get this behaviour, the field/parameter needs to be annotated with @Singular
. The feature has its own documentation.
- Now that the "static method" mode is clear, putting a @Builder
annotation on a constructor functions similarly; effectively, constructors are just static methods that have a special syntax to invoke them: Their 'return type' is the class they construct, and their type parameters are the same as the type parameters of the class itself.
+ Now that the "method" mode is clear, putting a @Builder
annotation on a constructor functions similarly; effectively, constructors are just static methods that have a special syntax to invoke them: Their 'return type' is the class they construct, and their type parameters are the same as the type parameters of the class itself.
Finally, applying @Builder
to a class is as if you added @AllArgsConstructor(access = AccessLevel.PACKAGE)
to the class and applied the @Builder
annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself. If you do have an explicit constructor, put the @Builder
annotation on the constructor instead of on the class.
- The name of the builder class is FoobarBuilder
, where Foobar is the simplified, title-cased form of the return type of the target - that is, the name of your type for @Builder
on constructors and types, and the name of the return type for @Builder
on static methods. For example, if @Builder
is applied to a class named com.yoyodyne.FancyList<T>
, then the builder name will be FancyListBuilder<T>
. If @Builder
is applied to a static method that returns void
, the builder will be named VoidBuilder
.
+ If using @Builder
to generate builders to produce instances of your own class (this is always the case unless adding @Builder
to a method that doesn't return your own type), you can use @Builder(toBuilder = true)
to also generate an instance method in your class called toBuilder()
; it creates a new builder that starts out with all the values of this instance. You can put the @Builder.ObtainVia
annotation on the parameters (in case of a constructor or method) or fields (in case of @Builder
on a type) to indicate alternative means by which the value for that field/parameter is obtained from this instance. For example, you can specify a method to be invoked: @Builder.ObtainVia(method = "calculateFoo")
.
+
+ The name of the builder class is FoobarBuilder
, where Foobar is the simplified, title-cased form of the return type of the target - that is, the name of your type for @Builder
on constructors and types, and the name of the return type for @Builder
on methods. For example, if @Builder
is applied to a class named com.yoyodyne.FancyList<T>
, then the builder name will be FancyListBuilder<T>
. If @Builder
is applied to a method that returns void
, the builder will be named VoidBuilder
.
The configurable aspects of builder are:
"build"
)
"builder"
)
+ toBuilder()
(default: no)
@Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld")
@Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld", toBuilder = true)
- By annotating one of the parameters (if annotating a static method or constructor with @Builder
) or fields (if annotating a class with @Builder
) with the @Singular
annotation, lombok will treat that builder node as a collection, and it generates 2 'adder' methods instead of a 'setter' method. One which adds a single element to the collection, and one which adds all elements of another collection to the collection. No setter to just set the collection (replacing whatever was already added) will be generated. These 'singular' builders are very complicated in order to guarantee the following properties:
+ By annotating one of the parameters (if annotating a method or constructor with @Builder
) or fields (if annotating a class with @Builder
) with the @Singular
annotation, lombok will treat that builder node as a collection, and it generates 2 'adder' methods instead of a 'setter' method. One which adds a single element to the collection, and one which adds all elements of another collection to the collection. No setter to just set the collection (replacing whatever was already added) will be generated. A 'clear' method is also generated. These 'singular' builders are very complicated in order to guarantee the following properties:
build()
, the produced collection will be immutable.
build()
does not modify any already generated objects, and, if build()
is later called again, another collection with all the elements added since the creation of the builder is generated.
+ Calling one of the 'adder' methods, or the 'clear' method, after invoking build()
does not modify any already generated objects, and, if build()
is later called again, another collection with all the elements added since the creation of the builder is generated.
@Singular
can only be applied to collection types known to lombok. Currently, the supported types are:
java.util
:
+ java.util
:
Iterable
, Collection
, and List
(backed by a compacted unmodifiable ArrayList
in the general case).
@@ -98,6 +104,8 @@
ImmutableSet
and ImmutableSortedSet
(backed by the builder feature of those types).
ImmutableMap
, ImmutableBiMap
, and ImmutableSortedMap
(backed by the builder feature of those types).
+ ImmutableTable
(backed by the builder feature of ImmutableTable
).
@Singular
on it is an english plural and will attempt to automatically singularize that name. If this is possible, the add-one method will use this name. For example, if your collection is called statuses
, then the add-one method will automatically be called status
. You can also specify the singular form of your identifier explictly by passing the singular form as argument to the annotation like so: @Singular("axis") List<Line> axes;
.
- The snippet below does not show what lombok generates for a @Singular
field/parameter because it is rather complicated. You can view a snippet here.
+ The snippet below does not show what lombok generates for a @Singular
field/parameter because it is rather complicated. You can view a snippet here.
SortedSet
, NavigableSet
, SortedMap
, NavigableMap
and guava: ImmutableSortedSet
, ImmutableSortedMap
) require that the type argument of the collection has natural order (implements java.util.Comparable
). There is no way to pass an explicit Comparator
to use in the builder.
An ArrayList
is used to store added elements as call methods of a @Singular
marked field, if the target collection is from the java.util
package, even if the collection is a set or map. Because lombok ensures that generated collections are compacted, a new backing instance of a set or map must be constructed anyway, and storing the data as an ArrayList
during the build process is more efficient that storing it as a map or set. This behaviour is not externally visible, an implementation detail of the current implementation of the java.util
recipes for @Singular @Builder
.
+
+ With toBuilder = true
applied to methods, any type parameter of the annotated method itself must also show up in the return type.
+ These config keys can make lombok affect source files even if they have 0 lombok annotations in them.
+
lombok.fieldDefaults.defaultPrivate = true
lombok.fieldDefaults.defaultFinal = true
+ @FieldDefaults
documentation for more.
+
+ @f.featureSection>
@f.scaffold>
diff --git a/website2/templates/features/constructor.html b/website2/templates/features/constructor.html
index 241926c0..fb331113 100644
--- a/website2/templates/features/constructor.html
+++ b/website2/templates/features/constructor.html
@@ -7,7 +7,7 @@
This set of 3 annotations generate a constructor that will accept 1 parameter for certain fields, and simply assigns this parameter to the field.
- @NoArgsConstructor
will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead. For fields with constraints, such as @NonNull
fields, no check or assignment is generated, so be aware that these constraints may then not be fulfilled until those fields are properly initialized later. Certain java constructs, such as hibernate and the Service Provider Interface require a no-args constructor. This annotation is useful primarily in combination with either @Data
or one of the other constructor generating annotations.
+ @NoArgsConstructor
will generate a constructor with no parameters. If this is not possible (because of final fields), a compiler error will result instead, unless @NoArgsConstructor(force = true
is used, then all final fields are initialized with 0
/ false
/ null
. For fields with constraints, such as @NonNull
fields, no check is generated,so be aware that these constraints will generally not be fulfilled until those fields are properly initialized later. Certain java constructs, such as hibernate and the Service Provider Interface require a no-args constructor. This annotation is useful primarily in combination with either @Data
or one of the other constructor generating annotations.
@RequiredArgsConstructor
generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final
fields get a parameter, as well as any fields that are marked as @NonNull
that aren't initialized where they are declared. For those fields marked with @NonNull
, an explicit null check is also generated. The constructor will throw a NullPointerException
if any of the parameters intended for the fields marked with @NonNull
contain null
. The order of the parameters match the order in which the fields appear in your class.
@@ -17,7 +17,7 @@
To put annotations on the generated constructor, you can use onConstructor=@__({@AnnotationsHere})
, but be careful; this is an experimental feature. For more details see the documentation on the onX feature.
- Static fields are skipped by these annotations. Also, a @java.beans.ConstructorProperties
annotation is added for all constructors with at least 1 argument, which allows bean editor tools to call the generated constructors. @ConstructorProperties
is now in Java 1.6, which means that if your code is intended for compilation on Java 1.5, a compiler error will occur. Running on a JVM 1.5 should be no problem (the annotation will be ignored). To suppress the generation of the @ConstructorProperties
annotation, add a parameter to your annotation: @AllArgsConstructor(suppressConstructorProperties=true)
. However, as java 1.5, which has already been end-of-lifed, fades into obscurity, this parameter will eventually be removed. It has also been marked deprecated for this reason.
+ Static fields are skipped by these annotations. Also, a @java.beans.ConstructorProperties
annotation is added for all constructors with at least 1 argument, which allows bean editor tools to call the generated constructors. @ConstructorProperties
is new in Java 1.6, which means that if your code is intended for compilation on Java 1.5, a compiler error will occur. Running on a JVM 1.5 should be no problem (the annotation will be ignored). To suppress the generation of the @ConstructorProperties
annotation, add a parameter to your annotation: @AllArgsConstructor(suppressConstructorProperties=true)
. However, as java 1.5, which has already been end-of-lifed, fades into obscurity, this parameter will eventually be removed. It has also been marked deprecated for this reason.
Unlike most other lombok annotations, the existence of an explicit constructor does not stop these annotations from generating their own constructor. This means you can write your own specialized constructor, and let lombok generate the boilerplate ones as well. If a conflict arises (one of your constructors ends up with the same signature as one that lombok generates), a compiler error will occur.
diff --git a/website2/templates/features/data.html b/website2/templates/features/data.html index d018ae72..59370cc8 100644 --- a/website2/templates/features/data.html +++ b/website2/templates/features/data.html @@ -7,7 +7,7 @@
@Data
is a convenient shortcut annotation that bundles the features of @ToString
, @EqualsAndHashCode
, @Getter
/ @Setter
and @RequiredArgsConstructor
together: In other words, @Data
generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, setters for all non-final fields, and appropriate toString
, equals
and hashCode
implementations that involve the fields of the class, and a constructor that initializes all final fields, as well as all non-final fields with no initializer that have been marked with @NonNull
, in order to ensure the field is never null.
- @Data
is like having implicit @Getter
, @Setter
, @ToString
, @EqualsAndHashCode
and @RequiredArgsConstructor
annotations on the class. However, the parameters of these annotations (such as callSuper
, includeFieldNames
and exclude
) cannot be set with @Data
. If you need to set non-default values for any of these parameters, just add those annotations explicitly; @Data
is smart enough to defer to those annotations.
+ @Data
is like having implicit @Getter
, @Setter
, @ToString
, @EqualsAndHashCode
and @RequiredArgsConstructor
annotations on the class (except that no constructor will be generated if any explicitly written constructors already exist). However, the parameters of these annotations (such as callSuper
, includeFieldNames
and exclude
) cannot be set with @Data
. If you need to set non-default values for any of these parameters, just add those annotations explicitly; @Data
is smart enough to defer to those annotations.
All generated getters and setters will be public
. To override the access level, annotate the field or class with an explicit @Setter
and/or @Getter
annotation. You can also use this annotation (by combining it with AccessLevel.NONE
) to suppress generating a getter and/or setter altogether.
diff --git a/website2/templates/features/experimental/FieldDefaults.html b/website2/templates/features/experimental/FieldDefaults.html index 77ed37b6..0d4cda9e 100644 --- a/website2/templates/features/experimental/FieldDefaults.html +++ b/website2/templates/features/experimental/FieldDefaults.html @@ -24,9 +24,9 @@
The @FieldDefaults
annotation can add an access modifier (public
, private
, or protected
) to each field in the annotated class or enum. It can also add final
to each field in the annotated class or enum.
- To add final
to each field, use @FieldDefaults(makeFinal=true)
. Any non-final field which must remain nonfinal can be annotated with @NonFinal
(also in the lombok.experimental
package).
+ To add final
to each (instance) field, use @FieldDefaults(makeFinal=true)
. Any non-final field which must remain nonfinal can be annotated with @NonFinal
(also in the lombok.experimental
package).
- To add an access modifier to each field, use @FieldDefaults(level=AccessLevel.PRIVATE)
. Any field that does not already have an access modifier (i.e. any field that looks like package private access) is changed to have the appropriate access modifier. Any package private field which must remain package private can be annotated with @PackagePrivate
(also in the lombok.experimental
package).
+ To add an access modifier to each (instance) field, use @FieldDefaults(level=AccessLevel.PRIVATE)
. Any field that does not already have an access modifier (i.e. any field that looks like package private access) is changed to have the appropriate access modifier. Any package private field which must remain package private can be annotated with @PackagePrivate
(also in the lombok.experimental
package).
lombok.fieldDefaults.flagUsage
= [warning
| error
] (default: not set)
@FieldDefaults
as a warning or error if configured.
+ lombok.fieldDefautls.defaultPrivate
= [true
| false
] (default: false)
+ true
, every field in every class or enum anywhere in the sources being compiled will be marked as private
unless it has an explicit access modifier or the @PackagePrivate
annotation, or an explicit @FieldDefaults
annotation is present to override this config key.
+ lombok.fieldDefaults.defaultFinal
= [true
| false
] (default: false)
+ true
, every field in every class or enum anywhere in the sources being compiled will be marked as final
unless it has the @NonFinal
annotation, or an explicit @FieldDefaults
annotation is present to override this config key.
@Value
has proven its value and has been moved to the main package.
@main.feature>
- <@main.feature title="@Builder: promoted" code="/features/Builder">
+ <@main.feature title="@Builder: promoted" href="../Builder">
@Builder
is a solid base to build APIs on, and has been moved to the main package.
@main.feature>
+ var
was introduced in lombok 1.16.12 as experimental feature.
+
var
obsolete.
+
+ var
works exactly like val
, except the local variable is not marked as final
.
+
+ The type is still entirely derived from the mandatory initializer expression, and any further assignments, while now legal (because the variable is no longer final
), aren't looked at to determine the appropriate type.
+ For example, var x = "Hello"; x = Color.RED;
does not work; the type of x will be inferred to be java.lang.String
and thus, the x = Color.RED
assignment will fail. If the type of x
was inferred to be java.lang.Object
this code would have compiled, but that's not howvar
works.
+
lombok.var.flagUsage
= [warning
| error
] (default: not set)
+ var
as a warning or error if configured.
+ close()
methods safely with no hassle.
@main.feature>
- <@main.feature title="@Getter/@Setter" code="getter-setter">
+ <@main.feature title="@Getter/@Setter" href="GetterSetter">
Never write public int getFoo() {return foo;}
again.
@main.feature>
- <@main.feature title="@ToString" code="to-string">
+ <@main.feature title="@ToString" href="ToString">
No need to start a debugger to see your fields: Just let lombok generate a toString
for
you!
@main.feature>
- <@main.feature title="@EqualsAndHashCode" code="equals-and-hashcode">
+ <@main.feature title="@EqualsAndHashCode" href="EqualsAndHashCode">
Equality made easy: Generates hashCode
and equals
implementations from the
fields of your object..
@main.feature>
- <@main.feature title="@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor" code="constructor">
+ <@main.feature title="@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor" href="constructor">
Constructors made to order: Generates constructors that take no arguments, one argument per final /
non-nullfield, or one argument for every field.
@main.feature>
- <@main.feature title="@Data" code="data">
+ <@main.feature title="@Data" href="Data">
All together now: A shortcut for @ToString
, @EqualsAndHashCode
,
@Getter
on all fields, and @Setter
on all non-final fields, and
@RequiredArgsConstructor
!
@main.feature>
- <@main.feature title="@Value" code="value">
+ <@main.feature title="@Value" href="Value">
Immutable classes made very easy.
@main.feature>
- <@main.feature title="@Builder" code="builder">
+ <@main.feature title="@Builder" href="Builder">
... and Bob's your uncle: No-hassle fancy-pants APIs for object creation!
@main.feature>
- <@main.feature title="@SneakyThrows" code="sneaky-throws">
+ <@main.feature title="@SneakyThrows" href="SneakyThrows">
To boldly throw checked exceptions where no one has thrown them before!
@main.feature>
- <@main.feature title="@Synchronized" code="sync">
+ <@main.feature title="@Synchronized" href="Synchronized">
synchronized
done right: Don't expose your locks.
@main.feature>
- <@main.feature title="@Getter(lazy=true)" code="getter-lazy">
+ <@main.feature title="@Getter(lazy=true)" href="GetterLazy">
Laziness is a virtue!
@main.feature>
- <@main.feature title="@Log" code="log">
+ <@main.feature title="@Log" href="log">
Captain's Log, stardate 24435.7: "What was that line again?"
@main.feature>
@Getter
back into the actual getter. It then removes the annotation. This is useful for all sorts of reasons; you can check out what's happening under the hood, if the unthinkable happens and you want to stop using lombok, you can easily remove all traces of it in your source, and you can use delombok to preprocess your source files for source-level tools such as javadoc and GWT. More information about how to run delombok, including instructions for build tools can be found at the delombok page.
+ Delombok copies your source files to another directory, replacing all lombok annotations with their desugared form. So, it'll turn @Getter
back into the actual getter. It then removes the annotation. This is useful for all sorts of reasons; you can check out what's happening under the hood, if the unthinkable happens and you want to stop using lombok, you can easily remove all traces of it in your source, and you can use delombok to preprocess your source files for source-level tools such as javadoc and GWT. More information about how to run delombok, including instructions for build tools can be found at the delombok page.
You put the variant of @Log
on your class (whichever one applies to the logging system you use); you then have a static final log
field, initialized to the name of your class, which you can then use to write log statements.
- There are six choices available:
+ There are several choices available:
@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
+ @JBossLog
+ private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
lombok.log.javaUtilLogging.flagUsage
= [warning
| error
] (default: not set)
@lombok.extern.java.Log
as a warning or error if configured.
+ lombok.log.jbosslog.flagUsage
= [warning
| error
] (default: not set)
+ @lombok.extern.jbosslog.JBossLog
as a warning or error if configured.
lombok.log.log4j.flagUsage
= [warning
| error
] (default: not set)
+ val
was introduced in lombok 0.10.
+
- NEW in Lombok 0.10: You can use val
as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields. The initializer expression is required.
+ You can use val
as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields. The initializer expression is required.
val
is actually a 'type' of sorts, and exists as a real class in the lombok
package. You must import it for val to work (or use lombok.val
as the type). The existence of this type on a local variable declaration triggers both the adding of the final
keyword as well as copying the type of the initializing expression which overwrites the 'fake' val
type.
diff --git a/website2/templates/features/value.html b/website2/templates/features/value.html index d9ea1ca7..fdad0e12 100644 --- a/website2/templates/features/value.html +++ b/website2/templates/features/value.html @@ -17,8 +17,8 @@
In practice, @Value
is shorthand for: final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter
, except that explicitly including an implementation of any of the relevant methods simply means that part won't be generated and no warning will be emitted. For example, if you write your own toString
, no error occurs, and lombok will not generate a toString
. Also, any explicit constructor, no matter the arguments list, implies lombok will not generate a constructor. If you do want lombok to generate the all-args constructor, add @AllArgsConstructor
to the class. You can mark any constructor or method with @lombok.experimental.Tolerate
to hide them from lombok.
- It is possible to override the final-by-default and private-by-default behaviour using either an explicit access level on a field, or by using the @NonFinal
or @PackagePrivate
annotations.
- It is possible to override any default behaviour for any of the 'parts' that make up @Value
by explicitly using that annotation.
+ It is possible to override the final-by-default and private-by-default behavior using either an explicit access level on a field, or by using the @NonFinal
or @PackagePrivate
annotations.
+ It is possible to override any default behavior for any of the 'parts' that make up @Value
by explicitly using that annotation.