Java工具类-Java非常好用的反射框架Reflections-《Java笔记》

admin 2025-10-19 03:11:40 编程 来源:ZONE.CI 全球网 0 阅读模式

Java

简介

Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据。使用Reflections可以很轻松的获取以下元数据信息:

  • 获取某个类型的全部子类
  • 只要类型、构造器、方法,字段上带有特定注解,便能获取带有这个注解的全部信息(类型、构造器、方法,字段)
  • 获取所有能匹配某个正则表达式的资源
  • 获取所有带有特定签名的方法,包括参数,参数注解,返回类型
  • 获取所有方法的名字
  • 获取代码里所有字段、方法名、构造器的使用

    Maven依赖

    在pom.xml中添加reflections的依赖:
    1. <dependency>
    2. <groupId>org.reflections</groupId>
    3. <artifactId>reflections</artifactId>
    4. <version>0.9.11</version>
    5. </dependency>
    1. // 实例化Reflections,并指定要扫描的包名
    2. Reflections reflections = new Reflections("my.project");
    3. // 获取某个类的所有子类
    4. Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
    5. // 获取包含某个注解的所有类
    6. Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

    使用

    ```java //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”)) …);

  1. <a name="pbgSm"></a>
  2. ### 扫描子类
  3. ```java
  4. Set<Class<? extends Module>> modules = reflections.getSubTypesOf(com.google.inject.Module.class);

扫描注解

  1. //TypeAnnotationsScanner
  2. Set<Class<?>> singletons = reflections.getTypesAnnotatedWith(javax.inject.Singleton.class);

扫描资源

  1. //ResourcesScanner
  2. Set<String> properties = reflections.getResources(Pattern.compile(".*\\.properties"));

扫描方法注解

  1. //MethodAnnotationsScanner
  2. Set<Method> resources = reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);
  3. Set<Constructor> injectables = reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class);

扫描字段注解

  1. //FieldAnnotationsScanner
  2. Set<Field> ids = reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);

扫描方法参数

  1. //MethodParameterScanner
  2. Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class);
  3. Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
  4. Set<Method> pathParamMethods = reflections.getMethodsWithAnyParamAnnotated(PathParam.class);

扫描方法参数名

  1. //MethodParameterNamesScanner
  2. List<String> parameterNames = reflections.getMethodParamNames(Method.class)

扫描方法调用情况

  1. //MemberUsageScanner
  2. Set<Member> usages = reflections.getMethodUsages(Method.class)

参考资料

https://github.com/ronmamo/reflections

以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  12