mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add methods
This commit is contained in:
parent
7f906049f5
commit
aae583e57f
@ -18,6 +18,7 @@ import org.dromara.hutool.core.lang.Assert;
|
|||||||
import org.dromara.hutool.core.reflect.lookup.LookupUtil;
|
import org.dromara.hutool.core.reflect.lookup.LookupUtil;
|
||||||
|
|
||||||
import java.lang.invoke.MethodHandle;
|
import java.lang.invoke.MethodHandle;
|
||||||
|
import java.lang.invoke.MethodHandles;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -105,14 +106,33 @@ public class MethodHandleUtil {
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
* @throws HutoolException 执行异常包装
|
* @throws HutoolException 执行异常包装
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> T invokeExact(final Object obj, final Method method, final Object... args) throws HutoolException{
|
public static <T> T invokeExact(final Object obj, final Method method, final Object... args) throws HutoolException{
|
||||||
Assert.notNull(method, "Method must be not null!");
|
Assert.notNull(method, "Method must be not null!");
|
||||||
|
MethodHandle handle;
|
||||||
|
try {
|
||||||
|
handle = LookupUtil.unreflectMethod(method);
|
||||||
|
} catch (final Throwable e) {
|
||||||
|
throw ExceptionUtil.wrapRuntime(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null != obj) {
|
||||||
|
handle = handle.bindTo(obj);
|
||||||
|
}
|
||||||
|
return invokeWithArguments(handle, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行方法句柄,{@link MethodHandle#invokeWithArguments(Object...)}包装<br>
|
||||||
|
*
|
||||||
|
* @param handle {@link MethodHandle}
|
||||||
|
* @param args 方法参数值,支持子类转换和自动拆装箱
|
||||||
|
* @param <T> 返回值类型
|
||||||
|
* @return 方法返回值
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> T invokeWithArguments(final MethodHandle handle, final Object... args){
|
||||||
|
Assert.notNull(handle, "MethodHandle must be not null!");
|
||||||
try {
|
try {
|
||||||
MethodHandle handle = LookupUtil.unreflectMethod(method);
|
|
||||||
if (null != obj) {
|
|
||||||
handle = handle.bindTo(obj);
|
|
||||||
}
|
|
||||||
return (T) handle.invokeWithArguments(args);
|
return (T) handle.invokeWithArguments(args);
|
||||||
} catch (final Throwable e) {
|
} catch (final Throwable e) {
|
||||||
throw ExceptionUtil.wrapRuntime(e);
|
throw ExceptionUtil.wrapRuntime(e);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user