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  



评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值