The meaning of Java annotations Retention, Documented, Target

 

Retention annotation

The Retention (retention) annotation indicates that this type of annotation will be retained to that stage. There are three values: 
1.RetentionPolicy.SOURCE - This type of Annotations is only retained at the source code level and will be ignored at compile time 
2. RetentionPolicy.CLASS - Annotations of this type are retained at compile time and exist in the class file, but will be ignored 
by the JVM Read and use by the JVM or other code that uses reflection mechanisms.

In the example below, the @Retention(RetentionPolicy.RUNTIME) annotation indicates that the Test_Retention annotation will be retained by the virtual machine so that it can be read by reflection at runtime.

Documented annotations

The Documented annotation indicates that this annotation should be documented by the javadoc tool. By default, javadoc does not include annotations. But if @Documented is specified when declaring an annotation, it will be processed by tools such as javadoc, so the annotation type information will also be used. Included in the generated document. (Personal opinion: not the point, just understand. Don't spray)

Target annotation

@Target specifies the scope of objects modified by Annotation: Annotation can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumeration values), method parameters and Local variables (such as loop variables, catch parameters). The use of target in the declaration of the Annotation type can be more explicit about the target of its modification. 
Role: used to describe the scope of use of the annotation (ie: where the described annotation can be used) The 
values ​​(ElementType) are:

1.CONSTRUCTOR: used to describe the constructor 
2.FIELD: used to describe the domain 
3.LOCAL_VARIABLE: used to describe the local variables 
4.METHOD: used to describe the method 
5.PACKAGE: used to describe the package 
6.PARAMETER: used to describe the parameters 
7.TYPE: used to describe classes, interfaces (including annotation types) or enum declarations

Inherited annotation

This is a slightly more complex type of annotation. It indicates that the annotated class will be automatically inherited. More specifically, if you define an annotation with the @Inherited tag, and then use the defined annotation to annotate another parent class, the parent class has A subclass (subclass), all the properties of the parent class will be inherited into its subclass.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340227&siteId=291194637