What is Java annotation and annotation principle

  • 2020-05-26 08:37:09
  • OfStack

Principle of Java annotations

java: use annotations, https: / / www ofstack. com article / 101747. htm

What are annotations

Annotations are also called metadata, such as the common @Override and @Deprecated. Annotations are a feature introduced at the beginning of JDK1.5 to explain code, including packages, classes, interfaces, fields, method parameters, local variables, and so on. Its main functions are as follows:

Generate the javadoc document from the metadata identified in the code. Compile checking, which allows the compiler to check for validation at compile time with the metadata identified in the code. Compile-time dynamic processing, which is done at compile time through metadata identified in the code, such as dynamic code generation. Runtime dynamic processing, which is done by metadata identified in the code, for example by injecting instances with reflection.

1. General annotations can be divided into three categories:

Class 1 is the standard annotation that comes with Java, including @Override, @Deprecated, and @SuppressWarnings, which are used to indicate that a method has been overwritten, that a class or method is out of date, and that it is to be ignored.

The first category is meta-annotations, which are annotations used to define annotations, including @Retention, @Target, @Inherited, @Documented, @Retention to indicate the stage at which the annotation is retained, @Target to indicate the scope of use of the annotation, @Inherited to indicate that the annotation is inheritable, and @Documented to indicate whether an javadoc document is generated.

Class 1 is a custom annotation, which can be defined according to your own requirements, and can be annotated with meta-annotations.

Principle of annotation:

See how annotations are supported under Java's larger system. Or return to custom annotations in the example above, for annotations Test, as follows, if to annotation of AnnotationTest class, the runtime can be AnnotationTest. class. getAnnotation (Test. class) to obtain annotations declared value, can be seen from the above sentence, it is get out Test annotate the class structure, so it must be sometime in the annotations are added to class structure.


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

From java source to class bytecode is done by the compiler, the compiler will to parse and generate class java source files, and annotation is at compile time by the compiler, the compiler will handle and attached to class structure of annotation symbols, according to the jvm specification, class the format of the file structure is strict and orderly, can only 1 additional information to save class structure is the way to class attributes attribute of structure. We know that for classes, fields, methods, and in the class structure has its own specific table structure, and each has its own properties, and for comments, the scope of the role can also be different, can effect on the class, also can effect on the field, or method, then the compiler will correspond to the annotation information stored on classes, fields, methods, their properties.

After our AnnotationTest class is compiled, the corresponding AnnotationTest.class file will contain an RuntimeVisibleAnnotations property. Since the annotation is applied to the class, this property is added to the class's property set. The key value pair for Test annotation value=test will be recorded. When JVM loads the AnnotationTest.class file bytecode, it will save the RuntimeVisibleAnnotations attribute value to AnnotationTest's Class object, so it can get the Test annotation object through AnnotationTest.class.getAnnotation (Test.class), and then get the Test attribute value through Test annotation object.

There may be some questions here, what is the Test annotation object? In fact, after the annotation is compiled, it is essentially an interface that inherits Annotation interface, so @Test is actually "public interface Test extends Annotation". When we call AnnotationTest.class.getAnnotation (Test.class), JDK will generate an object that implements Test interface through dynamic proxy, and set RuntimeVisibleAnnotations attribute value into this object. This object is known as the Test annotation object, and its value() method is used to get the annotation value.

The entire process of implementing the Java annotation mechanism is shown above, and its implementation requires the compiler to work with JVM1.

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


Related articles: