Java uses reflection to get comments on classes, methods, properties

Source: Internet
Author: User

Sometimes we want to use reflection to get a comment on a class, a comment on a method, a comment on a property.

The following is a simple example. It includes the three points mentioned above.

Package Com.mine.practice.reflectfield;import Java.lang.annotation.annotation;import Java.lang.reflect.Field; Import Java.lang.reflect.method;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.xmlaccessortype;import Javax.xml.bind.annotation.xmlattribute;import Javax.xml.bind.annotation.xmlelement;import javax.xml.bind.annotation.xmlrootelement;/** * * @author 2014-11-10 PM 01 : 54:48 * @version V1.0 */@XmlRootElement (name= "user") @XmlAccessorType (Xmlaccesstype.field) public class User {private S Tring pwd; @XmlElement (name = "ID") private int ID, @XmlAttribute @xmlelementprivate String name;/*** * 1, gets a comment of the specified type on the property * 2, Gets a comment of the specified type on the property specified method * 3, gets all comments on the property * 4, gets all comments on the class * 5, gets all comments on the method * @author 2014-11-10 pm 02:18:24 * @param args * * @Supp Resswarnings ("Rawtypes") public static void main (string[] args) {field[] fields = User.class.getDeclaredFields (); Field f:fields) {String filedname = F.getname (); System.out.println ("Property name:" "+filedname+" "),//1, gets the specified type of note on the propertyrelease annotation annotation = F.getannotation (xmlelement.class);//There is a comment of that type exists if (annotation!=null) {//cast to the corresponding comment XmlElement XmlElement = (xmlElement) ANNOTATION;//3, gets the specified method of the annotation of the specified type on the property//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+ "annotation is the defaults:" +xmlelement.name ());} else {System.out.println ("+filedname+" annotation uses a name that is a custom value: "+xmlelement.name ());}} 2. Get all annotations on attributes annotation[] allannotations = F.getannotations (); for (Annotation an:allannotations) {Class Annotationtype = An.annotationtype (); System.out.println (the annotation type for "attribute" "+filedname+" is: "+ Annotationtype);} System.out.println ("----------Gorgeous split-line--------------");} 4. Get all comments on the class annotation[] classannotation = User.class.getAnnotations (); for (Annotation cannotation:classannotation ) {Class Annotationtype = Cannotation.annotationtype (); System.out.println ("Comments on the user class are:" +annotationtype);} System.out.println ("----------Gorgeous split Line--------------");//5, get all comments on method Method;try {method = User.class.getMethod ("SetpWD ", String.class);  annotation[] methodannotations = Method.getannotations (); for (Annotation me:methodannotations) {Class Annotationtype = Me.annotationtype (); System.out.println ("Comments on the SetPwd method are:" + Annotationtype);}} catch (SecurityException e) {e.printstacktrace ();} catch (Nosuchmethodexception e) {e.printstacktrace ()}} @XmlElementpublic void SetPwd (String pwd) {this.pwd = pwd;} Public String getpwd () {return pwd;}}


The result of the operation is as follows

Property name: "pwd"----------Gorgeous split Line--------------property name: "ID" property "id" comment using the name is a custom value: The comment type for id attribute "ID" is: interface Javax.xml.bind.annotation.XmlElement----------Gorgeous Split line--------------property name: The name of the "name" property "name" comment uses the default value: # #default属性 The comment types for "name" are: interface Javax.xml.bind.annotation.XmlAttribute the comment type for the property "name" is: interface Javax.xml.bind.annotation.XmlElement----------Gorgeous Split line--------------comments on the user class are: interface The annotations on the Javax.xml.bind.annotation.XmlAccessorTypeUser class are: interface Javax.xml.bind.annotation.XmlRootElement----------Gorgeous Split line--------------comments on the SetPwd method are: interface Javax.xml.bind.annotation.XmlElement



Java uses reflection to get comments on classes, methods, properties

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.