Java Programming/Annotations/Meta-Annotations

There are five annotation types in the  package called meta-annotations. These annotation types are used to annotate other annotation types.

Documented
If a member is annotated with a type itself marked as, then that member will be documented as annotating that type.

In the documentation for the  class, such as the JavaDoc,   will be shown as annotated with , but not.

Inherited
Exactly as the name sounds, an  annotation type is inherited by subclasses of an annotated type.

In this example,  has been explicitly annotated with both   and. hasn't been explicitly marked with either one; however, it inherits  because the latter is annotated with. isn't annotated, so it isn't inherited by.

Repeatable
A  annotation type is repeatable - i.e. can be specified multiple times on the same class. This meta-annotation was added in Java 8.

Retention
Different annotation types have different purposes. Some are intended for use with the compiler; others are meant to be reflected dynamically at runtime. There's no reason for a compiler annotation to be available at runtime, so the  meta-annotation specifies how long an annotation type should be retained. The  attribute is one of the   enum constants. The possible values, in order from shortest to longest retention, are as follows:


 * The annotation will not be included in the class file. This is useful for annotations which are intended for the compiler only.
 * The annotation will not be included in the class file. This is useful for annotations which are intended for the compiler only.


 * The annotation will be included in the class file, but cannot be read reflectively.
 * The annotation will be included in the class file, but cannot be read reflectively.


 * The annotation can be reflected at runtime.
 * The annotation can be reflected at runtime.

If no  policy is specified, it defaults to.

Target
The  meta-annotation determines what may be marked by the annotation. The  attribute is one or more of the   enum constants. Those constants are,  ,  ,  ,  ,  ,  , and.

If  is not specified, the annotation may be used on any program element.