From 2d76b1d22dea1e78326ebafdb48967512183cede Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 23 May 2013 21:12:01 +0200 Subject: First steps Builder support --- src/core/lombok/experimental/Builder.java | 112 ++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/core/lombok/experimental/Builder.java (limited to 'src/core/lombok/experimental') diff --git a/src/core/lombok/experimental/Builder.java b/src/core/lombok/experimental/Builder.java new file mode 100644 index 00000000..b6667462 --- /dev/null +++ b/src/core/lombok/experimental/Builder.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2013 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.experimental; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * The builder annotation creates a so-called 'builder' aspect to the class that is annotated or the class + * that contains a member which is annotated with {@code @Builder}. + *

+ * If a member is annotated, it must be either a constructor or a static method. If a class is annotated, + * then a private constructor is generated with all fields as arguments + * (as if {@code @AllArgsConstructor(AccessLevel.PRIVATE)} is present + * on the class), and it is as if this constructor has been annotated with {@code @Builder} instead. + *

+ * The effect of {@code @Builder} is that an inner class is generated named TBuilder, + * with a private constructor. Instances of TBuilder are made with the static + * method named {@code builder()} which is also generated for you in the class itself (not in the builder class). + *

+ * The TBuilder class contains 1 method for each parameter of the annotated + * constructor / static method (each field, when annotating a class), which returns the builder itself. + * The builder also has a build() method which returns a completed instance of the original type, + * created by passing all parameters as set via the various other methods in the builder to the constructor + * or static method that was annotated with {@code @Builder}. The return type of this method will be the same + * as the relevant class, unless a static method has been annotated, in which case it'll be equal to the + * return type of that method. + *

+ * Complete documentation is found at the project lombok features page for @Builder. + *

+ *

+ * Before: + * + *

+ * @Builder
+ * class Example {
+ * 	private int foo;
+ * 	private final String bar;
+ * }
+ * 
+ * + * After: + * + *
+ * class Example<T> {
+ * 	private T foo;
+ * 	private final String bar;
+ * 	
+ * 	private Example(T foo, String bar) {
+ * 		this.foo = foo;
+ * 		this.bar = bar;
+ * 	}
+ * 	
+ * 	public static <T> ExampleBuilder<T> builder() {
+ * 		return new ExampleBuilder<T>();
+ * 	}
+ * 	
+ * 	public static class ExampleBuilder<T> {
+ * 		private T foo;
+ * 		private String bar;
+ * 		
+ * 		private ExampleBuilder() {}
+ * 		
+ * 		public ExampleBuilder foo(T foo) {
+ * 			this.foo = foo;
+ * 			return this;
+ * 		}
+ * 		
+ * 		public ExampleBuilder bar(String bar) {
+ * 			this.bar = bar;
+ * 			return this;
+ * 		}
+ * 		
+ * 		@java.lang.Override public String toString() {
+ * 			return "ExampleBuilder(foo = " + foo + ", bar = " + bar + ")";
+ * 		}
+ * 		
+ * 		public Example build() {
+ * 			return new Example(foo, bar);
+ * 		}
+ * 	}
+ * }
+ * 
+ */ +@Target({TYPE, METHOD, CONSTRUCTOR}) +@Retention(SOURCE) +public @interface Builder { + /** Name of the static method that creates a new builder instance. Default: {@code builder}. */ + String builderMethodName() default "builder"; +} -- cgit From 648c3eeee69bede925f794b16b1f3d184359761f Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 10 Jun 2013 23:14:23 +0200 Subject: Eclipse Builder implementation finished. Tests need fleshing out though. --- src/core/lombok/experimental/Builder.java | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/core/lombok/experimental') diff --git a/src/core/lombok/experimental/Builder.java b/src/core/lombok/experimental/Builder.java index b6667462..5f2d1ca6 100644 --- a/src/core/lombok/experimental/Builder.java +++ b/src/core/lombok/experimental/Builder.java @@ -109,4 +109,13 @@ import java.lang.annotation.Target; public @interface Builder { /** Name of the static method that creates a new builder instance. Default: {@code builder}. */ String builderMethodName() default "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. + * Default for {@code @Builder} on types and constructors: {@code (TypeName)Builder}. + * Default for {@code @Builder} on static methods: {@code (ReturnTypeName)Builder}. + */ + String builderClassName() default ""; } -- cgit From 54ef2d08523abf09d8be127e59bcf61106479ca6 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 25 Jun 2013 00:27:09 +0200 Subject: ... and now that @Value has been promoted to the main package, the old experimental one is now deprecated. --- src/core/lombok/experimental/Value.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/lombok/experimental') diff --git a/src/core/lombok/experimental/Value.java b/src/core/lombok/experimental/Value.java index 048066df..8c78143c 100644 --- a/src/core/lombok/experimental/Value.java +++ b/src/core/lombok/experimental/Value.java @@ -42,6 +42,7 @@ import java.lang.annotation.Target; */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) +@Deprecated public @interface Value { /** * If you specify a static constructor name, then the generated constructor will be private, and -- cgit From c3e4eb4f745b9f11b07f4a2c48273e7dfdeb7f69 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 25 Jun 2013 00:28:44 +0200 Subject: and added some more javadoc to point at the new main package variant --- src/core/lombok/experimental/Value.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/lombok/experimental') diff --git a/src/core/lombok/experimental/Value.java b/src/core/lombok/experimental/Value.java index 8c78143c..b7700bb5 100644 --- a/src/core/lombok/experimental/Value.java +++ b/src/core/lombok/experimental/Value.java @@ -39,6 +39,7 @@ import java.lang.annotation.Target; * @see lombok.ToString * @see lombok.EqualsAndHashCode * @see lombok.Data + * @deprecated {@link lombok.Value} has been promoted to the main package, so use that one instead. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) -- cgit From 7af9add9996f2efab6cccc50c5503b3457534930 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 16 Jul 2013 00:51:31 +0200 Subject: * Fixed issues with @FieldDefaults and @Value (you can NOT override @Value's final-by-default and private-by-default with it; now appropriate warnings are emitted) * Builder now errors out on presence of most lombok annotations on an explicit builder class. * Builder now takes @FieldDefaults/@Value into account. * Builder on type now generates the constructor as package private instead of private to avoid synthetic accessor constructors. * added a bunch of test cases. * added a test case feature: If the expected file is omitted entirely but there are expected messages, the differences in the output itself are ignored. * streamlined checking for boolean-ness (removed some duplicate code) * added 'fluent' and 'chain' to @Builder. --- src/core/lombok/experimental/Builder.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/core/lombok/experimental') diff --git a/src/core/lombok/experimental/Builder.java b/src/core/lombok/experimental/Builder.java index 5f2d1ca6..1300e7d3 100644 --- a/src/core/lombok/experimental/Builder.java +++ b/src/core/lombok/experimental/Builder.java @@ -118,4 +118,20 @@ public @interface Builder { * Default for {@code @Builder} on static methods: {@code (ReturnTypeName)Builder}. */ String builderClassName() default ""; + + /** + * Normally the builder's 'set' methods are fluent, meaning, they have the same name as the field. Set this + * to {@code false} to name the setter method for field {@code someField}: {@code setSomeField}. + *

+ * Default: true + */ + boolean fluent() default true; + + /** + * Normally the builder's 'set' methods are chaining, meaning, they return the builder so that you can chain + * calls to set methods. Set this to {@code false} to have these 'set' methods return {@code void} instead. + *

+ * Default: true + */ + boolean chain() default true; } -- cgit