!837 ReflectUtil.invokeRaw方法 对检查用户传入参数时,对于类型不同的字段,尝试转换,转换失败后的操作

Merge pull request !837 from 高邮湖徐先生/v5-dev
This commit is contained in:
Looly 2022-10-16 12:50:27 +00:00 committed by Gitee
commit f31eb8512e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -327,7 +327,7 @@ public class ReflectUtil {
* *
* @param obj 对象如果是static字段此参数为null * @param obj 对象如果是static字段此参数为null
* @param field 字段 * @param field 字段
* @param value 当值类型与字段类型不匹配时会尝试转换 * @param value 当值类型与字段类型不匹配时会尝试转换
* @throws UtilException UtilException 包装IllegalAccessException异常 * @throws UtilException UtilException 包装IllegalAccessException异常
*/ */
public static void setFieldValue(Object obj, Field field, Object value) throws UtilException { public static void setFieldValue(Object obj, Field field, Object value) throws UtilException {
@ -544,10 +544,10 @@ public class ReflectUtil {
if (ArrayUtil.isNotEmpty(methods)) { if (ArrayUtil.isNotEmpty(methods)) {
for (Method method : methods) { for (Method method : methods) {
if (StrUtil.equals(methodName, method.getName(), ignoreCase) if (StrUtil.equals(methodName, method.getName(), ignoreCase)
&& ClassUtil.isAllAssignableFrom(method.getParameterTypes(), paramTypes) && ClassUtil.isAllAssignableFrom(method.getParameterTypes(), paramTypes)
//排除协变桥接方法pr#1965@Github //排除协变桥接方法pr#1965@Github
&& (res == null && (res == null
|| res.getReturnType().isAssignableFrom(method.getReturnType()))) { || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
res = method; res = method;
} }
} }
@ -613,9 +613,9 @@ public class ReflectUtil {
if (ArrayUtil.isNotEmpty(methods)) { if (ArrayUtil.isNotEmpty(methods)) {
for (Method method : methods) { for (Method method : methods) {
if (StrUtil.equals(methodName, method.getName(), ignoreCase) if (StrUtil.equals(methodName, method.getName(), ignoreCase)
//排除协变桥接方法pr#1965@Github //排除协变桥接方法pr#1965@Github
&& (res == null && (res == null
|| res.getReturnType().isAssignableFrom(method.getReturnType()))) { || res.getReturnType().isAssignableFrom(method.getReturnType()))) {
res = method; res = method;
} }
} }
@ -665,7 +665,7 @@ public class ReflectUtil {
public static Method[] getMethods(Class<?> beanClass) throws SecurityException { public static Method[] getMethods(Class<?> beanClass) throws SecurityException {
Assert.notNull(beanClass); Assert.notNull(beanClass);
return METHODS_CACHE.computeIfAbsent(beanClass, return METHODS_CACHE.computeIfAbsent(beanClass,
() -> getMethodsDirectly(beanClass, true, true)); () -> getMethodsDirectly(beanClass, true, true));
} }
/** /**
@ -715,8 +715,8 @@ public class ReflectUtil {
*/ */
public static boolean isEqualsMethod(Method method) { public static boolean isEqualsMethod(Method method) {
if (method == null || if (method == null ||
1 != method.getParameterCount() || 1 != method.getParameterCount() ||
false == "equals".equals(method.getName())) { false == "equals".equals(method.getName())) {
return false; return false;
} }
return (method.getParameterTypes()[0] == Object.class); return (method.getParameterTypes()[0] == Object.class);
@ -730,8 +730,8 @@ public class ReflectUtil {
*/ */
public static boolean isHashCodeMethod(Method method) { public static boolean isHashCodeMethod(Method method) {
return method != null// return method != null//
&& "hashCode".equals(method.getName())// && "hashCode".equals(method.getName())//
&& isEmptyParam(method); && isEmptyParam(method);
} }
/** /**
@ -742,8 +742,8 @@ public class ReflectUtil {
*/ */
public static boolean isToStringMethod(Method method) { public static boolean isToStringMethod(Method method) {
return method != null// return method != null//
&& "toString".equals(method.getName())// && "toString".equals(method.getName())//
&& isEmptyParam(method); && isEmptyParam(method);
} }
/** /**
@ -846,7 +846,7 @@ public class ReflectUtil {
public static <T> T newInstance(Class<T> clazz, Object... params) throws UtilException { public static <T> T newInstance(Class<T> clazz, Object... params) throws UtilException {
if (ArrayUtil.isEmpty(params)) { if (ArrayUtil.isEmpty(params)) {
final Constructor<T> constructor = getConstructor(clazz); final Constructor<T> constructor = getConstructor(clazz);
if(null == constructor){ if (null == constructor) {
throw new UtilException("No constructor for [{}]", clazz); throw new UtilException("No constructor for [{}]", clazz);
} }
try { try {
@ -1056,7 +1056,7 @@ public class ReflectUtil {
actualArgs[i] = null; actualArgs[i] = null;
} else if (false == parameterTypes[i].isAssignableFrom(args[i].getClass())) { } else if (false == parameterTypes[i].isAssignableFrom(args[i].getClass())) {
//对于类型不同的字段尝试转换转换失败则使用原对象类型 //对于类型不同的字段尝试转换转换失败则使用原对象类型
final Object targetValue = Convert.convert(parameterTypes[i], args[i]); final Object targetValue = Convert.convertQuietly(parameterTypes[i], args[i], args[i]);
if (null != targetValue) { if (null != targetValue) {
actualArgs[i] = targetValue; actualArgs[i] = targetValue;
} }