使用反射来提取注解annotation
2016-06-27 16:10
246 查看
package com.samsung.TextFileTest; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; public class AnotitaionDemo { public static void main(String[] args) throws Exception { Class<?> classtype=Class.forName("com.samsung.TextFileTest.AnotitionTest"); boolean annotationPresent = classtype.isAnnotationPresent(Description.class); if (annotationPresent) { Description annotation = classtype.getAnnotation(Description.class); System.out.println(annotation.value()); } Method[] declaredMethods = classtype.getDeclaredMethods(); for (Method method : declaredMethods) { if(method.isAnnotationPresent(Author.class)){ Author author=(Author)method.getAnnotation(Author.class); System.out.println("name="+author.name()+",group="+author.group()); } } } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented @interface Author { String name(); String group(); } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @interface Description { String value(); } @Description("这是一个AnotitionTest类") class AnotitionTest { @Author(name = "zhanghao", group = "SW") public void test() { System.out.println("this is a test"); } }
这是一个AnotitionTest类 name=zhanghao,group=SW
相关文章推荐
- HTTP接口测试还可以这么玩
- 1、TCP/IP协议簇
- 请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同,False代
- Labview数据库简单操作
- 组动画
- shell脚本实例
- 多表更新时碰到的 ERROR 1292 (22007)隐式转换错误
- 实现定时和调度的思路
- 微信Token失败
- coredump详解
- Centos 使用YUM安装MariaDB
- Android Studio设置默认的签名文件
- caffe|Fine-tuning for driver
- C++ 信号处理
- leetcode 204 Count Primes C++
- java学习:eclipse + Weblogic 集成开发环境搭建
- MTK selinux编绎及快速验证问题的方法
- linux之间传输文件
- ios开发——日常之三种方式使得iOS应用能够在后台进行数据更新和下载
- PHP知识大全