The concept of annotations
Annotations (Annotation), also called metadata (Metadata), are a new feature of Java5, and JDK5 introduces Metadata easily to invoke annotations. Annotations are used in the same hierarchy as classes, interfaces, enumerations, and can be applied to packages, types, construction methods, methods, member variables, parameters, and declarations of local variables to annotate these elements.
Syntax and definition form of annotations
(1) define with @interface keyword
(2) annotations contain members, and members are declared in the form of a parameterless method. Its method name and return value define the name and type of the member.
(3) The member assignment is in the form of @annotation (Name=value).
(4) Annotations need to indicate the life cycle of the annotations, the modified target of the annotations, etc., which are realized through meta annotations.
The Target annotations defined in Java.lang.annotation illustrate:
[Java]View Plain copy
- @Retention (value = retentionpolicy.runtime)
- @Target (value = {Elementtype.annotation_type})
- Public @interface Target {
- Elementtype[] Value ();
- }
The source code analysis is as follows:
First: Meta annotation @retention, the value of member value is retentionpolicy.runtime.
Second: meta-annotation @target, member value is a number of groups, with the form of {}, the value is Elementtype.annotation_type
Third: The member name is value and the type is elementtype[]
Also, be aware that if the member name is value, it can be abbreviated during the assignment. You can also shorthand if the member type is an array, but only one element is assigned. As the abbreviated form above is:
[Java]View Plain copy
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.annotation_type)
- Public @interface Target {
- Elementtype[] Value ();
- }
Classification of annotations
There are two types of classification of annotations:
The first method of division
1, the basic built-in annotations, refers to Java comes with a few annotation, such as @override, Deprecated, @SuppressWarnings, etc.;
2, meta-annotations (meta-annotation), is responsible for annotating the annotations of other annotations, JDK 1.5 and later versions define 4 standard meta-annotation types, as follows:
- @Target
- @Retention
- @Documented
- @Inherited
3, custom annotations, as needed can be customized annotations, custom annotations need to use the above meta-annotation
The second method of division
Annotations need to indicate the life cycle of the annotations, which are implemented through the meta-annotations @Retention , and the values of the annotations are retentionpolicyof the enum type, including the following:
[Java]View Plain copy
- Public enum Retentionpolicy {
- /**
- * Annotations are retained only in the source file, and the annotations are discarded when the Java file is compiled into a class file.
- * This means: annotation only exists during compiler processing, and after the compiler finishes processing, the annotation is useless.
- */
- SOURCE,
- /**
- * Annotations are persisted to the class file, but the JVM is discarded when loading the class file, which is the default life cycle.
- */
- CLASS,
- /**
- * Annotations are not only saved to the class file, but are still present after the JVM loads the class file.
- * Saved to class object, can be obtained by reflection
- */
- RUNTIME
- }
Meta annotations
As described above, Java defines 4 standard meta-annotations:
@Documented: Markup annotations, which describe other types of annotations that should be used as public APIs for annotated program members, can be documented by tools such as Javadoc.
[Java]View Plain copy
- @Documented
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.annotation_type)
- Public @interface documented {
- }
@Inherited: Tagging annotations , allowing subclasses to inherit annotations from the parent class
[Java]View Plain copy
- @Documented
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.annotation_type)
- Public @interface inherited {
- }
@Retention: Refers to the length of time annotation is retained, indicating the life cycle of the annotation, 3 kinds of retentionpolicy value meaning above to explain
[Java]View Plain copy
- @Documented
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.annotation_type)
- Public @interface Retention {
- Retentionpolicy value ();
- }
@Target: A decorated target that indicates the annotation, a common
[Java]View Plain copy
- @Documented
- @Retention (Retentionpolicy.runtime)
- @Target (Elementtype.annotation_type)
- Public @interface Target {
- Elementtype[] Value ();
- }
- ElementType value
- Public enum ElementType {
- /** class, interface (including annotation type) or enumeration */
- TYPE,
- /** field property, also includes annotations used by enum constants * /
- FIELD,
- /** method * /
- METHOD,
- /** Parameters * /
- PARAMETER,
- /** Constructor * /
- CONSTRUCTOR,
- /** Local Variables * /
- Local_variable,
- meta-annotations used on/** annotations * /
- Annotation_type,
- /** bag * /
- Package
- }
Summary of basic concepts of Java annotations