diff --git a/CHANGELOG.md b/CHANGELOG.md index 367f3f236..d3f82a83d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * 【system 】 JavaInfo增加版本(issue#2310@Github) * 【core 】 新增CastUtil(pr#2313@Github) * 【core 】 ByteUtil新增bytesToShort重载(issue#I57FA7@Gitee) +* 【core 】 ReflectUtil.invoke方法抛出运行时异常增加InvocationTargetRuntimeException(issue#I57GI2@Gitee) * ### 🐞Bug修复 * 【core 】 MapUtil.map对null友好,且修复了测试用例中分组问题(pr#614@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/InvocationTargetRuntimeException.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/InvocationTargetRuntimeException.java new file mode 100644 index 000000000..b6444bd54 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/InvocationTargetRuntimeException.java @@ -0,0 +1,34 @@ +package cn.hutool.core.exceptions; + +/** + * InvocationTargetException的运行时异常 + * + * @author looly + * @since 5.8.1 + */ +public class InvocationTargetRuntimeException extends UtilException { + + public InvocationTargetRuntimeException(Throwable e) { + super(e); + } + + public InvocationTargetRuntimeException(String message) { + super(message); + } + + public InvocationTargetRuntimeException(String messageTemplate, Object... params) { + super(messageTemplate, params); + } + + public InvocationTargetRuntimeException(String message, Throwable throwable) { + super(message, throwable); + } + + public InvocationTargetRuntimeException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) { + super(message, throwable, enableSuppression, writableStackTrace); + } + + public InvocationTargetRuntimeException(Throwable throwable, String messageTemplate, Object... params) { + super(throwable, messageTemplate, params); + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java index 39d94196e..7a4c741d4 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java @@ -5,6 +5,7 @@ import cn.hutool.core.bean.NullWrapperBean; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.UniqueKeySet; import cn.hutool.core.convert.Convert; +import cn.hutool.core.exceptions.InvocationTargetRuntimeException; import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Filter; @@ -16,6 +17,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.AbstractMap; import java.util.ArrayList; @@ -864,7 +866,7 @@ public class ReflectUtil { * Set -》 HashSet * * - * @param 对象类型 + * @param 对象类型 * @param type 被构造的类 * @return 构造后的对象,构造失败返回{@code null} */ @@ -873,7 +875,7 @@ public class ReflectUtil { Assert.notNull(type); // 原始类型 - if(type.isPrimitive()){ + if (type.isPrimitive()) { return (T) ClassUtil.getPrimitiveDefaultValue(type); } @@ -985,10 +987,42 @@ public class ReflectUtil { * @param method 方法(对象方法或static方法都可) * @param args 参数对象 * @return 结果 - * @throws UtilException 一些列异常的包装 + * @throws InvocationTargetRuntimeException 目标方法执行异常 + * @throws UtilException {@link IllegalAccessException}异常的包装 + */ + public static T invoke(Object obj, Method method, Object... args) throws InvocationTargetRuntimeException, UtilException { + try { + return invokeRaw(obj, method, args); + } catch (InvocationTargetException e) { + throw new InvocationTargetRuntimeException(e); + } catch (IllegalAccessException e) { + throw new UtilException(e); + } + } + + /** + * 执行方法 + * + *

+ * 对于用户传入参数会做必要检查,包括: + * + *

+	 *     1、忽略多余的参数
+	 *     2、参数不够补齐默认值
+	 *     3、传入参数为null,但是目标参数类型为原始类型,做转换
+	 * 
+ * + * @param 返回对象类型 + * @param obj 对象,如果执行静态方法,此值为{@code null} + * @param method 方法(对象方法或static方法都可) + * @param args 参数对象 + * @return 结果 + * @throws InvocationTargetRuntimeException 目标方法执行异常 + * @throws UtilException {@link IllegalAccessException}异常的包装 + * @since 5.8.1 */ @SuppressWarnings("unchecked") - public static T invoke(Object obj, Method method, Object... args) throws UtilException { + public static T invokeRaw(Object obj, Method method, Object... args) throws InvocationTargetException, IllegalAccessException { setAccessible(method); // 检查用户传入参数: @@ -1025,11 +1059,7 @@ public class ReflectUtil { return MethodHandleUtil.invokeSpecial(obj, method, args); } - try { - return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs); - } catch (Exception e) { - throw new UtilException(e); - } + return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs); } /** @@ -1041,7 +1071,7 @@ public class ReflectUtil { * @param methodName 方法名 * @param args 参数列表 * @return 执行结果 - * @throws UtilException IllegalAccessException包装 + * @throws UtilException IllegalAccessException等异常包装 * @see NullWrapperBean * @since 3.1.2 */