-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use annotation default values with
@AutoBuilder
and @AutoAnnotation
.
You can use `@AutoBuilder` to call an `@AutoAnnotation` method. As usual for `@AutoAnnotation`, each parameter of that method corresponds to an element of the annotation. If that element has a default value, the parameter should default to that same value. Trivial example: ```java class Example { @interface MyAnnotation { String value() default "foo"; } @AutoAnnotation static MyAnnotation myAnnotation(String value) { return new AutoAnnotation_Example_myAnnotation(value); } @autobuilder(callMethod = "myAnnotation") interface MyAnnotationBuilder { MyAnnotationBuilder value(String value); MyAnnotation build(); } static MyAnnotationBuilder myAnnotationBuilder() { return new AutoBuilder_Example_MyAnnotationBuilder(); } } ``` In the example, the call `myAnnotationBuilder().build()` has the same effect as `myAnnotationBuilder().value("foo").build()` because of `value() default "foo"`. RELNOTES=AutoBuilder now uses annotation defaults when building a call to an `@AutoAnnotation` method. PiperOrigin-RevId: 395114215
- Loading branch information
1 parent
02ff0f1
commit fb96c83
Showing
8 changed files
with
171 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,39 @@ | |
* parameter corresponding to an array-valued annotation member, and the implementation of each such | ||
* member will also return a clone of the array. | ||
* | ||
* <p>If your annotation has many elements, you may consider using {@code @AutoBuilder} to make it | ||
* easier to construct instances. In that case, {@code default} values from the annotation will | ||
* become default values for the parameters of the {@code @AutoAnnotation} method. For example: | ||
* | ||
* <pre> | ||
* class Example { | ||
* {@code @interface} MyAnnotation { | ||
* String name() default "foo"; | ||
* int number() default 23; | ||
* } | ||
* | ||
* {@code @AutoAnnotation} | ||
* static MyAnnotation myAnnotation(String value) { | ||
* return new AutoAnnotation_Example_myAnnotation(value); | ||
* } | ||
* | ||
* {@code @AutoBuilder(callMethod = "myAnnotation")} | ||
* interface MyAnnotationBuilder { | ||
* MyAnnotationBuilder name(String name); | ||
* MyAnnotationBuilder number(int number); | ||
* MyAnnotation build(); | ||
* } | ||
* | ||
* static MyAnnotationBuilder myAnnotationBuilder() { | ||
* return new AutoBuilder_Example_MyAnnotationBuilder(); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* Here, {@code myAnnotationBuilder().build()} is the same as {@code | ||
* myAnnotationBuilder().name("foo").number(23).build()} because those are the defaults in the | ||
* annotation definition. | ||
* | ||
* @author [email protected] (Éamonn McManus) | ||
*/ | ||
@Target(ElementType.METHOD) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters