码迷,mamicode.com
首页 > 编程语言 > 详细

java 反射工具包reflections

时间:2017-12-04 13:13:58      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:color   添加   nbsp   image   ons   扫描   ann   sys   nss   

reflections 中包含很多的Scanner ,也就是扫描器,调用对应的方法时需要有配置对应的扫描器,不然程序会抛出异常.

扫描器结构:

技术分享图片

使用时,我们主要使用Reflections 这个类来调用.

技术分享图片

Reflections 默认配置了一部分扫描器,

技术分享图片

但是在实际使用时需要添加很多的扫描器,可以根据程序抛出的异常进行添加对应的扫描

使用方法:

Reflections reflections = new Reflections("cn.*", new MethodAnnotationsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner(), new MethodParameterNamesScanner());

指明扫描的包路径,并配置扫描器

1.获取所有带有action注解的类

Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Action.class);
        for (Class<?> action : classes) {
            RequestMapper request = action.getAnnotation(RequestMapper.class);
            System.out.println(action + "=RequestMapper==" + request.value());
        }

 

2.获取所有带有requestMapper注解的方法

Set<Method> methods = reflections.getMethodsAnnotatedWith(RequestMapper.class);

        for (Method method : methods) {
            System.out.println(method.getName() + "=methods==" + reflections.getMethodParamNames(method));
        }

获取某个方法的参数名称:(jdk里面没有对应的方法)

reflections.getMethodParamNames



 

java 反射工具包reflections

标签:color   添加   nbsp   image   ons   扫描   ann   sys   nss   

原文地址:http://www.cnblogs.com/gj1990/p/7976389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!