From 389034c937d9358baf08e0877a2d0c119b67da59 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
* Default for {@code @Builder} on static methods: {@code (ReturnTypeName)Builder}.
*/
String builderClassName() default "";
+
+ /**
+ * If true, generate an instance method to obtain a builder that is initialized with the values of this instance.
+ * Legal only if {@code @Builder} is used on a constructor, on the type itself, or on a static method that returns itself.
+ */
+ boolean toBuilder() default false;
+
+ /**
+ * Put on a field (in case of {@code @Builder} on a type) or a parameter (for {@code @Builder} on a constructor or static method) to
+ * indicate how lombok should obtain a value for this given an instance; this is only relevant if {@code toBuilder} is true.
+ *
+ * Note that one of {@code field} or {@code method} should be set, or an error is generated.
+ *
+ * The default behaviour is to obtain a value by referencing the name of the parameter as a field on 'this'.
+ */
+ public @interface ObtainVia {
+ /** Tells lombok to obtain a value with the expression {@code this.value}. */
+ String field() default "";
+
+ /** Tells lombok to obtain a value with the expression {@code this.method()}. */
+ String method() default "";
+
+ /** Tells lombok to obtain a value with the expression {@code SelfType.method(this)}; requires {@code method} to be set. */
+ boolean isStatic() default false;
+ }
}
diff --git a/src/core/lombok/core/TypeLibrary.java b/src/core/lombok/core/TypeLibrary.java
index dc557c47..d4f5f2b0 100644
--- a/src/core/lombok/core/TypeLibrary.java
+++ b/src/core/lombok/core/TypeLibrary.java
@@ -50,13 +50,20 @@ public class TypeLibrary {
}
private TypeLibrary(String fqnSingleton) {
- unqualifiedToQualifiedMap = null;
- qualified = fqnSingleton;
- int idx = fqnSingleton.lastIndexOf('.');
- if (idx == -1) {
- unqualified = fqnSingleton;
+ if (fqnSingleton.indexOf("$") != -1) {
+ unqualifiedToQualifiedMap = new HashMap
+ If using
The name of the builder class is build() method which calls the static method, passing in each field. It returns the same type that the
target returns.toString() implementation.builder() method, which creates a new instance of the builder.builder() method, which creates a new instance of the builder.@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.
@Builder to generate builders to produce instances of your own class (this is always the case unless adding @Builder to a static 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 static 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 the instance. For example, you can specify a method to be invoked: @Builder.ObtainVia(method = "calculateFoo").
+ 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
@@ -69,9 +71,10 @@
"build")"builder")toBuilder() (default: no)
- @Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld")
+ @Builder(builderClassName = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld", toBuilder = true)
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 an implementation detail of the current implementation of the java.util recipes for @Singular @Builder.
+
+ With toBuilder = true applied to static methods, any type parameter on the annotated static method must show up in the returntype.
* Note that one of {@code field} or {@code method} should be set, or an error is generated. *
* The default behaviour is to obtain a value by referencing the name of the parameter as a field on 'this'.