PackageCom.mine.practice.reflectfield;Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;ImportJavax.xml.bind.annotation.XmlAttribute;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement;/** * * @author2014-11-10 pm 01:54:48 *@versionV1.0*/@XmlRootElement (Name= "User") @XmlAccessorType (Xmlaccesstype.field) Public classUser {PrivateString pwd; @XmlElement (Name= "ID") Private intID; @XmlAttribute @XmlElementPrivateString name; /*** 1, get comment of the specified type on the attribute * 2, get a comment of the specified type on the property of the specified method * 3, get all comments on the property * 4, get all comments on the class * 5, get all comments on the method *@author2014-11-10 pm 02:18:24 *@paramargs*/@SuppressWarnings ("Rawtypes") Public Static voidMain (string[] args) {field[] fields= User.class. Getdeclaredfields (); for(Field f:fields) {String filedname=F.getname (); System.out.println ("Property name:" "+filedname+" ""); //1. Gets a comment of the specified type on the propertyAnnotation Annotation = f.getannotation (XmlElement.class); //There are annotations of that type exist if(annotation!=NULL) { //force conversion to the appropriate commentXmlElement XmlElement =(XmlElement) annotation; //3. Gets the specified method of the annotation of the specified type on the property//specific is not the default value can go to see the source code if(Xmlelement.name (). Equals ("# #default") {System.out.println (The name used by the attribute "+filedname+" comment is the default value: "+xmlelement.name ()); }Else{System.out.println (The "attribute" "+filedname+" comment uses a name that is a custom value: "+xmlelement.name ()); } } //2. Get all comments on attributesannotation[] Allannotations =f.getannotations (); for(Annotation an:allannotations) {Class Annotationtype=An.annotationtype (); System.out.println (The annotation types for attribute "+filedname+" are: "+Annotationtype); } System.out.println ("----------Gorgeous split-line--------------"); } //4. Get all comments on a classannotation[] Classannotation = User.class. Getannotations (); for(Annotation cannotation:classannotation) {Class Annotationtype=Cannotation.annotationtype (); System.out.println (The comments on the user class are: "+Annotationtype); } System.out.println ("----------Gorgeous split-line--------------"); //5. Get all comments on the methodmethod method; Try{method= User.class. GetMethod ("SetPwd", String.class); Annotation[] Methodannotations=method.getannotations (); for(Annotation me:methodannotations) {Class Annotationtype=Me.annotationtype (); System.out.println (The notes on the SetPwd method are: "+Annotationtype); } } Catch(SecurityException e) {e.printstacktrace (); } Catch(nosuchmethodexception e) {e.printstacktrace (); }} @XmlElement Public voidsetpwd (String pwd) { This. PWD =pwd; } PublicString getpwd () {returnpwd; The run result looks like the property name: "PWD"----------Gorgeous split-line--------------Property name: "id" attribute "id" comment using the name is a custom value: The comment type for id attribute "id" is:Interfacejavax.xml.bind.annotation.XmlElement----------Gorgeous split-line--------------Property Name: "Name" property "name" comment using the name is the default value: # #defaultthe comment types for the property "name" are:Interfacethe comment types for the Javax.xml.bind.annotation.XmlAttribute property "name" are:Interfacejavax.xml.bind.annotation.XmlElement----------Gorgeous split-line--------------the annotations on the user class are:Interfacethe annotations on the Javax.xml.bind.annotation.XmlAccessorTypeUser class are:Interfacejavax.xml.bind.annotation.XmlRootElement----------Gorgeous split-line--------------the annotations on the SetPwd method are:Interfacejavax.xml.bind.annotation.XmlElement Public Static voidMain (string[] args) {field[] fields=admevaluationitementity.class. Getdeclaredfields (); for(Field field:fields) {//Get Property nameString name=Field.getname (); System.out.print ("Property name:" +name+ ""); //gets the datatype of the attribute column;Column col=field.getannotation (column.class); DataType D=Col.datatype (); System.out.print ("Data type:" +d.name () + ""); //get the name of column;String tname=Col.name (); System.out.print ("Column-name:" +tname+ ""); //Get ID CommentID id=field.getannotation (ID.class); if(id!=NULL) {System.out.println ("ID:" +field.getname ()); }Else{System.out.println (); } } //Get table nameTable t=admevaluationitementity.class. getannotation (Table.class); String N=T.name (); System.out.println ("Table name:" +N); Property name: ID data type: STRING column-Name:id Id:id Property name: ItemType data type: STRING column-Name:itemtype Property Name: ItemName data type: STRING column-Name:itemname Property Name: Description data type: STRING column-name:description Property Name: Weight data type: DECIMAL column-Name:weight Property Name: Typeindex data type: DECIMAL column-name:typeindex table name: Adm_evaluationitem
Java gets annotations through reflection