You have been using annotations, but you know it's the principle of it to achieve?

Built-in Java annotations and a custom annotation we are more familiar with, and now look at the principles of notes realized, look at the following Java system is how to support the annotations.

Before discussing a look custom annotations example, to achieve such a custom annotation: injection by a certain string to the @Test, injecting a string to a method @TestMethod.

① Create a Test annotation statement on the role of class and reserved to run, the default is default.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
    String value() default "default";
}

② create TestMethod notes, declarations and reservations to act on the method runtime.

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestMethod {
    String value();
}

③ Test class, default and the output after running two strings tomcat-method, since no incoming @Test value, the output of the default value, and outputs @TestMethod injection string.

@Test()
public class AnnotationTest {
    @TestMethod("tomcat-method")
    public void test(){
    }
    public static void main(String[] args){
        Test t = AnnotationTest.class.getAnnotation(Test.class);
        System.out.println(t.value());
        TestMethod tm = null;
        try {
            tm = AnnotationTest.class.getDeclaredMethod("test",null).getAnnotation(TestMethod.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(tm.value());
    }
}

Test for annotation, to annotate AnnotationTest if the class can be acquired by the runtime AnnotationTest.class.getAnnotation (Test.class) annotation value declared, can be seen from the above sentence, which is acquired from the Test class structure annotated, so it must be at some point annotations are added to the class structure go.

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

From the source to the class java bytecode is done by the compiler, the compiler will parse source code and generates java class file, and the annotation is processed by the compiler at compile time, the compiler will process and attached to the annotation symbol class structure, according to jvm specification, class file structure is strict and orderly format, the only way of additional information to the class structure is saved to the attributes attribute class structure.

We know that for classes, fields, methods, in the structure of the class has its own specific table structure, and each has its own properties, and for the annotation, the range of action may be different, may act on the class, can also act on the field or method, then the compiler should be stored in the annotation information on classes, fields, methods, own properties.

After our AnnotationTest class is compiled, in the corresponding AnnotationTest.class file will contain a RuntimeVisibleAnnotations property, since this is a comment on the role of class, so this attribute is added to the set of attributes of the class.

That Test annotation of key-value pairs value = test will be recorded together. When loading AnnotationTest.class JVM byte code files will be saved to the property values ​​RuntimeVisibleAnnotations Class object AnnotationTest in, so he can get the annotation object via Test AnnotationTest.class.getAnnotation (Test.class), and further through the Test annotation object to get inside the property value Test.

Here may be in doubt, what Test annotation objects are?

In fact, the nature of the annotations are compiled is an inherited interfaces Annotation interface, so @Test fact, public interface Test extends Annotation, when we call by AnnotationTest.class.getAnnotation (Test.class), JDK will be achieved by generating a dynamic proxy Test interface of the object, and the object is set into this RuntimeVisibleAnnotations property value, the object is the Test annotation object, through its value () method can be acquired annotation value.

Java annotations implementation mechanism of the process as shown, with its implementation and requires compiler with JVM above.

Guess you like

Origin blog.51cto.com/14230003/2421308