If there is no method or work to read the annotations, then annotations are no more useful than annotations. An important part of the process of using annotations is to create them using the annotation processor. Java SE5 extends the reflection mechanism API to help programmers quickly construct custom annotation processors.
Note Processor Class library (java.lang.reflect.AnnotatedElement):
Java uses the annotation interface to represent annotations in front of the program element, which is the parent interface for all annotation types. In addition, Java has added a annotatedelement interface under the Java.lang.reflect package, which represents a program element that can accept annotations in the program, which has the following implementation classes:
Class: Classes Definition
Constructor: Constructor definition
Field: Tired member variable definition
Method: Methods Definition of Class
Package: Packages definition for Class
The Java.lang.reflect package consists of a number of tool classes that implement the reflection function, and in fact, the Java.lang.reflect package provides the ability to read runtime annotation information with all the provided reflection APIs. When a annotation type is defined as a run-time annotation, the annotation is visible to the runtime, and the annotation stored in the class file is read by the virtual machine when the class file is loaded.
The Annotatedelement interface is the parent interface for all program elements (class, method, and constructor), so the program obtains the Annotatedelement object for a class by reflection. The program can call the following four methods of the object to access the annotation information:
Method 1:<t extends Annotation> T getannotation (class<t> annotationclass): Returns an annotation of the specified type that exists on the modified program element, if the type annotation does not exist, Then 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 the program element contains annotations of the specified type, and returns True if present. Otherwise, false is returned.
Method 4:annotation[] Getdeclaredannotations (): Returns all annotations 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 can arbitrarily modify the returned array, which does not have any effect on the array returned by other callers.
A simple annotation Processor:
/*********** Annotation Declaration ***************//*** Fruit Name notes *@authorPeida **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented Public@InterfaceFruitname {String value ()default"";}/*** Fruit Color annotations *@authorPeida **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented Public@InterfaceFruitcolor {/*** Color Enumeration *@authorPeida **/ Public enumcolor{Bule,red,green}; /*** Color Properties *@return */Color Fruitcolor ()defaultColor.green;}/*** Notes for fruit suppliers *@authorPeida **/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented Public@InterfaceFruitprovider {/*** Supplier Number *@return */ Public intID ()default-1; /*** Supplier Name *@return */ PublicString name ()default""; /*** Supplier Address *@return */ PublicString address ()default"";}/*********** annotations using ***************/ Public classApple {@FruitName ("Apple") PrivateString Applename; @FruitColor (Fruitcolor=color.red)PrivateString AppleColor; @FruitProvider (ID=1,name= "Shaanxi Red Fuji Group", address= "Red Fuji Building, No. 89th Yan an LU, Xi ' an, Shaanxi province") PrivateString Appleprovider; Public voidSetapplecolor (String applecolor) { This. AppleColor =AppleColor; } PublicString Getapplecolor () {returnAppleColor; } Public voidsetapplename (String applename) { This. Applename =Applename; } PublicString Getapplename () {returnApplename; } Public voidSetappleprovider (String appleprovider) { This. Appleprovider =Appleprovider; } PublicString Getappleprovider () {returnAppleprovider; } Public voidDisplayName () {System.out.println ("The name of the fruit is: Apple"); }}/*********** Annotation Processor ***************/ Public classFruitinfoutil { Public Static voidGetfruitinfo (class<?>clazz) {String Strfruitname= "Fruit Name:"; String Strfruitcolor= "Fruit Color:"; String Strfruitprovicer= "Supplier Information:"; Field[] Fields=Clazz.getdeclaredfields (); for(Field field:fields) {if(Field.isannotationpresent (Fruitname.class) {fruitname Fruitname= (fruitname) field.getannotation (fruitname.class); Strfruitname=strfruitname+Fruitname.value (); System.out.println (Strfruitname); } Else if(Field.isannotationpresent (Fruitcolor.class) {Fruitcolor Fruitcolor= (Fruitcolor) field.getannotation (Fruitcolor.class); Strfruitcolor=strfruitcolor+Fruitcolor.fruitcolor (). toString (); System.out.println (Strfruitcolor); } Else if(Field.isannotationpresent (Fruitprovider.class) {Fruitprovider Fruitprovider= (Fruitprovider) field.getannotation (Fruitprovider.class); Strfruitprovicer= "Vendor Number:" +fruitprovider.id () + "supplier Name:" +fruitprovider.name () + "supplier Address:" +fruitprovider.address (); System.out.println (Strfruitprovicer); } } }}/*********** Output Results ***************/ Public classFruitrun {/** * @paramargs*/ Public Static voidMain (string[] args) {fruitinfoutil.getfruitinfo (Apple.class); }}====================================Fruit Name: Apple fruit Color: RED supplier Number:1 Supplier Name: Shaanxi Red Fuji Group supplier Address: Xi ' an city, Shaanxi Province, No. 89th Yan an LU Red Fuji building
Class<t> when instantiating, T should be replaced by a specific class
Class<?> is it a wildcard generic,? can represent any type
The basics of Java annotations (see the map below) are basically all over again, the next we can synthesize the application and further deepen the understanding and application of the various knowledge points of annotations by designing a simple ORM framework based on annotations.
An in-depth understanding of Java annotations (Annotation): Annotations Processor (3)