mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add InvocationTargetRuntimeException
This commit is contained in:
parent
5d4e18a63b
commit
7ceab0dc3c
@ -11,6 +11,7 @@
|
|||||||
* 【system 】 JavaInfo增加版本(issue#2310@Github)
|
* 【system 】 JavaInfo增加版本(issue#2310@Github)
|
||||||
* 【core 】 新增CastUtil(pr#2313@Github)
|
* 【core 】 新增CastUtil(pr#2313@Github)
|
||||||
* 【core 】 ByteUtil新增bytesToShort重载(issue#I57FA7@Gitee)
|
* 【core 】 ByteUtil新增bytesToShort重载(issue#I57FA7@Gitee)
|
||||||
|
* 【core 】 ReflectUtil.invoke方法抛出运行时异常增加InvocationTargetRuntimeException(issue#I57GI2@Gitee)
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 MapUtil.map对null友好,且修复了测试用例中分组问题(pr#614@Gitee)
|
* 【core 】 MapUtil.map对null友好,且修复了测试用例中分组问题(pr#614@Gitee)
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ import cn.hutool.core.bean.NullWrapperBean;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.UniqueKeySet;
|
import cn.hutool.core.collection.UniqueKeySet;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.exceptions.InvocationTargetRuntimeException;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.Filter;
|
import cn.hutool.core.lang.Filter;
|
||||||
@ -16,6 +17,7 @@ import java.lang.reflect.AccessibleObject;
|
|||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -864,7 +866,7 @@ public class ReflectUtil {
|
|||||||
* Set -》 HashSet
|
* Set -》 HashSet
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @param type 被构造的类
|
* @param type 被构造的类
|
||||||
* @return 构造后的对象,构造失败返回{@code null}
|
* @return 构造后的对象,构造失败返回{@code null}
|
||||||
*/
|
*/
|
||||||
@ -873,7 +875,7 @@ public class ReflectUtil {
|
|||||||
Assert.notNull(type);
|
Assert.notNull(type);
|
||||||
|
|
||||||
// 原始类型
|
// 原始类型
|
||||||
if(type.isPrimitive()){
|
if (type.isPrimitive()) {
|
||||||
return (T) ClassUtil.getPrimitiveDefaultValue(type);
|
return (T) ClassUtil.getPrimitiveDefaultValue(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -985,10 +987,42 @@ public class ReflectUtil {
|
|||||||
* @param method 方法(对象方法或static方法都可)
|
* @param method 方法(对象方法或static方法都可)
|
||||||
* @param args 参数对象
|
* @param args 参数对象
|
||||||
* @return 结果
|
* @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")
|
@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);
|
setAccessible(method);
|
||||||
|
|
||||||
// 检查用户传入参数:
|
// 检查用户传入参数:
|
||||||
@ -1025,11 +1059,7 @@ public class ReflectUtil {
|
|||||||
return MethodHandleUtil.invokeSpecial(obj, method, args);
|
return MethodHandleUtil.invokeSpecial(obj, method, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs);
|
||||||
return (T) method.invoke(ClassUtil.isStatic(method) ? null : obj, actualArgs);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new UtilException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1041,7 +1071,7 @@ public class ReflectUtil {
|
|||||||
* @param methodName 方法名
|
* @param methodName 方法名
|
||||||
* @param args 参数列表
|
* @param args 参数列表
|
||||||
* @return 执行结果
|
* @return 执行结果
|
||||||
* @throws UtilException IllegalAccessException包装
|
* @throws UtilException IllegalAccessException等异常包装
|
||||||
* @see NullWrapperBean
|
* @see NullWrapperBean
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user