Detailed java meta-annotations

table of Contents

One, @Target

Two, @Retention

三、@Documented

四、@Inherited

Five, @Repeatable


One, @Target

Used to specify the modified custom annotations can only be used to modify which elements in the program, the meta annotation has the following attribute values:
1. ElementType. ANNOTATION_TYPE : meta annotation applied to other annotations
2. ElementType. CONSTRUCTOR : applied to the constructor
3. ElementType. FIELD : applied to global attributes
4. ElementType. LOCAL_VARIABLE : applied to local variables in methods
5. ElementType. METHOD : applied to methods
6. ElementType. PACKAGE : applied to packages
7. ElementType. PARAMETER : applied to methods the parameter
8, ElementType. the tYPE : apply to class, interface, or enum declaration

Custom @SuppressWarnings annotation, restrict the annotation can only be used to modify attributes and methods

 

Two, @Retention

Used to specify how long the modified custom annotation can be retained. The meta annotation has the following attribute values:

1. RetentionPolicy. SOURCE : The compiler will directly discard the modified annotations .
2. RetentionPolicy. CLASS : The default value . The compiler will record the annotations in the class file. When running the Java program, the virtual machine no longer retains the annotations ;
3. RetentionPolicy. RUNTIME : The compiler will record the annotations in the class file . When running a java program, the virtual machine retains the annotation, and the program can obtain the annotation through reflection ;

三、@Documented

When the javadoc command is executed, the custom annotation modified by the meta annotation will also be generated in the document , as in the following example:

Perform the following operations in Eclipse:
1. Select the java project  right-click on "Export...", the following figure appears:

3. Enter javadoc in the red box in the above picture  click the "Next >" button, the following picture will appear:

Note: If the @Documented meta-annotation does not modify the previous Override custom annotation , the generated document will not have the part framed by the red frame in the above figure .


四、@Inherited

If the annotation used by the parent class is modified by @Inherited, the child class can inherit the annotation , otherwise it cannot.

Five, @Repeatable

The modified custom annotations can be used repeatedly on the same class, method or variable .

The equivalent (not "same") of code 3 is the following code:

Code 3 is equivalent (not "effective") to the following code:

 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/113531331