介绍
Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据。
使用Reflections可以很轻松的获取以下元数据信息:
1)获取某个类型的所有子类;比如,有一个父类是TestInterface,可以获取到TestInterface的所有子类。
2)获取某个注解的所有类型/字段变量,支持注解参数匹配。
3)使用正则表达式获取所有匹配的资源文件
4)获取特定签名方法。
通常的用法有:
引入依赖jar
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
项目中使用:
// 初始化工具类
Reflections reflections = new Reflections(new ConfigurationBuilder().forPackages(basePackages).addScanners(new SubTypesScanner()).addScanners(new FieldAnnotationsScanner()));
// 获取某个包下类型注解对应的类
Set<Class<?>> typeClass = reflections.getTypesAnnotatedWith(RpcInterface.class, true);
// 获取子类
Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
// 获取注解对应的方法
Set<Method> resources =reflections.getMethodsAnnotatedWith(SomeAnnotation.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<String> properties = reflections.getResources(Pattern.compile(".*\\.properties"));
具体demo
package demo;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import java.util.Set;
public class ReflectionsDemo {
public static void main(String[] args) {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forPackage("demo"))
.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner()));
// 1. 查找包含特定注解的类
Set<Class<?>> annotatedClasses = reflections.getTypesAnnotatedWith(DemoAnnotation.class);
System.out.println("Classes with DemoAnnotation:");
annotatedClasses.forEach(System.out::println);
// 2. 查找实现特定接口的类
Set<Class<? extends Plugin>> pluginClasses = reflections.getSubTypesOf(Plugin.class);
System.out.println("\nClasses implementing Plugin interface:");
pluginClasses.forEach(System.out::println);
// 3. 查找特定包中的所有类
Set<Class<?>> allClasses = reflections.getTypesAnnotatedWith(Object.class);
System.out.println("\nAll classes in the 'demo' package:");
allClasses.forEach(System.out::println);
// 4. 在运行时调用注解方法
System.out.println("\nExecuting plugins:");
pluginClasses.forEach(pluginClass -> {
try {
Plugin plugin = pluginClass.newInstance();
plugin.execute();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
});
}
}