Annotation Type AutoAnnotation
-
@Retention(CLASS) @Target(METHOD) public @interface AutoAnnotation
Annotation that causes an implementation of an annotation interface to be generated. The annotation is applied to a method whose return type is an annotation interface. The method can then create and return an instance of the generated class that conforms to the specification ofAnnotation
, in particular as regardsequals
andhashCode
. These instances behave essentially the same as instances returned byAnnotatedElement.getAnnotation(java.lang.Class<T>)
.For example, suppose you have an annotation like this:
package com.google.inject.name; public @interface Named { String value(); }
You could write a method like this to construct implementations of the interface:
package com.example; public class Names { @AutoAnnotation public static Named named(String value) { return new AutoAnnotation_Names_named(value); } }
Because the annotated method is called
Names.named
, the generated class is calledAutoAnnotation_Names_named
in the same package. If the annotated method were in a nested class, for exampleOuter.Names.named
, then the generated class would be calledAutoAnnotation_Outer_Names_named
. The generated class is package-private and it is not expected that it will be referenced outside the@AutoAnnotation
method.The names and types of the parameters in the annotated method must be the same as the names and types of the annotation elements, except that elements which have default values can be omitted. The parameters do not need to be in any particular order.
The annotated method does not need to be public. It can have any visibility, including private. This means the method can be a private implementation called by a public method with a different API, for example using a builder.
It is a compile-time error if more than one method with the same name in the same class is annotated
@AutoAnnotation
.The constructor of the generated class has the same parameters as the
@AutoAnnotation
method. It will throwNullPointerException
if any parameter is null. In order to guarantee that the constructed object is immutable, the constructor will clone each array parameter corresponding to an array-valued annotation member, and the implementation of each such member will also return a clone of the array.If your annotation has many elements, you may consider using
@AutoBuilder
instead of@AutoAnnotation
to make it easier to construct instances. In that case,default
values from the annotation will become default values for the values in the builder. For example:class Example {
Here,@interface
MyAnnotation { String name() default "foo"; int number() default 23; }@AutoBuilder(ofClass = MyAnnotation.class)
interface MyAnnotationBuilder { MyAnnotationBuilder name(String name); MyAnnotationBuilder number(int number); MyAnnotation build(); } static MyAnnotationBuilder myAnnotationBuilder() { return new AutoBuilder_Example_MyAnnotationBuilder(); } }myAnnotationBuilder().build()
is the same asmyAnnotationBuilder().name("foo").number(23).build()
because those are the defaults in the annotation definition.- Author:
- [email protected] (Éamonn McManus)