Annotation Type AutoValue.CopyAnnotations
-
@Retention(CLASS) @Target({TYPE,METHOD}) public static @interface AutoValue.CopyAnnotations
Specifies that AutoValue should copy any annotations from the annotated element to the generated class. This annotation supports classes and methods.The following annotations are excluded:
- AutoValue and its nested annotations;
- any annotation appearing in the
exclude()field; - any class annotation which is itself annotated with the
Inheritedmeta-annotation.
For historical reasons, annotations are always copied from an
@AutoValueproperty method to its implementation, unless@CopyAnnotationsis present and explicitly excludes that annotation. But annotations are not copied from the@AutoValueclass itself to its implementation unless@CopyAnnotationsis present.If you want to copy annotations from your @AutoValue-annotated class's methods to the generated fields in the AutoValue_... implementation, annotate your method with @AutoValue.CopyAnnotations. For example, if Example.java is:
@Immutable@AutoValue abstract class Example {@CopyAnnotations@SuppressWarnings("Immutable") // justification ... abstract Object getObject(); // other details ... }Then AutoValue will generate the following AutoValue_Example.java:
final class AutoValue_Example extends Example {@SuppressWarnings("Immutable") private final Object object;@SuppressWarnings("Immutable")@Override Object getObject() { return object; } // other details ... }When the type of an
@AutoValueproperty method has annotations, those are part of the type, so by default they are copied to the implementation of the method. But if a type annotation is mentioned inexcludethen it is not copied.For example, suppose
@Confidentialis aTYPE_USEannotation:
Then the implementation of the@AutoValue abstract class Person { static Person create(@Confidential String name, int id) { return new AutoValue_Person(name, id); } abstract@Confidential String name(); abstract int id(); }name()method will also have return type@Confidential String. But ifname()were written like this...@AutoValue.CopyAnnotations(exclude = Confidential.class)abstract@Confidential String name();...then the implementation of
name()would have return typeStringwithout the annotation.- Author:
- Carmi Grushko
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Class<? extends Annotation>[]exclude
-
-
-
Element Detail
-
exclude
Class<? extends Annotation>[] exclude
- Default:
- {}
-
-