利用反射获取类或者方法或者字段上的注解的值
2016-05-26 23:34
393 查看
从JDK1.5之后,注解在各大框架上得到了广泛的应用。下面这个例子中,你可以判断一个类或者方法或者字段上有没有注解,以及怎么获取上面的注解值。话不多说,代码如下:
AnnotationTest01.java
AnnotationTest04.java
AnnotationTest01.java
package com.zkn.newlearn.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD,ElementType.PARAMETER}) public @interface AnnotationTest01 { String color(); }AnnotationTest02.java
package com.zkn.newlearn.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface AnnotationTest02 { String getUserName(); }
AnnotationTest04.java
package com.zkn.newlearn.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.FIELD}) public @interface AnnotationTest04 { String getAddress(); }AnnotationTest03.java
package com.zkn.newlearn.annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 测试Annotation * @author zkn * */ @AnnotationTest02(getUserName="zhangsan") public class AnnotationTest03 { @AnnotationTest01(color="red") public static String testColor(String color){ System.out.println(color); return color; } @AnnotationTest04(getAddress="北京市海淀区") String address; public static void main(String[] args) { //获取方法上的注解值 Method[] methods = AnnotationTest03.class.getDeclaredMethods(); if(methods != null){ for(Method method : methods){ AnnotationTest01 annotation = method.getAnnotation(AnnotationTest01.class); if(annotation == null) continue; Method[] me = annotation.annotationType().getDeclaredMethods(); for(Method meth : me){ try { String color = (String) meth.invoke(annotation,null); System.out.println(color); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } //获取类上的注解值 AnnotationTest02 anno = AnnotationTest03.class.getAnnotation(AnnotationTest02.class); if(anno != null){ Method[] met = anno.annotationType().getDeclaredMethods(); for(Method me : met ){ if(!me.isAccessible()){ me.setAccessible(true); } try { System.out.println(me.invoke(anno, null)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } //获取字段上的注解值 AnnotationTest03 noon = new AnnotationTest03(); Field[] field = AnnotationTest03.class.getDeclaredFields(); if(field != null){ for(Field fie : field){ if(!fie.isAccessible()){ fie.setAccessible(true); } AnnotationTest04 annon = fie.getAnnotation(AnnotationTest04.class); Method[] meth = annon.annotationType().getDeclaredMethods(); for(Method me : meth){ if(!me.isAccessible()){ me.setAccessible(true); } try { //给字段重新赋值 fie.set(noon, me.invoke(annon, null)); System.out.println(fie.get(noon)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } }
相关文章推荐
- 猿题库面试总结
- 获取当前工程路径
- java上机作业_文件的复制
- android中string.xml中%1$s、%1$d等的用法
- 定制跳转界面
- SDAU练习三1024
- Linux使用Eclipse开发C/C++环境配置
- PB编写三层及三层树的函数
- Android常见的系统设置
- c++ 引用 5.26
- openjudge8471 切割回文
- 云计算的概念和价值(Xmind思维导图)
- 调查管理系统(5)---答案的存储
- 搭建git for windows服务器(100%可以成功)【转】
- innerHTML引起IE的内存泄漏
- List集合中list.remove()返回false原因
- mysql5.6 gtid复制
- 菜鸟网络
- 1、python文件读写练习
- 团队作业六