What is java annotation and its principle are introduced in detail

Principles of Java annotations

Java: annotation usage, https://www.oudahe.com/p/23180/

What is annotation

Annotations are also called metadata, such as @ override and @ deprecated, and the annotation is jdk1 A feature introduced in version 5 is used to describe the code and annotate packages, classes, interfaces, fields, method parameters, local variables, etc. Its main functions are as follows:

General notes can be divided into three categories:

One is Java's own standard annotations, including @ override, @ deprecated and @ suppresswarnings, which are respectively used to indicate that a method is overridden, that a class or method is obsolete, and that warnings are to be ignored. After these annotations are marked, the compiler will check.

One is meta annotation, which is used to define annotations, including @ retention, @ target, @ inherited, @ documented, @ retention is used to indicate the stage in which annotations are retained, @ target is used to indicate the scope of annotation use, @ inherited is used to indicate that annotations can be inherited, and @ documented is used to indicate whether Javadoc documents are generated.

One is user-defined annotations, which can be defined according to their own needs, and meta annotations can be used to annotate user-defined annotations.

Annotation principle:

See how annotations are supported under the Java system. Let's go back to the example of custom annotation above. For annotation test, as shown below, if annotationtest class is annotated, the runtime can use annotationtest class. Getannotation (test. Class) gets the value of the annotation declaration. As can be seen from the above sentence, it gets the test annotation from the class structure, so the annotation must be added to the class structure at some time.

From Java source code to class bytecode is completed by the compiler. The compiler will parse the Java source code and generate class files, and annotations are processed by the compiler during compilation. The compiler will process annotation symbols and attach them to the class structure. According to the JVM specification, the class file structure is a strict and orderly format, The only way to attach information to the class structure is to save it in the attributes attribute of the class structure. We know that for classes, fields and methods, each has its own specific table structure in the class structure, and each has its own attributes. For annotations, the scope of action can also be different, which can act on classes, fields or methods. At this time, the compiler will store the annotation information in its own attributes of classes, fields and methods.

After our annotationtest class is compiled, in the corresponding annotationtest The class file will contain a runtimevisibleannotations attribute. Since this annotation is used on the class, this attribute is added to the property set of the class. That is, the key value pair value = Test of the test annotation will be recorded. When the JVM loads annotationtest Class file bytecode, the runtimevisibleannotations attribute value will be saved to the class object of annotationtest, so you can use annotationtest class. Getannotation (test. Class) obtains the test annotation object, and then obtains the attribute value in test through the test annotation object.

There may be questions here. What is the test annotation object? In fact, the essence of annotation after compilation is an interface that inherits the annotation interface, so @ test is actually "public interface test extends Annotation". When we pass the annotation test class. When getannotation (test. Class) is called, JDK will generate an object that implements the test interface through the dynamic agent, and set the runtimevisibleannotations property value into this object. This object is the test annotation object, and the annotation value can be obtained through its value () method.

The whole process of Java annotation implementation mechanism is shown above. Its implementation needs the cooperation of compiler and JVM.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>