Person.builder().setName("Jane").build() instead of Person.builder().name("Jane").build() and what it should be.Įxample usage where all options are changed from their = "HelloWorldBuilder", buildMethodName = "execute", builderMethodName = "helloWorld", toBuilder = true, access = AccessLevel.PRIVATE, setterPrefix = "set") (discouraged) If you want your builder's 'set' methods to have a prefix, i.e. The access level of all generated elements (default: public). The builder() method's name (default: "builder") The build() method's name (default: "build") The builder's class name (default: return type + 'Builder') If is applied to a method that returns void, the builder will be named VoidBuilder. For example, if is applied to a class named, then the builder name will be FancyListBuilder. 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 on constructors and types, and the name of the return type for on methods. For example, you can specify a method to be invoked: = "calculateFoo"). You can put the annotation on the parameters (in case of a constructor or method) or fields (in case of on a type) to indicate alternative means by which the value for that field/parameter is obtained from this instance. If using to generate builders to produce instances of your own class (this is always the case unless adding to a method that doesn't return your own type), you can use = 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. Note that if you put both and on a class, the package-private constructor that wants to generate 'wins' and suppresses the constructor that wants to make. If you do have an explicit constructor, put the annotation on the constructor instead of on the class. This only works if you haven't written any explicit constructors yourself. Now that the "method" mode is clear, putting a 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.įinally, applying to a class is as if you added = AccessLevel.PACKAGE) to the class and applied the annotation to this all-args-constructor. To get this behavior, the field/parameter needs to be annotated with The feature has its own documentation. Would result in the List jobs field to have 2 strings in it. These take 1 element instead of an entire list, and add the element to the list. You may not put any other method (or constructor) generating lombok annotation on a builder class though for example, you can not put on the builder can generate so-called 'singular' methods for collection parameters/fields. This includes the builder itself: If that class already exists, lombok will simply start injecting fields and methods inside this already existing class, unless of course the fields / methods to be injected already exist. In the class containing the target: A builder() method, which creates a new instance of the builder.Įach listed generated element will be silently skipped if that element already exists (disregarding parameter counts and looking only at names). In the builder: A sensible toString() implementation. 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 builder itself, so that the setter calls can be chained, as in the above example. In the builder: A 'setter'-like method for each parameter of the target: It has the same type as that parameter and the same name. In the builder: A package private no-args empty constructor. In the builder: One private non-static non-final field for each parameter of the target. While the "on a class" and "on a constructor" mode are the most common use-case, is most easily explained with the "method" use-case.Ī method annotated with (from now on called the target) causes the following 7 things to be generated:Īn inner static class named FooBuilder, with the same type arguments as the static method (called the builder).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |