Home > Java > javaTutorial > body text

What are annotations in Java and a detailed introduction to their principles

高洛峰
Release: 2017-01-23 15:29:04
Original
1434 people have browsed it

What are annotations

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 package , classes, interfaces, fields, method parameters, local variables, etc. are annotated. Its main functions are as follows:

Generate documents and generate javadoc documents through the metadata identified in the code.

Compilation check allows the compiler to check and verify during compilation through the metadata identified in the code.

Dynamic processing at compile time. Dynamic processing at compile time through the metadata identified in the code, such as dynamically generating code.

Dynamic processing at runtime. Dynamic processing at runtime through the metadata identified in the code, such as using reflection injection instances.

General annotations can be divided into three categories:

The first category is the standard annotations that come with Java, including @Override, @Deprecated and @SuppressWarnings, which are used to indicate overriding a method, respectively. Mark a class or method as obsolete and mark warnings to be ignored. The compiler will check them after marking them with these annotations.

The first category is meta-annotations. Meta-annotations are annotations used to define annotations, including @Retention, @Target, @Inherited, @Documented. @Retention is used to indicate the stage in which the annotation is retained, @ Target is used to indicate the scope of use of annotations, @Inherited is used to indicate that annotations can be inherited, and @Documented is used to indicate whether to generate javadoc documents.

The first category is custom annotations. You can define annotations according to your own needs, and use meta-annotations to annotate custom annotations.

Annotation principle:

Look at how annotations are supported under the Java system. Let’s go back to the custom annotation example above. For the annotation Test, as follows, if the AnnotationTest class is annotated, the value of the annotation declaration can be obtained at runtime through AnnotationTest.class.getAnnotation(Test.class). From the above sentence, you can It can be seen that it obtains the Test annotation from the class structure, so the annotation must have been added to the class structure at some point.

@Test("test")
public class AnnotationTest {
public void test(){
}
}
Copy after login

The process from java source code to class bytecode is completed by the compiler. The compiler will parse the java source code and generate class files. Annotations are also processed by the compiler during compilation. The compiler will The annotation symbols are processed and appended to the class structure. According to the JVM specification, the class file structure is in a strictly ordered format. The only way to append information to the class structure is to save it in the attributes attribute of the class structure. We know that classes, fields, and methods have their own specific table structures in the class structure, and each has its own attributes. As for annotations, their scope of action can also be different. They can act on classes or on On a field or method, the compiler will store the annotation information in the properties of the class, field, or method.

After our AnnotationTest class is compiled, the corresponding AnnotationTest.class file will contain a RuntimeVisibleAnnotations attribute. Since this annotation is applied to the class, this attribute is added to the attribute set of the class. That is, the key-value pair value=test of the Test annotation will be recorded. When the JVM loads the AnnotationTest.class file bytecode, it will save the RuntimeVisibleAnnotations attribute value to the Class object of AnnotationTest, so the Test annotation object can be obtained through AnnotationTest.class.getAnnotation(Test.class), and then through The Test annotation object obtains the attribute values ​​​​in Test.

You may have questions here, what is the Test annotation object? In fact, the essence of the compiled annotation is an interface that inherits the Annotation interface, so @Test is actually "public interface Test extends Annotation". When we call it through AnnotationTest.class.getAnnotation(Test.class), the JDK will generate it through a dynamic proxy An object that implements the Test interface, and sets the RuntimeVisibleAnnotations attribute value into this object. This object is the Test annotation object, and the annotation value can be obtained through its value() method.

The entire process of Java annotation implementation mechanism is as shown above. Its implementation requires the cooperation of the compiler and JVM.

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more Java information about what annotations are and their principles, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!