java 反射工具包reflections

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


官方文档:https://github.com/ronmamo/reflections




 

posted @ 2017-12-04 11:45  超人吃鸡蛋  阅读(10100)  评论(1编辑  收藏  举报