Java annotation is an annotation mechanism introduced by JDK5.0.
A lot of articles about Java annotation on the Internet, see people dazzling. Java annotation would have been very simple, the result of the people did not say clearly, the person who made the look more confused.
According to my own thinking, I annotation. The key to understanding the Annotation is to understand the grammar and usage of Annotation, I have made a detailed description of these, I understand the Annotation grammar and usage, then look at the Annotation frame diagram, may have more profound experience. To say so much nonsense, the following begins to explain the annotation. If you find that there are errors or deficiencies in the article, I hope you can point out!
Part 1th Annotation Architecture
Let's take a look at annotation's architectural chart:
From this, we can see:
(01) 1 Annotation and one Retentionpolicy association. Can be understood as: Every 1 annotation objects, there will be unique retentionpolicy properties.
(02) 1 annotation and 1~n elementtype associations. You can understand that for every 1 annotation objects, you can have several ElementType properties.
Annotation has many implementation classes, including: deprecated, documented, inherited, override, and so on. Each implementation class of the Annotation is "associated with a Retentionpolicy" and "is associated with a 1~n ElementType."
Let me first introduce the left half of the frame (pictured below), annotation, Retentionpolicy, ElementType, and then give an example of the annotation implementation class.
Part 2nd parts of annotation
1 Annotation composition
In the composition of the Java annotation, there are 3 very important backbone classes. They are:
(Annotation.java)
Package java.lang.annotation;
Public interface Annotation {
boolean equals (Object obj);
int hashcode ();
String toString ();
class<? Extends annotation> annotationtype ();
}
(Elementtype.java)
Package java.lang.annotation;
public enum ElementType {
type,/ * class, interface (including annotation type) or enum declaration/
field,/ * Fields declaration (including enumerated constants) * *
methods,/ * method declaration/ PARAMETER,/* parameter declaration//
Constructor,/ * Construction Method declaration
* * local_variable,/ * local variable declaration//
Annotation_type,/ * annotation type declaration */PACKAGE/ * Package Declaration */
}
(Retentionpolicy.java)
Package java.lang.annotation;
public enum Retentionpolicy {
SOURCE, /* annotation information exists only during compiler processing, the compiler does not have the annotation information after processing.
class, The/ * Compiler stores annotation in the. class file corresponding to the classes. Default behavior /
RUNTIME/* The compiler stores annotation in a class file and can be read by the JVM/
}
Description: (Annotation) is an interface. "Every 1 annotation" is associated with "1 Retentionpolicy" and is associated with "1~n ElementType". Can be understood as a popular: Every 1 annotation objects, there will be unique retentionpolicy attributes, as for the ElementType property, there are 1~n.
ElementType is the Enum enumeration type, which is used to specify the type of annotation. "Every 1 annotation" is associated with "1~n ElementType". When annotation is associated with a elementtype, it means: annotation has a purpose. For example, if a annotation object is a method type, then the annotation can only be used to modify methods.
(Retentionpolicy) is the Enum enumeration type, which is used to specify the annotation policy. Popular point, that is, different retentionpolicy types of annotation different scopes. "Every 1 annotation" is associated with "1 Retentionpolicy". A if the type of annotation is SOURCE, it means: annotation only exists during compiler processing, the annotation is useless when the compiler is finished. For example, the "@Override" flag is a annotation. When it modifies a method, it means that the method overrides the method of the parent class, and a grammar check is performed during compilation! When the compiler finishes processing, the "@Override" has no effect. b If the annotation type is class, it means that the compiler stores the annotation in the. class file corresponding to the class, which is the default behavior of annotation. c If the annotation type is RUNTIME, it means that the compiler stores annotation in the class file and can be read by the JVM.
At this point, just remember that "every 1 annotation" is associated with "1 Retentionpolicy" and is associated with "1~n ElementType". After learning the rest of the content, look back at the content, it will be easier to understand.
The 3rd part is the annotation of the Java self-band
After understanding the function of the 3 classes above, we can then explain the syntax definition of the annotation implementation class.
@Documented @Target (Elementtype.type) @Retention (retentionpolicy.runtime) public @interface MyAnnotation1 {}
Description
The function above is to define a annotation, whose name is MyAnnotation1. After defining the MyAnnotation1, we can use it in the code through "@MyAnnotation1". Other, @Documented, @Target, @Retention, @interface are to modify the MyAnnotation1. Let's say what they mean:
@interface defining annotations using @interface means that it implements the Java.lang.annotation.Annotation interface, that is, the annotation is a annotation. @interface is required when defining annotation. Note: It is different from the way we normally implemented implement interfaces. The implementation details of the annotation interface are completed by the compiler. After the annotation is defined by @interface, the annotation cannot inherit other annotations or interfaces.
The annotation of @Documented classes and methods are not present in Javadoc by default. If you use @documented to decorate the annotation, it means that it can appear in Javadoc. @Documented is optional when defining annotation, and annotation does not appear in Javadoc if there is no definition.
@Target (Elementtype.type), as we said earlier, ElementType is the annotation type attribute. The @target function is to specify the type attribute of the annotation. The meaning of @Target (Elementtype.type) is to specify that the annotation type is elementtype.type. This means that MyAnnotation1 is the annotation that modifies the "class, interface (including annotation type) or enumeration declaration." When defining annotation, @Target dispensable. If there is a @target, the annotation can only be used where it is specified, and if there is no @target, the annotation may be used anywhere.