In addition to Retention, there are three other annotations provided by JDK 5, namely Target, Inherited and Documented.
The Target target is where the Target annotation is used, which defines the timing of the annotation being used, that is, the type of program elements to which the annotation is applicable. If a Target meta annotation does not exist in the annotation type declaration, the declared type can be used on any program element. If such a metaannotation exists, the compiler enforces the specified usage limit.
Target is defined as follows:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target { ElementType[] value();}As you can see, Target has only one value attribute, and its type is the enum type ElementType. The ElementType declaration is as follows:
public enum ElementType { /** Annotations can be used in classes, interfaces (including annotation types) or enum declarations */ TYPE, /** field declarations (including enum constants) */ FIELD, /** method declarations */ METHOD, /** parameter declarations */ PARAMETER, /** Constructor method declarations */ CONSTRUCTOR, /** local variable declarations */ LOCAL_VARIABLE, /** annotation type declarations */ ANNOTATION_TYPE, /** package declarations */ PACKAGE}Documented annotations indicate whether the annotation information is added to the document when making javadoc. If the annotation uses @Documented when declaring, the annotation information will be added to the javadoc when making the javadoc. The annotation statement is as follows:
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)//Indicate that this annotation can only be used when declaring annotation, that is, meta annotation public @interface Documented {}The Inherited annotation is also a meta annotation, and the declaration is as follows:
@Documented@Retention(value=RUNTIME)@Target(value=ANNOTATION_TYPE)public @interface Inherited {}Inherited annotations indicate whether the annotations will be inherited by subclasses, and the default is not inherited. When an annotation is declared, the @Inherited annotation is used, then the annotation will be inherited by a subclass of the class using the annotation.