mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
e295779330
@ -0,0 +1,20 @@
|
|||||||
|
package cn.hutool.core.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为了解决反射过程中,需要传递null参数,但是会丢失参数类型而设立的包装类
|
||||||
|
*/
|
||||||
|
public class NullWrapperBean {
|
||||||
|
|
||||||
|
private final Class<?> mClasses;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param classes null的类型
|
||||||
|
*/
|
||||||
|
public NullWrapperBean(Class<?> classes) {
|
||||||
|
this.mClasses = classes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getClasses() {
|
||||||
|
return mClasses;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.NullWrapperBean;
|
||||||
import cn.hutool.core.convert.BasicType;
|
import cn.hutool.core.convert.BasicType;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
@ -144,7 +145,13 @@ public class ClassUtil {
|
|||||||
Object obj;
|
Object obj;
|
||||||
for (int i = 0; i < objects.length; i++) {
|
for (int i = 0; i < objects.length; i++) {
|
||||||
obj = objects[i];
|
obj = objects[i];
|
||||||
classes[i] = (null == obj) ? Object.class : obj.getClass();
|
if (obj instanceof NullWrapperBean) {
|
||||||
|
classes[i] = ((NullWrapperBean) obj).getClasses();
|
||||||
|
} else if (null == obj) {
|
||||||
|
classes[i] = Object.class;
|
||||||
|
} else {
|
||||||
|
classes[i] = obj.getClass();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
import cn.hutool.core.annotation.Alias;
|
import cn.hutool.core.annotation.Alias;
|
||||||
|
import cn.hutool.core.bean.NullWrapperBean;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
@ -890,8 +891,9 @@ public class ReflectUtil {
|
|||||||
// 检查用户传入参数:
|
// 检查用户传入参数:
|
||||||
// 1、忽略多余的参数
|
// 1、忽略多余的参数
|
||||||
// 2、参数不够补齐默认值
|
// 2、参数不够补齐默认值
|
||||||
// 3、传入参数为null,但是目标参数类型为原始类型,做转换
|
// 3、通过NullWrapperBean传递的参数,会直接赋值null
|
||||||
// 4、传入参数类型不对应,尝试转换类型
|
// 4、传入参数为null,但是目标参数类型为原始类型,做转换
|
||||||
|
// 5、传入参数类型不对应,尝试转换类型
|
||||||
final Class<?>[] parameterTypes = method.getParameterTypes();
|
final Class<?>[] parameterTypes = method.getParameterTypes();
|
||||||
final Object[] actualArgs = new Object[parameterTypes.length];
|
final Object[] actualArgs = new Object[parameterTypes.length];
|
||||||
if (null != args) {
|
if (null != args) {
|
||||||
@ -899,7 +901,10 @@ public class ReflectUtil {
|
|||||||
if (i >= args.length || null == args[i]) {
|
if (i >= args.length || null == args[i]) {
|
||||||
// 越界或者空值
|
// 越界或者空值
|
||||||
actualArgs[i] = ClassUtil.getDefaultValue(parameterTypes[i]);
|
actualArgs[i] = ClassUtil.getDefaultValue(parameterTypes[i]);
|
||||||
} else if (false == parameterTypes[i].isAssignableFrom(args[i].getClass())) {
|
} else if (args[i] instanceof NullWrapperBean) {
|
||||||
|
//如果是通过NullWrapperBean传递的null参数,直接赋值null
|
||||||
|
actualArgs[i] = null;
|
||||||
|
} else if (!parameterTypes[i].isAssignableFrom(args[i].getClass())) {
|
||||||
//对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
//对于类型不同的字段,尝试转换,转换失败则使用原对象类型
|
||||||
final Object targetValue = Convert.convert(parameterTypes[i], args[i]);
|
final Object targetValue = Convert.convert(parameterTypes[i], args[i]);
|
||||||
if (null != targetValue) {
|
if (null != targetValue) {
|
||||||
@ -920,6 +925,7 @@ public class ReflectUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行对象中指定方法
|
* 执行对象中指定方法
|
||||||
|
* 如果需要传递的参数为null,请使用NullWrapperBean来传递,不然会丢失类型信息
|
||||||
*
|
*
|
||||||
* @param <T> 返回对象类型
|
* @param <T> 返回对象类型
|
||||||
* @param obj 方法所在对象
|
* @param obj 方法所在对象
|
||||||
@ -927,6 +933,7 @@ public class ReflectUtil {
|
|||||||
* @param args 参数列表
|
* @param args 参数列表
|
||||||
* @return 执行结果
|
* @return 执行结果
|
||||||
* @throws UtilException IllegalAccessException包装
|
* @throws UtilException IllegalAccessException包装
|
||||||
|
* @see NullWrapperBean
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
*/
|
*/
|
||||||
public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException {
|
public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user