The principle of Java annotation
java: Use annotation, //www.VeVB.COM/article/101747.htm
What is annotation
Annotations are also called metadata, such as our common @Override and @Deprecated. Annotations are a feature introduced in the JDK1.5 version. They are used to explain the code and can annotate packages, classes, interfaces, fields, method parameters, local variables, etc. Its main functions are as follows:
General annotations can be divided into three categories:
One category is the standard annotations that come with Java, including @Override, @Deprecated and @SuppressWarnings, which are used to indicate that a certain method is rewrite, a certain class or method is outdated, and a warning to be ignored. After these annotations are marked, the compiler will check it.
One type is meta annotation. Meta annotation is annotation used to define annotations, including @Retention, @Target, @Inherited, @Documented. @Retention is used to indicate the stage where the annotation is retained, @Target is used to indicate the scope of the annotation used, @Inherited is used to indicate that the annotation can be inherited, and @Documented is used to indicate whether to generate a javadoc document.
One type is custom annotations. You can define annotations according to your own needs and use meta annotations to annotate custom annotations.
Annotation principle:
Let’s see how to support annotations under the Java system. Let’s go back to the above example of custom annotation. For annotation Test, as follows. If you annotate the AnnotationTest class, the value of the annotation declaration can be obtained through AnnotationTest.class.getAnnotation(Test.class) at the runtime. It can be seen from the above sentence that it obtains the Test annotation from the class structure, so it must be that the annotation was added to the class structure at some time.
@Test("test") public class AnnotationTest { public void test(){ } }From java source code to class bytecode is done by the compiler. The compiler will parse the java source code and generate a class file. Annotations are also processed by the compiler at compile time. The compiler will process annotation symbols and attach them to the class structure. According to the jvm specification, the class file structure is a strictly ordered format. The only way to attach information to the class structure is to save it into the attributes attributes of the class structure. We know that for classes, fields, and methods, there are their own specific table structures in the class structure, and each has its own attributes. For annotations, the scope of their functions can also be different. They can be used on classes, or on fields or methods. At this time, the compiler will store the annotation information on the attributes of the class, fields, and methods accordingly.
After our AnnotationTest class is compiled, a RuntimeVisibleAnnotations property will be included in the corresponding AnnotationTest.class file. Since this annotation is applied to the class, this property is added to the class's attribute set. That is, the key-value pair value=test of the Test annotation will be recorded. When the JVM loads the bytecode of the AnnotationTest.class file, the RuntimeVisibleAnnotations property value will be saved to the Class object of AnnotationTest, so the Test annotation object can be obtained through AnnotationTest.class.getAnnotation(Test.class), and then the property value in Test is obtained through the Test annotation object.
There may be questions here, what is the Test annotation object? In fact, the essence of annotation after being compiled is an interface that inherits the Annotation interface, so @Test is actually "public interface Test extends Annotation". When we call through AnnotationTest.class.getAnnotation(Test.class), the JDK will generate an object that implements the Test interface through dynamic proxy, and set the RuntimeVisibleAnnotations property value into this object. This object is a Test annotation object. The annotation value can be obtained through its value() method.
The entire process of Java annotation implementation mechanism is shown above. Its implementation requires the cooperation of the compiler and the JVM.
Thank you for reading, I hope it can help you. Thank you for your support for this site!