diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 5f145f83..14baeb49 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -3,9 +3,10 @@ Lombok Changelog ### v1.16.5 "Edgy Guinea Pig" * FEATURE: `@Helper` can be placed on method-local inner classes to make all methods in the class accessible to the rest of the method. [Full documentation](https://projectlombok.org/features/experimental/Helper.html). +* FEATURE: `@Builder(toBuilder = true)` is now available. It produces an instance method that creates a new builder, initialized with all the values of that instance. For more, read the [Feature page on Builder](https://projectlombok.org/features/Builder.html). +* FEATURE: the `hashCode()` method generated by lombok via `@EqualsAndHashCode`, `@Data`, and `@Value` is now smarter about nulls; they are treated as if they hash to a magic prime instead of 0, which reduces hash collisions. * BUGFIX: Parameterized static methods with `@Builder` would produce compiler errors in javac. [Issue #828](https://github.com/rzwitserloot/lombok/issues/828). * PERFORMANCE: the config system caused significant slowdowns in eclipse if the filesystem is very slow (network file system) or has a slow authentication system. -* FEATURE: the `hashCode()` method generated by lombok via `@EqualsAndHashCode`, `@Data`, and `@Value` is now smarter about nulls; they are treated as if they hash to a magic prime instead of 0, which reduces hash collisions. * BUGFIX: Various quickfixes in Eclipse Mars were broken. [Issue #861](https://github.com/rzwitserloot/lombok/issues/861) [Issue #866](https://github.com/rzwitserloot/lombok/issues/866) [Issue #870](https://github.com/rzwitserloot/lombok/issues/870). ### v1.16.4 (April 14th, 2015) diff --git a/src/core/lombok/Builder.java b/src/core/lombok/Builder.java index 4556bef0..0639c4cb 100644 --- a/src/core/lombok/Builder.java +++ b/src/core/lombok/Builder.java @@ -113,9 +113,40 @@ public @interface Builder { /** Name of the instance method in the builder class that creates an instance of your {@code @Builder}-annotated class. */ String buildMethodName() default "build"; - /** Name of the builder class. + /** + * Name of the builder class. + * * Default for {@code @Builder} on types and constructors: {@code (TypeName)Builder}. + *
* 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 + * an instance of the declaring type. + */ + 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 field or parameter given an instance; this is only relevant if {@code toBuilder} is {@code true}. + * + * You do not need to supply an {@code @ObtainVia} annotation unless you wish to change the default behaviour: Use a field with the same name. + *
+ * 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..cdaf7a70 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 HashMapbuild() 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.
+ If using @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").
+
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
@@ -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.