别再造反射轮子了,Spring中ReflectionUtils 工具类,应有尽有!

简介: ReflectionUtils是spring针对反射提供的工具类。

作者:策马踏清风
链接:https://www.jianshu.com/p/756778f5dc87

ReflectionUtilsspring针对反射提供的工具类。

handleReflectionException异常处理

推荐一个开源免费的 Spring Boot 实战项目:

https://github.com/javastacks/spring-boot-best-practice

源码:

public static void handleReflectionException(Exception ex) {
    if (ex instanceof NoSuchMethodException) {
        throw new IllegalStateException("Method not found: " + ex.getMessage());
    }
    if (ex instanceof IllegalAccessException) {
        throw new IllegalStateException("Could not access method: " + ex.getMessage());
    }
    if (ex instanceof InvocationTargetException) {
        handleInvocationTargetException((InvocationTargetException) ex);
    }
    if (ex instanceof RuntimeException) {
        throw (RuntimeException) ex;
    }
    throw new UndeclaredThrowableException(ex);
}

主要是将反射中的异常分成几个部分,规范化输出

  • boolean declaresException(Method method, Class<?> exceptionType)判断方法上是否声明了指定的异常类型

findField查找字段

  • Field findField(Class<?> clazz, String name, Class<?> type)

查找指定类的指定名称和指定类型的方法

public static Field findField(Class<?> clazz, String name, Class<?> type) {
    Class<?> searchType = clazz;
    while (Object.class != searchType && searchType != null) {
        Field[] fields = getDeclaredFields(searchType);
        for (Field field : fields) {
            if ((name == null || name.equals(field.getName())) &&
                    (type == null || type.equals(field.getType()))) {
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

获取所有的方法,然后循环遍历,知道找到满足条件的返回 其中getDeclaredFields(searchType)方法使用ConcurrentReferenceHashMapField缓存,并优先从缓存中取。

  • Field findField(Class<?> clazz, String name)

设置字段setField

  • void setField(Field field, Object target, Object value)设置指定字段的值 直接使用Field.set/get方法,然后格式化处理了异常
  • Object getField(Field field, Object target)获取指定字段的值

查找方法findMethod

  • Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)查找方法,方法的参数是一个可变长的Class
  • Method findMethod(Class<?> clazz, String name)直接查,不指定参数

调用方法invokeMethod

  • Object invokeMethod(Method method, Object target, Object... args)调用方法
  • Object invokeMethod(Method method, Object target)简单版本
  • 插播一条:如果你近期准备面试跳槽,建议在Java面试库小程序在线刷题,涵盖 2000+ 道 Java 面试题,几乎覆盖了所有主流技术面试题。

判断类

  • boolean declaresException(Method method, Class<?> exceptionType)方法上是否声明了指定的异常
  • boolean isPublicStaticFinal(Field field)判断字段首付是public static final
  • boolean isEqualsMethod(Method method)判断方法是否是equals方法
  • boolean isHashCodeMethod(Method method)判断方法是否是hashcode方法
  • boolean isToStringMethod(Method method)判断方法是否是toString方法
  • boolean isObjectMethod(Method method)判断方法是否是Object类上的方法

操作

  • void makeAccessible(Field field)使私有的字段可写
  • void makeAccessible(Method method)私有方法可调用
  • void makeAccessible(Constructor<?> ctor)私有构造器可调用
  • void doWithLocalMethods(Class<?> clazz, MethodCallback mc)遍历类上的方法,并执行回调
public interface MethodCallback {
    void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
  • void doWithMethods(Class<?> clazz, MethodCallback mc, MethodFilter mf)增加了一个方法过滤器
public interface MethodFilter {
    boolean matches(Method method);
}
相关文章
|
Java Spring
解决Spring工具类BeanUtils copyProperties方法复制null的问题
解决Spring工具类BeanUtils copyProperties方法复制null的问题
864 0
|
3月前
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
106 3
|
4月前
|
存储 缓存 Java
深入解析Spring框架中的ReflectionUtils
深入解析Spring框架中的ReflectionUtils
|
5月前
|
开发框架 Java 数据库
|
12月前
|
存储 NoSQL Java
Spring Boot 如何编写一个通用的 Redis 工具类
Spring Boot 如何编写一个通用的 Redis 工具类
359 0
Spring Boot 如何编写一个通用的 Redis 工具类
|
10月前
|
Java Apache Spring
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
Spring BeanUtils 2、Cglib BeanCopier 3、Apache BeanUtils 4、Apache PropertyUtils 5、Dozer 那么,我们到底应该选择哪种工具类更加合适呢?为什么Java开发手册中提到禁止使用Apache BeanUtils呢
97 0
|
12月前
|
Java Spring
【Spring】org.springframework.util.StringUtils工具类中commaDelimitedListToStringArray的使用
【Spring】org.springframework.util.StringUtils工具类中commaDelimitedListToStringArray的使用
59 0
|
Java Spring
Spring 如何通过反射获取controller 包下所有的类,以及类上的注解
Spring 如何通过反射获取controller 包下所有的类,以及类上的注解
289 0
|
Java Spring
动态获取spring管理的bean工具类
动态获取spring管理的bean工具类
148 0
|
消息中间件 JavaScript 小程序
别再自己瞎写工具类了,Spring Boot 内置工具类应有尽有, 建议收藏!!
别再自己瞎写工具类了,Spring Boot 内置工具类应有尽有, 建议收藏!!
下一篇
无影云桌面