ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

「工具」反射框架Reflections

2022-01-11 08:32:20  阅读:160  来源: 互联网

标签:反射 Set 框架 扫描 Reflections 注解 new class reflections


Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据。

使用Reflections可以很轻松的获取以下元数据信息:

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

Maven依赖

在pom.xml中添加reflections的依赖:

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

API 使用

//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);

扫描注解

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

扫描资源

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

扫描方法注解

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

扫描字段注解

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

扫描方法参数

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

扫描方法参数名

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

扫描方法调用情况

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

总结

项目中使用

public class ReflectionTest {
    public static void main(String[] args) {
        // 扫包
        Reflections reflections = new Reflections(new ConfigurationBuilder()
                .forPackages("com.boothsun.reflections") // 指定路径URL
                .addScanners(new SubTypesScanner()) // 添加子类扫描工具
                .addScanners(new FieldAnnotationsScanner()) // 添加 属性注解扫描工具
                .addScanners(new MethodAnnotationsScanner() ) // 添加 方法注解扫描工具
                .addScanners(new MethodParameterScanner() ) // 添加方法参数扫描工具
                );
        // 反射出子类
        Set<Class<? extends ISayHello>> set = reflections.getSubTypesOf( ISayHello.class ) ;
        System.out.println("getSubTypesOf:" + set);

​    // 反射出带有指定注解的类
​    Set<Class<?>> ss = reflections.getTypesAnnotatedWith( MyAnnotation.class );
​    System.out.println("getTypesAnnotatedWith:" + ss);

​    // 获取带有特定注解对应的方法
​    Set<Method> methods = reflections.getMethodsAnnotatedWith( MyMethodAnnotation.class ) ;
​    System.out.println("getMethodsAnnotatedWith:" + methods);

​    // 获取带有特定注解对应的字段
​    Set<Field> fields = reflections.getFieldsAnnotatedWith( Autowired.class ) ;
​    System.out.println("getFieldsAnnotatedWith:" + fields);

​    // 获取特定参数对应的方法
​    Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class);
​    System.out.println("getMethodsMatchParams:" + someMethods);

​    Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
​    System.out.println( "getMethodsReturn:" + voidMethods);

​    Set<Method> pathParamMethods =reflections.getMethodsWithAnyParamAnnotated( PathParam.class);
​    System.out.println("getMethodsWithAnyParamAnnotated:" + pathParamMethods);
}

}

 

标签:反射,Set,框架,扫描,Reflections,注解,new,class,reflections
来源: https://www.cnblogs.com/xfeiyun/p/15786771.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有