Reflectios通过扫描classpath,索引元数据,允许在运行时查询这些元数据,也可以保存收集项目中多个模块的元数据信息。使用Reflectios可以查询以下元数据信息: 1)获得某个类型的所有子类型2)获得标记了某个注解的所有类型/成员变量,支持注解参数匹配。3)使用正则表达式获得所有匹配的资源文件4)获得所有特定签名(包括参数,参数注解,返回值)的方法
Reflectios依赖Google的Guava库和Javassist库。
Mave项目导入
<depedecy> <groupId>org.reflectios</groupId> <artifactId>reflectios</artifactId> <versio>0.9.10</versio></depedecy>通常用法:
Reflectios reflectios = ew Reflectios("my.project");Set<Class<? exteds SomeType>> subTypes = reflectios.getSubTypesOf(SomeType.class);Set<Class<?>> aotated = reflectios.getTypesAotatedWith(SomeAotatio.class);Reflectios初始化代码。
//sca urls that cotai 'my.package', iclude iputs startig with 'my.package', use the default scaersReflectios reflectios = ew Reflectios("my.package");//or usig CofiguratioBuilderew Reflectios(ew CofiguratioBuilder() .setUrls(ClasspathHelper.forPackage("my.project.prefix")) .setScaers(ew SubTypesScaer(), ew TypeAotatiosScaer().filterResultsBy(optioalFilter), ...), .filterIputsBy(ew FilterBuilder().icludePackage("my.project.prefix")) ...);以下是一些使用例子代码。
//SubTypesScaerSet<Class<? exteds Module>> modules = reflectios.getSubTypesOf(com.google.iject.Module.class);//TypeAotatiosScaer Set<Class<?>> sigletos = reflectios.getTypesAotatedWith(javax.iject.Sigleto.class);//ResourcesScaerSet<Strig> properties = reflectios.getResources(Patter.compile(".*\\.properties"));//MethodAotatiosScaerSet<Method> resources = reflectios.getMethodsAotatedWith(javax.ws.rs.Path.class);Set<Costructor> ijectables = reflectios.getCostructorsAotatedWith(javax.iject.Iject.class);//FieldAotatiosScaerSet<Field> ids = reflectios.getFieldsAotatedWith(javax.persistece.Id.class);//MethodParameterScaerSet<Method> someMethods = reflectios.getMethodsMatchParams(log.class, it.class);Set<Method> voidMethods = reflectios.getMethodsRetur(void.class);Set<Method> pathParamMethods = reflectios.getMethodsWithAyParamAotated(PathParam.class);//MethodParameterNamesScaerList<Strig> parameterNames = reflectios.getMethodParamNames(Method.class)//MemberUsageScaerSet<Member> usages = reflectios.getMethodUsages(Method.class)
评论