Deep analysis of java annotations. Let you take a quick step!
java meta-annotation
Meta-annotations are responsible for annotating other annotations. Java 5.0 defines four standard meta-annotations. Includes: @Target, @Documented, @Retention, @Inherit.
@ Target annotation: It is used to describe the types of annotations that can be modified. The types that can be modified are TYPE (class, interface, enumeration), FIELD, METHOD (method declaration), PARAMETER (parameter), CONSTRUCTOR (construction method), LOCAL_VARIABLE (local variable), ANNOTATION_TYPE (annotation type declaration), PACKAGE (package).
The code is as follows:
@ Retention annotation: Used to represent the action phase of the annotation. Its parameters can take the values in RetentionPolicy. Such as:
SOURCE: Acting on the source code, the compilation phase is removed.
CLASS: It can be compiled into a Class file, but the virtual machine ignores it.
RUNTIME: It can be compiled into a Class file and used by the virtual machine at runtime.
The code is:
@ Documented annotation: Used to generate API documents so that the annotation can be presented on the API using the annotation.
@ Inherit annotation: A tag annotation that describes how an annotation can be inherited. If a class uses (@Interit annotation), then the annotation can be inherited by a subclass of that class.
If you use reflection to find a class annotated with the @Inherit annotation, the reflection code checks the class and its parent class until it finds the top-level class annotated with the annotation.
The code is as follows:
Custom Annotations
According to the meta-annotations above, we can define our own annotations. When defining annotations, we can specify different parameters and default values of the parameters.
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface PersonAnno {
String name() default ""; int age() default 0;
}123456789123456789
Here we define a comment, which adds two parameters, name and age, and sets the default values.
According to the annotations defined above, they are reserved for runtime and tagged on methods.
public class Person {
@PersonAnno(name = "wy", age = 20) public void getPerson() {
}
}1234512345
The following is for this annotationtest.
public class MainTest { public static void main(String[] args) throws Exception {
Person person = new Person();
Class pers = person.getClass(); // Reflective Acquisition Method
Method method= pers.getDeclaredMethod("getPerson"); // Get annotations
PersonAnno ana = method.getAnnotation(PersonAnno.class);
System.out.println(ana.name() + ":" + ana.age());
}
}12345678910111213141234567891011121314
The output results are as follows:
wy:2011
Essence of annotation
Essentially, annotations are compiled into inherited interfaces.
Decompilation of PersonAnno.class above shows the following code:
From the decompiled code above, you can see that annotations are actually compiled into interfaces.
The Annotation interface, which is inherited by all annotation types, has been described here./** * The common interface extended by all annotation types. Note that an * interface that manually extends this one does <i>not</i> define * an annotation type. Also note that this interface does not itself * define an annotation type. * * More information about annotation types can be found in section 9.6 of * <cite>The Java™ Language Specification</cite>. * * @author Josh Bloch * @since 1.5 */public interface Annotation {}1234567891011121312345678910111213