java reflections_java 非常好用的反射框架Reflections

format,png

背景

最近在设计和开发部门的基础架构,需要利用反射技术找到classpath目录下所有包含指定注解的类,然后根据注解配置完成指定的功能。

最初是想自己写代码来实现这些功能,边查资料边写,整了大半天,写出来的效果自己都不太满意。一方面是代码多,不好维护;另一方面是性能不太好。不过,在查资料的过程中,我发现了一个非常好用的反射框架:reflections。

reflections简单好用,性能也不错,很快就完成了我想要的功能,就是这么优秀!

简介

Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据。

使用Reflections可以很轻松的获取以下元数据信息:

获取某个类型的全部子类

只要类型、构造器、方法,字段上带有特定注解,便能获取带有这个注解的全部信息(类型、构造器、方法,字段)

获取所有能匹配某个正则表达式的资源

获取所有带有特定签名的方法,包括参数,参数注解,返回类型

获取所有方法的名字

获取代码里所有字段、方法名、构造器的使用

Maven依赖

在pom.xml中添加reflections的依赖:

dependency>    org.reflectionsgroupId>    reflectionsartifactId>    0.9.11version>dependency>

// 实例化Reflections,并指定要扫描的包名Reflections reflections = new Reflections("my.project");// 获取某个类的所有子类Set> subTypes = reflections.getSubTypesOf(SomeType.class);// 获取包含某个注解的所有类Set> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

使用

//scan urls that contain 'my.package', include inputs starting with 'my.package', use the default scannersReflections reflections = new Reflections("my.package");//or using ConfigurationBuildernew Reflections(new ConfigurationBuilder()     .setUrls(ClasspathHelper.forPackage("my.project.prefix"))     .setScanners(new SubTypesScanner(),                  new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...),     .filterInputsBy(new FilterBuilder().includePackage("my.project.prefix"))     ...);

扫描子类

Set> modules =    reflections.getSubTypesOf(com.google.inject.Module.class);

扫描注解

//TypeAnnotationsScannerSet> singletons =    reflections.getTypesAnnotatedWith(javax.inject.Singleton.class);

扫描资源

//ResourcesScannerSet properties =    reflections.getResources(Pattern.compile(".*\\.properties"));

扫描方法注解

//MethodAnnotationsScannerSetresources =    reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);Set injectables =    reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class);

扫描字段注解

//FieldAnnotationsScannerSet ids =    reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);

扫描方法参数

//MethodParameterScannerSetsomeMethods =    reflections.getMethodsMatchParams(long.class, int.class);SetvoidMethods =    reflections.getMethodsReturn(void.class);SetpathParamMethods =    reflections.getMethodsWithAnyParamAnnotated(PathParam.class);

扫描方法参数名

//MethodParameterNamesScannerListparameterNames =    reflections.getMethodParamNames(Method.class)

扫描方法调用情况

//MemberUsageScannerSet usages =    reflections.getMethodUsages(Method.class)

出处:cnblogs.com/panchanggui/p/13188345.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值