摘要
org.reflections是一个强大的反射工具,它能够反射扫描你的类路径,索引元数据,允许你在运行时查询它,并可以保存和收集项目中许多模块的信息。
org.reflections是一个强大的反射工具,它能够反射扫描你的类路径,索引元数据,允许你在运行时查询它,并可以保存和收集项目中许多模块的信息。
反射一站式服务对象
反射扫描你的类路径,索引元数据,允许你在运行时查询它,并可以保存和收集项目中许多模块的信息。
使用反射,您可以查询元数据,例如:
获取某种类型的所有子类型
获取所有类型/构造函数/方法/字段,并使用一些注释进行注释,可选地使用匹配的注释参数
获取匹配正则表达式的所有资源
获取具有特定签名的所有方法,包括参数、参数注释和返回类型
获取所有方法参数名称
获取代码中的所有字段/方法/构造函数用法
maven项目请前往:https://mvnrepository.com/artifact/org.reflections/reflections拷贝依赖,非maven项目可在连接中找到jar下载拷贝到项目中。
<!-- https://mvnrepository.com/artifact/org.reflections/reflections --> <dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.10.2</version> </dependency>
Reflections reflections = new Reflections("my.project.prefix"); Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class); Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
要使用反射应该使用构造函数来实例化它(传入的为包所在的路径:请注意,是相对路径,例如:com.cn.xxxx),然后根据扫描仪,方便的使用查询方法:
Reflections reflections = new Reflections("my.package.prefix"); //or Reflections reflections = new Reflections(ClasspathHelper.forPackage("my.package.prefix"), new SubTypesScanner(), new TypesAnnotationScanner(), new FilterBuilder().include(...), ...); //or using the ConfigurationBuilder new Reflections(new ConfigurationBuilder() .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.project.prefix"))) .setUrls(ClasspathHelper.forPackage("my.project.prefix")) .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...));
Set<Class<? extends Module>> modules = reflections.getSubTypesOf(com.google.inject.Module.class); Set<Class<?>> singletons = reflections.getTypesAnnotatedWith(javax.inject.Singleton.class); Set<String> properties = reflections.getResources(Pattern.compile(".\*\.properties")); Set<Constructor> injectables = reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class); Set<Method> deprecateds = reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class); Set<Field> ids = reflections.getFieldsAnnotatedWith(javax.persistence.Id.class); Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class); Set<Method> voidMethods = reflections.getMethodsReturn(void.class); Set<Method> pathParamMethods = reflections.getMethodsWithAnyParamAnnotated(PathParam.class); Set<Method> floatToString = reflections.getConverters(Float.class, String.class); List<String> parameterNames = reflections.getMethodsParamNames(Method.class); Set<Member> fieldUsage = reflections.getFieldUsage(Field.class); Set<Member> methodUsage = reflections.getMethodUsage(Method.class); Set<Member> constructorUsage = reflections.getConstructorUsage(Constructor.class);
下面示例将展示如何获取本项目中所有加载了某个注解的方法
例如,项目中有个方法如下(该方法使用了@EncodingResponse 注解):
@EncodingResponse public UserInfo getAllUsers(String userId) { ....... }
我们将通过上面的org.reflections来获取项目中所有使用了@EncodingResponse 注解的方法
Reflections reflections = new Reflections(new ConfigurationBuilder() .setUrls(ClasspathHelper.forPackage("cn.com.xxx")) .setScanners(new MethodAnnotationsScanner())); Set<Method> allMethods = reflections.getMethodsAnnotatedWith(EncodingResponse .class); System.out.println(allMethods.size()); for (Method m : allMethods) { if (m.isAnnotationPresent(EncodingResponse .class)) { System.out.println("哈哈哈哈"); } }
注意:上述代码可以在任何方法中执行,而无需应用运行(你可以在任何时候,适合地方调用该方法)
有条件的可以看下官方文档:https://www.javadoc.io/doc/org.reflections/reflections/0.9.10/org/reflections/Reflections.html