1. 程式人生 > >Java 使用反射獲取類、方法、屬性上的註解

Java 使用反射獲取類、方法、屬性上的註解

有的時候我們想使用反射獲取某個類的註解、方法上的註解、屬性上的註解。

下面是一個簡單的例子。裡面包括了上面提到的三個點。

01.package com.mine.practice.reflectfield;  
02.  
03.import java.lang.annotation.Annotation;  
04.import java.lang.reflect.Field;  
05.import java.lang.reflect.Method;  
06.  
07.import javax.xml.bind.annotation.XmlAccessType;  
08.import javax.xml.bind.annotation.XmlAccessorType;  
09.import javax.xml.bind.annotation.XmlAttribute;  
10.import javax.xml.bind.annotation.XmlElement;  
11.import javax.xml.bind.annotation.XmlRootElement;  
12.  
13./** 
14.  *  
15.  * @author 2014-11-10 下午01:54:48  
16.  * @version V1.0   
17. */  
18.@XmlRootElement(name="user")  
19.@XmlAccessorType(XmlAccessType.FIELD)  
20.public class User {  
21.  
22.    private String pwd;  
23.  
24.    @XmlElement(name = "ID")  
25.    private int id;  
26.      
27.    @XmlAttribute  
28.    @XmlElement  
29.    private String name;  
30.      
31.    /*** 
32.     *  1、獲取屬性上的指定型別的註解 
33.     *  2、獲取屬性上的指定型別的註解的指定方法 
34.     *  3、獲取屬性上的所有註解 
35.     *  4、獲取類上的所有註解 
36.     *  5、獲取方法上的所有註解 
37.      * @author 2014-11-10 下午02:18:24 
38.      * @param args  
39.     */  
40.    @SuppressWarnings("rawtypes")  
41.    public static void main(String[] args) {  
42.          
43.        Field[] fields =  User.class.getDeclaredFields();  
44.          
45.        for(Field f : fields){  
46.            String filedName = f.getName();  
47.            System.out.println("屬性名稱:【"+filedName+"】");  
48.  
49.            //1、獲取屬性上的指定型別的註解  
50.            Annotation annotation = f.getAnnotation(XmlElement.class);  
51.              
52.            //有該型別的註解存在  
53.            if (annotation!=null) {  
54.                //強制轉化為相應的註解      
55.                XmlElement xmlElement = (XmlElement)annotation;  
56.                //3、獲取屬性上的指定型別的註解的指定方法  
57.                //具體是不是預設值可以去檢視原始碼  
58.                if (xmlElement.name().equals("##default")) {  
59.                    System.out.println("屬性【"+filedName+"】註解使用的name是預設值: "+xmlElement.name());  
60.                }else {  
61.                    System.out.println("屬性【"+filedName+"】註解使用的name是自定義的值: "+xmlElement.name());  
62.                }  
63.            }  
64.  
65.            //2、獲取屬性上的所有註解  
66.            Annotation[] allAnnotations = f.getAnnotations();  
67.              
68.            for(Annotation an : allAnnotations){  
69.                  
70.                Class annotationType = an.annotationType();  
71.                  
72.                System.out.println("屬性【"+filedName+"】的註解型別有: " + annotationType);  
73.            }  
74.            System.out.println("----------華麗的分割線--------------");  
75.        }  
76.          
77.        //4、獲取類上的所有註解  
78.        Annotation[] classAnnotation = User.class.getAnnotations();  
79.          
80.        for(Annotation cAnnotation : classAnnotation){  
81.            Class annotationType =  cAnnotation.annotationType();  
82.            System.out.println("User類上的註解有: " +annotationType);  
83.        }  
84.          
85.        System.out.println("----------華麗的分割線--------------");  
86.          
87.        // 5、獲取方法上的所有註解  
88.        Method method;  
89.        try {  
90.            method = User.class.getMethod("setPwd",String.class);  
91.              
92.            Annotation[] methodAnnotations = method.getAnnotations();  
93.  
94.            for(Annotation me : methodAnnotations){  
95.                Class annotationType =  me.annotationType();  
96.                System.out.println("setPwd方法上的註解有: " + annotationType);  
97.            }  
98.        } catch (SecurityException e) {  
99.            e.printStackTrace();  
100.        } catch (NoSuchMethodException e) {  
101.            e.printStackTrace();  
102.        }  
103.    }  
104.  
105.    @XmlElement  
106.    public void setPwd(String pwd) {  
107.        this.pwd = pwd;  
108.    }  
109.  
110.    public String getPwd() {  
111.        return pwd;  
112.    }  
113.}  
執行結果如下所示
01.屬性名稱:【pwd】  
02.----------華麗的分割線--------------  
03.屬性名稱:【id】  
04.屬性【id】註解使用的name是自定義的值: ID  
05.屬性【id】的註解型別有: interface javax.xml.bind.annotation.XmlElement  
06.----------華麗的分割線--------------  
07.屬性名稱:【name】  
08.屬性【name】註解使用的name是預設值: ##default  
09.屬性【name】的註解型別有: interface javax.xml.bind.annotation.XmlAttribute  
10.屬性【name】的註解型別有: interface javax.xml.bind.annotation.XmlElement  
11.----------華麗的分割線--------------  
12.User類上的註解有: interface javax.xml.bind.annotation.XmlAccessorType  
13.User類上的註解有: interface javax.xml.bind.annotation.XmlRootElement  
14.----------華麗的分割線--------------  
15.setPwd方法上的註解有: interface javax.xml.bind.annotation.XmlElement