In-depth understanding of Java: Annotations (Annotation)--annotations processor

Source: Internet
Author: User
Tags class definition

In-depth understanding of Java: Annotations (Annotation)--Note Processors If there is no method or work to read the annotations, then the 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 definition Constructor: Constructor definition Field: The tired member variable defines method: The methods of the class define the package: class's bundle definition Java.lang.reflect package contains some tool classes that implement reflection functionality, 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 program element, or null if the type annotation does not exist.  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, returns True if present, or false otherwise. 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: Copy Code/*********** Annotation Declaration ***************//** * Fruit Name Note * @author Peida * * * *@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interfaceFruitname {String value () default "";} /** * Fruit Color Annotations * @author Peida * * */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interfaceFruitcolor {/** * Color enumeration * @author Peida * */public enumcolor{Bule,red,green}; /** * Color Properties * @return */Color Fruitcolor () defaultColor.green;} /** * Fruit Provider Notes * @author Peida * * * *@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interfaceFruitprovider {/** * vendor number * @return */public int ID () default-1; /** * Supplier Name * @return */public String name () default ""; /** * Supplier Address * @return */public String 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= "No. 89th, Yan an LU, Xi ' an city, 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 void Getfruitinfo (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= "supplier Number:" +fruitprovider.id () + "supplier Name:" + Fruitprovider.name () + "supplier Address:" +  fruitprovider.address (); System.out.println (Strfruitprovicer); }}}}/*********** output ***************/public class  Fruitrun {/** * @param args */public static void  Main (Str Ing[] args) {fruitinfoutil.getfruitinfo (Apple.class );}} ====================================  fruit Name: Apple fruit color: Red Vendor ID: 1 Supplier Name: Shaanxi Red Fuji Group supplier address: Shaanxi Province, Xi ' an city, Yan an LU 89th, Red Fuji building  

In-depth understanding of Java: Annotations (Annotation)--annotations processor

Related Article

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.