Java: annotation-annotation processing tool

Source: Internet
Author: User

If there is no method or work to read the annotation, the annotation will not be more useful than the annotation. An important part of the annotation process is to create an annotation processor. Java se5 extends the reflection API to helpProgramQuickly construct a custom annotation processor.

Annotation processor class library (Java. Lang. Reflect. annotatedelement ):

Java uses the annotation interface to represent the annotation before the program element. This interface is the parent interface of all annotation types. In addition, Java added the annotatedelement interface under the java. Lang. Reflect package. This interface represents the program elements that can be annotated in the program. This interface mainly has the following implementation classes:

Class: Class Definition
Constructor: constructor Definition
Field: Tired member variable definition
Method: class method definition
Package: Class package Definition

The Java. Lang. Reflect package mainly contains some tool classes for implementing the reflection function. In fact, all the reflection APIs provided by the java. Lang. Reflect package expand the ability to read annotation information during runtime. After an annotation type is defined as a runtime annotation, the annotation can be visible at runtime. When the class file is loaded, the annotation stored in the class file will be read by the virtual machine.
The annotatedelement interface is the parent interface of all program elements (class, method, and constructor). Therefore, after the program obtains the annotatedelement object of a class through 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 the annotation of the specified type on the modified program element. If the annotation of this type does not exist, returns null.
Method 2: annotation [] getannotations (): returns all annotations on the program element.
Method 3: Boolean is annotationpresent (class <? Extends annotation> annotationclass): determines whether the program element contains the specified type of Annotation. If the annotation exists, true is returned; otherwise, false is returned.
Method 4: annotation [] getdeclaredannotations (): returns all comments directly on this element. Unlike other methods in this interface, this method ignores the inherited comments. (If no annotation exists directly on this element, an array with zero length is returned .) The caller of this method can modify the returned array at will; this will not affect the array returned by other callers.

A simple annotation processor:

 /**  **************  */  /**  * Fruit name annotation *  @ Author  Peida *  */ @ Target (elementtype. Field) @ retention (retentionpolicy. runtime) @ incluented  Public @ Interface  Fruitname {string value ()  Default "" ;}  /**  * Fruit Color annotation *  @ Author  Peida *  */  @ Target (elementtype. Field) @ retention (retentionpolicy. runtime) @ incluented  Public @Interface  Fruitcolor {  /**  * Color enumeration *  @ Author  Peida *  */      Public   Enum  Color {Bule, red, green };  /**  * Color attribute *  @ Return       */  Color fruitcolor ()  Default Color. Green ;}  /**  * Fruit supplier Notes *  @ Author  Peida *  */  @ Target (elementtype. Field) @ retention (retentionpolicy. runtime) @ incluented  Public @ Interface  Fruitprovider {  /**  * Supplier ID *  @ Return       */      Public  Int ID () Default -1 ;  /**  * Supplier name *  @ Return       */      Public String name () Default "" ;  /**  * Supplier address *  @ Return       */      Public String address ()Default "" ;}  /**  **************  */  Public   Class  Apple {@ fruitname ( "Apple" )  Private  String applename; @ fruitcolor (fruitcolor = Color. Red)  Private  String applecolor; @ fruitprovider (ID = 1, name = "Shaanxi hongfuji group", address = "hongfuji building, No. 89, yan'an Road, Xi'an, Shaanxi Province" )  Private  String appleprovider;  Public   Void  Setapplecolor (string applecolor ){  This . Applecolor = Applecolor ;}  Public  String getapplecolor (){  Return  Applecolor ;}  Public  Void  Setapplename (string applename ){  This . Applename = Applename ;}  Public  String getapplename (){  Return  Applename ;}  Public   Void  Setappleprovider (string appleprovider ){  This . Appleprovider = Appleprovider ;} Public  String getappleprovider (){  Return  Appleprovider ;}  Public   Void  Displayname () {system. Out. println ( "Fruit name: Apple" );}}  /**  * ******** Annotation processor **************  */  Public   Class  Fruitinfoutil {  Public  Static   Void Getfruitinfo (class <?> Clazz) {string strfruitname = "Fruit name :" ; String strfruitcolor = "Fruit Color :" ; String strfruitprovicer = "Supplier information :" ; Field [] fields = Clazz. getdeclaredfields ();  For  (Field: fields ){  If (Field. isannotationpresent (fruitname. Class )) {Fruitname = (Fruitname) field. getannotation (fruitname. Class  ); Strfruitname = Strfruitname + Fruitname. Value (); system. Out. println (strfruitname );}Else   If (Field. isannotationpresent (fruitcolor. Class ) ) {Fruitcolor = (Fruitcolor) field. getannotation (fruitcolor. Class  ); Strfruitcolor = Strfruitcolor +Fruitcolor. fruitcolor (). tostring (); system. Out. println (strfruitcolor );}Else  If (Field. isannotationpresent (fruitprovider. Class ) ) {Fruitprovider = (Fruitprovider) field. getannotation (fruitprovider. Class  ); Strfruitprovicer = "Supplier No.:" + fruitprovider. ID () + "supplier name:" + fruitprovider. Name () + "supplier address:" + Fruitprovider. Address (); system. Out. println (strfruitprovicer );}}}}  /** **************  */  Public   Class  Fruitrun {  /**  *  @ Param  ARGs  */      Public   Static   Void  Main (string [] ARGs) {fruitinfoutil. getfruitinfo (Apple.  Class );}} ========================================== Fruit name: Apple Fruit Color: Red supplier No: 1 Supplier name: Shaanxi hongfuji group supplier address: hongfuji building, No. 89, yan'an Road, Xi'an, Shaanxi Province

The basic knowledge points of Java annotations (see the graph below) are basically all over again. In the next article, we will design a simple annotation-based ORM framework, to further deepen the understanding and application of each annotation knowledge point.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.