Annotations are no more useful than annotations if there is no method or work for reading annotations. In the process of using annotations, it is important to create a use of the annotation processor. The Java SE5 expands the reflection mechanism API to help programmers quickly construct custom annotation processors.
Annotation Processor Class Library (java.lang.reflect.AnnotatedElement):
Java uses the annotation interface to represent the annotations in front of the program element, which is the parent interface for all annotation types. In addition, Java in the Java.lang.reflect package of the new Annotatedelement interface, which represents the program can accept the annotation of the program elements, the interface is mainly the following implementation classes:
Class: Classes define
Constructor: constructor definition
Field: Tired member variable definition
Method: Methods definition for a class
Package: Package definition for Class
The Java.lang.reflect package contains a number of tool classes to implement the reflection function, in fact, the Java.lang.reflect package provides a reflection API that expands the ability to read run-time annotation information. When a annotation type is defined as a run-time annotation, the annotation can be visible at runtime, and the annotation that is stored in the class file when the class file is loaded is read by the virtual machine.
The Annotatedelement interface is the parent interface of all program elements (class, method, and constructor), so that after the program obtains the Annotatedelement object of a class by reflection, The program can invoke the following four methods of the object to access the annotation information:
Method 1:<t extends Annotation> T getannotation (class<t> annotationclass): Returns a specified type of annotation that exists on the modifier element, if the type annotation does not exist, NULL is returned.
Method 2:annotation[] Getannotations (): Returns all annotations that exist on the program element.
Method 3:boolean is annotationpresent (class<?extends annotation> annotationclass): Determines whether a specified type of annotation is included on the program element, and returns true if it exists. otherwise returns false.
Method 4:annotation[] Getdeclaredannotations (): Returns all comments that exist directly on this element. Unlike other methods in this interface, the method ignores inherited annotations. (if no comment exists directly on this element, an array of zero length is returned.) The caller of the method is free to modify the returned array, which does not have any effect on the array returned by the other caller.