This article refers to the documentation of many great gods, especially thanks again for sharing!!
1. What is an annotation?
Concepts: annotations (Annotation), also called Meta data. A description of the code level.
It is a feature introduced in JDK1.5 and later versions, with classes, interfaces, and enumerations at the same level.
It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on, to annotate these elements.
Annotations provides some data that would otherwise not be part of the program.
2. Classification of annotations
2.1 follow the operating mechanism (Will speak later)
SOURCE notes:
Annotations exist only in the source code and are compiled into a. class file that does not exist.
Compile-time annotations:
Annotations exist in both the source code and the. class file. (ex: three Annotations to JDK)
Run-time Annotations:
It also works in the run phase and even affects the annotations that run the logic.
2.2 according to the source of
Note from JDK: The three @override of the above picture @Deprecated @SuppressWarning,
Third-party annotations: such as the spring frame, mybatis frame annotations and so on.
Self-defined annotations: We'll talk about it in detail later.
3.JDK bring your own annotations
@Override means that the current method overrides the parent class
@Deprecation indicates that the method is obsolete, there are horizontal lines on the method, and warnings are used.
@SuppviseWarnings means to turn off some warning messages (notifies the Java compiler to ignore specific compilation warnings)
Section 1 Part Annotation Architecture
First look at the architecture diagram of annotation:
From this, we can see:
(01) 1 Annotation and one Retentionpolicy association.
It can be understood that each of the 1 annotation objects will have a unique retentionpolicy attribute.
(02) 1 annotation and 1~n elementtype associations.
It can be understood that there are several ElementType properties for each of the 1 annotation objects.
Annotation has many implementation classes, including: Deprecated, documented, inherited, override, and so on.
Each implementation class of Annotation is "associated with a Retentionpolicy" and "is associated with 1~n ElementType".
Section 2 Part Annotation Component
1 Annotation constituent Components
In the composition of Java annotation, there are 3 very important backbone classes. Each of them is:
(Annotation.java)
Annotationtype ();} " v:shapes= "text box _x0020_2" >
(Elementtype.java)
(Retentionpolicy.java)
Description
(Annotation) is an interface.
"Every 1 annotation" is associated with "1 Retentionpolicy" and is associated with "1~n ElementType". Can be commonly understood as: Every 1 annotation objects, there will be a unique Retentionpolicy property, as for the ElementType property, there are 1~n.
ElementType is an enum enumeration type that 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 that annotation has some purpose.
For example, if a annotation object is a method type, the annotation can only be used to modify the methods.
Retentionpolicy is an enum enumeration type that is used to specify the annotation policy. The popular point is that different retentionpolicy types of annotation have different scopes.
"Every 1 annotation" is associated with "1 Retentionpolicy".
A) If the type of annotation is SOURCE, it means that annotation only exists during compiler processing and that the annotation is useless after the compiler finishes processing.
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 the syntax is checked during compilation! When the compiler finishes processing, "@Override" has no effect.
b) If the type of annotation is class, it means that the compiler stores the annotation in the. class file for the classes, which is the default behavior of annotation.
c) If the type of annotation 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 " Association, and with " 1~n x ElementType " Association . It will be easier to understand the content after you have finished learning what's behind it.
Section 3 Part Java Bring your own Annotation
After understanding the function of the above 3 classes, we can then explain the syntax definition of the annotation implementation class.
1 Annotation
Generic Definition
@Documented
@Target (Elementtype.type)
@Retention (Retentionpolicy.runtime)
Public @interface MyAnnotation1 {
}
Description
The function above is to define a annotation, whose name is MyAnnotation1.
Once we have defined MyAnnotation1, we can use it in our code through "@MyAnnotation1".
The other, @Documented, @Target, @Retention, @interface are all to decorate MyAnnotation1. Here's what they mean:
(@interface)
When you use @interface to define annotations, it means that it implements the Java.lang.annotation.Annotation interface, that is, the annotation is a annotation.
when defining Annotation ,@interface is required.
Note: It differs from our usual method of implemented implementing interfaces. The implementation details of the annotation interface are completed by the compiler. After you define annotations through @interface, the annotations cannot inherit other annotations or interfaces.
(@Documented)
The annotation of classes and methods are not present in Javadoc by default. If the annotation is decorated with @documented, it can appear in Javadoc.
definition Annotation when the @Documented if not defined, the Annotation does not appear in Javadoc the.
(Geneva) @Target (Elementtype.type)
As we said earlier, ElementType is the type attribute of annotation. The function of @target is to specify the type attribute of the annotation.
@Target (elementtype.type) means that specifying the type of annotation is elementtype.type. This means that MyAnnotation1 is the annotation that modifies the class, interface (including the annotation type), or enum declaration.
definition Annotation when the @Target Optional. If there is a @Target, the Annotation can only be used where it is specified, and if there is no @Target, the Annotation can be used anywhere.
(@Retention) (Retentionpolicy.runtime)
As we said earlier, Retentionpolicy is the annotation policy attribute, and @retention's role is to specify the annotation's policy attributes.
@Retention (retentionpolicy.runtime) means that the annotation policy is retentionpolicy.runtime. This means that the compiler retains the annotation information in the. class file and can be read by the virtual machine.
definition Annotation when the @Retention Optional. If there is no @Retention, the default is retentionpolicy.class.
2 Java Bring your own Annotation
From the above example, we can understand:
@interface used to declare annotation,
@Documented is used to indicate whether the annotation will appear in Javadoc,
@Target used to specify the type of annotation,
@Retention the policy used to specify annotation.
Java used in Annotation :
@Deprecated-The contents of the @Deprecated are no longer recommended for use.
@Override--@Override can only label methods, which means that the method overrides a method in the parent class.
@Documented-The contents of the @Documented can appear in the Javadoc.
@Inherited-@Inherited can only be used to label the "annotation type", and the annotation it marks is inherited.
@Retention-@Retention can only be used to label the "annotation type", and it is used to specify the Retentionpolicy property of annotation.
@Target-@Target can only be used to label the "annotation type", and it is used to specify the ElementType property of annotation.
@SuppressWarnings-The compiler will silence these warnings @SuppressWarnings the warnings generated by the content being labeled.
Section 4 Part Annotation the role
Annotation is an auxiliary class that is used extensively in the tools framework of JUnit, Struts, and spring.
The annotation functions that we often use in programming are:
1 Compile Check
Annotation has the role of having the compiler check for compilation.
2 Using annotation in reflection
In the Reflection class, Method, field and other functions, there are many interfaces related to annotation.
This also means that we can parse and use annotation in reflection.
Java Annotations Related