当前位置: 首页 > 工具软件 > Reflections > 使用案例 >

Java反射工具包reflections

湛玄裳
2023-12-01

1. 简介

reflections是一个Java反射工具包,使用它可以非常方便的获取一下字段或者方法。

2. maven

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

3. 配置

最简单的配置,就是直接指定扫描包:

Reflections reflections = new Reflections(“java.util”);

当然,也可以:

@Test
public void config(){
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    //扫描vip.mycollege.jdk.reflect包
    Collection<URL> scannerPkg = ClasspathHelper.forPackage("vip.mycollege.jdk.reflect");

    SubTypesScanner subTypesScanner = new SubTypesScanner();

    //注解扫描
    TypeAnnotationsScanner typeAnnotationsScanner = new TypeAnnotationsScanner();

    FilterBuilder filter = new FilterBuilder()
            .includePackage("vip.mycollege.jdk.reflect")//注解扫描包
            .excludePackage("vip.mycollege.jdk.reflect.thirdpart");//注解扫描扫描排除包
    typeAnnotationsScanner.filterResultsBy(filter);

    ConfigurationBuilder configuration = configurationBuilder
            .setUrls(scannerPkg)
            .setScanners(subTypesScanner, typeAnnotationsScanner);

    Reflections reflections = new Reflections(configuration);
}

4. 通过类型扫描

@Test
public void newInstance(){
    Reflections reflections = new Reflections("java.util");
    //获取List及其子类
    Set<Class<? extends List>> listImpls = reflections.getSubTypesOf(List.class);
    listImpls.forEach(System.out::println);
}

5. 通过注解扫描

@Test
public void methodAnnotationsScanner(){
    Reflections reflections = new Reflections(SCAN_PKG);
    //方法上有Deprecated注解
    Set<Method> resources = reflections.getMethodsAnnotatedWith(Deprecated.class);
    //有PostConstruct注解的构造方法
    Set<Constructor> injectables = reflections.getConstructorsAnnotatedWith(PostConstruct.class);
}

@Test
public void FieldAnnotationsScanner(){
    //指定包下有Nonnegative的字段
    Reflections reflections = new Reflections(SCAN_PKG);
    Set<Field> ids = reflections.getFieldsAnnotatedWith(Nonnegative.class);
}

6. 方法相关

@Test
public void methodParameterScanner(){
    Reflections reflections = new Reflections(SCAN_PKG);
    //参数类型是long和int
    Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class);
    //参数返回值是void
    Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
    //任何参数上有注解Nullable
    Set<Method> pathParamMethods = reflections.getMethodsWithAnyParamAnnotated(Nullable.class);

    for(Method method : someMethods) {
        //获取方法参数的名称
        List<String> parameterNames = reflections.getMethodParamNames(method);
        parameterNames.forEach(System.out::println);
    }
}

7. 工具类

@Test
public void reflectionUtils(){
    //必须是public方法
    Predicate<Method> publicPredicate = ReflectionUtils.withModifier(Modifier.PUBLIC);
    //有get前缀
    Predicate<Method> getPredicate = ReflectionUtils.withPrefix("get");
    //参数个数为0
    Predicate<Member> paramPredicate = ReflectionUtils.withParametersCount(0);
    Set<Method> methods = ReflectionUtils.getAllMethods(LinkedList.class, publicPredicate, getPredicate, paramPredicate);
    methods.forEach(method -> System.out.println(method.getName()));

    System.out.println("---------------");
    //参数必须是Collection及其子类
    Predicate<Member> paramsPredicate = ReflectionUtils.withParametersAssignableTo(Collection.class);
    //返回类型是boolean
    Predicate<Method> returnPredicate = ReflectionUtils.withReturnType(boolean.class);
    methods = ReflectionUtils.getAllMethods(LinkedList.class, paramsPredicate, returnPredicate);
    methods.forEach(method -> System.out.println(method.getName()));

    System.out.println("---------------");
    //字段有注解Native
    Predicate<Field> annotationPredicate = ReflectionUtils.withAnnotation(Native.class);
    //字段类型是int及其子类
    Predicate<Field> typeAssignablePredicate = ReflectionUtils.withTypeAssignableTo(int.class);
    Set<Field> fields = ReflectionUtils.getAllFields(Integer.class, annotationPredicate, typeAssignablePredicate);
//        Set<Field> fields = ReflectionUtils.getAllFields(Integer.class, annotationPredicate);
//        Set<Field> fields = ReflectionUtils.getAllFields(Integer.class, typeAssignablePredicate);
    fields.forEach(field -> System.out.println(field.getName()));
}

8. 资料文档

Github

 类似资料: