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

莱莱  金牌会员 | 2024-2-18 08:57:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 900|帖子 900|积分 2700

作者:策马踏清风
链接:https://www.jianshu.com/p/756778f5dc87
ReflectionUtils是spring针对反射提供的工具类。
handleReflectionException异常处理

推荐一个开源免费的 Spring Boot 实战项目:
https://github.com/javastacks/spring-boot-best-practice
源码:
  1. public static void handleReflectionException(Exception ex) {
  2.     if (ex instanceof NoSuchMethodException) {
  3.         throw new IllegalStateException("Method not found: " + ex.getMessage());
  4.     }
  5.     if (ex instanceof IllegalAccessException) {
  6.         throw new IllegalStateException("Could not access method: " + ex.getMessage());
  7.     }
  8.     if (ex instanceof InvocationTargetException) {
  9.         handleInvocationTargetException((InvocationTargetException) ex);
  10.     }
  11.     if (ex instanceof RuntimeException) {
  12.         throw (RuntimeException) ex;
  13.     }
  14.     throw new UndeclaredThrowableException(ex);
  15. }
复制代码
主要是将反射中的异常分成几个部分,规范化输出

  • boolean declaresException(Method method, Class exceptionType)
    判断方法上是否声明了指定的异常类型
findField查找字段


  • Field findField(Class clazz, String name, Class type)
查找指定类的指定名称和指定类型的方法
  1. public static Field findField(Class<?> clazz, String name, Class<?> type) {
  2.     Class<?> searchType = clazz;
  3.     while (Object.class != searchType && searchType != null) {
  4.         Field[] fields = getDeclaredFields(searchType);
  5.         for (Field field : fields) {
  6.             if ((name == null || name.equals(field.getName())) &&
  7.                     (type == null || type.equals(field.getType()))) {
  8.                 return field;
  9.             }
  10.         }
  11.         searchType = searchType.getSuperclass();
  12.     }
  13.     return null;
  14. }
复制代码
获取所有的方法,然后循环遍历,知道找到满足条件的返回
其中getDeclaredFields(searchType)方法使用ConcurrentReferenceHashMap将Field缓存,并优先从缓存中取。

  • 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)简单版本
判断类


  • 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)
    遍历类上的方法,并执行回调
  1. public interface MethodCallback {
  2.     void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
  3. }
复制代码

  • void doWithMethods(Class clazz, MethodCallback mc, MethodFilter mf)
    增加了一个方法过滤器
  1. public interface MethodFilter {
  2.     boolean matches(Method method);
  3. }
复制代码
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
2.劲爆!Java 协程要来了。。。
3.Spring Boot 2.x 教程,太全了!
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
5.《Java开发手册(嵩山版)》最新发布,速速下载!
觉得不错,别忘了随手点赞+转发哦!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表