@[toc]
An annotation is the underlying implementation of getting the value of a property
The JVM generates proxy objects for annotations. The code for retrieving annotation information mentioned in the article in this sectionJavaweixin6.blog.csdn.net/article/det…Note that the annotation life cycle is set to RuntimeWhen the program runs, set the following JVM parameters and save the generated proxy object as a file-Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true
Only the values of member attributes are retrieved.After running the main method above, the generated code looks like thisIn the generated proxy objects, you can see that one of them implements the PersonInfoAnnotation annotation.This proxy object declares nine static methodsThese eight static methods correspond to the following, in addition to toString hashCode and so on, which are methods declared in annotations.
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
m6 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("language");
m5 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("name");
m2 = Class.forName("java.lang.Object").getMethod("toString");
m3 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("gender");
m4 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("age");
m7 = Class.forName("demo.annotation.PersonInfoAnnotation").getMethod("annotationType");
m0 = Class.forName("java.lang.Object").getMethod("hashCode");
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw newNoClassDefFoundError(var3.getMessage()); }}Copy the code
In the generated proxy object, each annotated member method will have a corresponding method in the proxy object, as shown in the following figurelanguage
Methods andname
Methods.Super is a Proxy object. As shown in the figure below In the proxy classsuper.h.invoke
H in is InvocationHandler.The invoke method of the InvocationHandler object is invoked.When the JVM starts, an extra parameter is added.-XX:+TraceClassLoading
Used to print out all loaded classes.In the console print information, you can see the AnnotationInvocationHandler class loading.AnnotationInvocationHandler class implements the InvocationHandler interface.When program is running, will perform AnnotationInvocationHandler invoke method in the class.The debug to run the program, the breakpoint to play in the invoke method AnnotationInvocationHandler classes, you can see memberValues, access to the values in the annotations.The essence of memberValues is the method name of a map, key annotation, and the value is the value assigned to the annotation.
How annotations work
- Assign values to attributes of annotations by means of key-value pairs.
As shown in the figure below, assignment is done by key-value pairs
- The compiler checks the scope of the annotations. Writes the annotation information to the element’s property sheet
- When the program runs, the JVM pulls out all the RUNTIME attributes and eventually stores them into the map.
- The JVM will create AnnotationInvocationHandler instance and pass step on the map
- The JVM will use the JDK dynamic proxy generated proxy class for annotations, and initialize AnnotationInvocationHandler
- Call the Invoke method, passing in the method name, and return the value of the attribute corresponding to the annotation.