add InvocationTargetRuntimeException

This commit is contained in:
Looly 2022-05-16 10:21:33 +08:00
parent 5d4e18a63b
commit 7ceab0dc3c
3 changed files with 75 additions and 10 deletions

View File

@ -11,6 +11,7 @@
* 【system 】 JavaInfo增加版本issue#2310@Github
* 【core 】 新增CastUtilpr#2313@Github
* 【core 】 ByteUtil新增bytesToShort重载issue#I57FA7@Gitee
* 【core 】 ReflectUtil.invoke方法抛出运行时异常增加InvocationTargetRuntimeExceptionissue#I57GI2@Gitee
*
### 🐞Bug修复
* 【core 】 MapUtil.map对null友好且修复了测试用例中分组问题pr#614@Gitee

View File

@ -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);
}
}

View File

@ -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
* </pre>
*
* @param <T> 对象类型
* @param <T> 对象类型
* @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> 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);
}
}
/**
* 执行方法
*
* <p>
* 对于用户传入参数会做必要检查包括
*
* <pre>
* 1忽略多余的参数
* 2参数不够补齐默认值
* 3传入参数为null但是目标参数类型为原始类型做转换
* </pre>
*
* @param <T> 返回对象类型
* @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> T invoke(Object obj, Method method, Object... args) throws UtilException {
public static <T> 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
*/