Java教程
  • Java教程
  • 简介
    • Java 库
      • JMH 应用指南
      • ZXing 应用指南
      • Thumbnailator 应用指南
      • Jsoup 应用指南
      • Dozer 应用指南
      • 细说 Java 主流日志工具库
      • Mockito 应用指南
      • JavaMail 应用指南
      • Freemark 应用指南
      • Lombok 应用指南
      • Java 与 JSON
      • JUnit5 应用指南
      • 细说 Java 主流工具包
      • Java 二进制序列化库
      • Reflections 应用指南
    • Java 工具
      • 构建工具
        • Ant 简易教程
        • Maven 教程
          • Maven 实战问题和最佳实践
          • Maven 教程之入门指南
          • Maven 插件之代码检查
          • Maven 教程之发布 jar 到私服或中央仓库
          • Maven 教程之 settings.xml 详解
          • Maven 教程之 pom.xml 详解
        • Gradle 应用指南
      • Elastic 技术栈
        • Elastic 技术栈之 Logstash 基础
        • Elastic 技术栈之 Kibana
        • Elasticsearch 运维
        • Filebeat 运维
        • Elasticsearch
        • Elastic 技术栈之 Filebeat
        • Logstash 运维
        • Elastic 快速入门
        • Kibana 运维
      • Java IDE
        • Eclipse 应用指南
        • Intellij IDEA 应用指南
        • vscode 应用指南
      • test
        • JMeter 应用指南
    • 附录
      • resources
    • Java Tutorial
    • java-tutorial
  • codes
    • javatool
      • javatool-server
    • javalib
      • Lombok 应用指南
Powered by GitBook
On this page
  • 使用
  • ReflectionUtils

Was this helpful?

  1. 简介
  2. Java 库

Reflections 应用指南

PreviousJava 二进制序列化库NextJava 工具

Last updated 5 years ago

Was this helpful?

引入

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.11</version>
</dependency>

典型应用

Reflections reflections = new Reflections("my.project");
Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

使用

基本上,使用 Reflections 首先使用 urls 和 scanners 对其进行实例化

//scan urls that contain 'my.package', include inputs starting with 'my.package', use the default scanners
Reflections reflections = new Reflections("my.package");

//or using ConfigurationBuilder
new Reflections(new ConfigurationBuilder()
     .setUrls(ClasspathHelper.forPackage("my.project.prefix"))
     .setScanners(new SubTypesScanner(),
                  new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...),
     .filterInputsBy(new FilterBuilder().includePackage("my.project.prefix"))
     ...);

然后,使用方便的查询方法

// 子类型扫描
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<Method> resources =
    reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);
Set<Constructor> injectables =
    reflections.getConstructorsAnnotatedWith(javax.inject.Inject.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);
// 方法参数名扫描
List<String> parameterNames =
    reflections.getMethodParamNames(Method.class)
// 方法使用扫描
Set<Member> usages =
    reflections.getMethodUsages(Method.class)

说明:

  • 如果未配置扫描程序,则将使用默认值 - SubTypesScanner 和 TypeAnnotationsScanner。

  • 还可以配置类加载器,它将用于从名称中解析运行时类。

  • Reflection 默认情况下会扩展超类型。 这解决了传输 URL 不被扫描的一些问题。

ReflectionUtils

import static org.reflections.ReflectionUtils.*;

Set<Method> getters = getAllMethods(someClass,
  withModifier(Modifier.PUBLIC), withPrefix("get"), withParametersCount(0));

//or
Set<Method> listMethodsFromCollectionToBoolean =
  getAllMethods(List.class,
    withParametersAssignableTo(Collection.class), withReturnType(boolean.class));

Set<Field> fields = getAllFields(SomeClass.class, withAnnotation(annotation), withTypeAssignableTo(type));
使用
ReflectionUtils