diff --git a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java index f4ca77b3e1ca..a575fa917e21 100644 --- a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java @@ -340,10 +340,9 @@ public static Method findDeclaredMethod(Class clazz, String methodName, Class return clazz.getDeclaredMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { - if (clazz.getSuperclass() != null) { - return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes); - } - return null; + return Optional.ofNullable(clazz.getSuperclass()) + .map(superClass -> findDeclaredMethod(superClass.getSuperclass(), methodName, paramTypes)) + .orElse(null); } } @@ -365,7 +364,7 @@ public static Method findDeclaredMethod(Class clazz, String methodName, Class @Nullable public static Method findMethodWithMinimalParameters(Class clazz, String methodName) throws IllegalArgumentException { - + Method targetMethod = findMethodWithMinimalParameters(clazz.getMethods(), methodName); if (targetMethod == null) { targetMethod = findDeclaredMethodWithMinimalParameters(clazz, methodName); @@ -412,21 +411,22 @@ public static Method findMethodWithMinimalParameters(Method[] methods, String me Method targetMethod = null; int numMethodsFoundWithCurrentMinimumArgs = 0; for (Method method : methods) { - if (method.getName().equals(methodName)) { - int numParams = method.getParameterCount(); - if (targetMethod == null || numParams < targetMethod.getParameterCount()) { + if (!method.getName().equals(methodName)) { + continue; + } + int numParams = method.getParameterCount(); + if (targetMethod == null || numParams < targetMethod.getParameterCount()) { + targetMethod = method; + numMethodsFoundWithCurrentMinimumArgs = 1; + } + else if (!method.isBridge() && targetMethod.getParameterCount() == numParams) { + if (targetMethod.isBridge()) { + // Prefer regular method over bridge... targetMethod = method; - numMethodsFoundWithCurrentMinimumArgs = 1; } - else if (!method.isBridge() && targetMethod.getParameterCount() == numParams) { - if (targetMethod.isBridge()) { - // Prefer regular method over bridge... - targetMethod = method; - } - else { - // Additional candidate with same length - numMethodsFoundWithCurrentMinimumArgs++; - } + else { + // Additional candidate with same length + numMethodsFoundWithCurrentMinimumArgs++; } } } @@ -493,7 +493,6 @@ else if (startParen == -1) { } } - /** * Retrieve the JavaBeans {@code PropertyDescriptor}s of a given class. * @param clazz the Class to retrieve the PropertyDescriptors for @@ -632,9 +631,7 @@ public static boolean hasUniqueWriteMethod(PropertyDescriptor pd) { if (pd instanceof GenericTypeAwarePropertyDescriptor gpd) { return gpd.hasUniqueWriteMethod(); } - else { - return (pd.getWriteMethod() != null); - } + return (pd.getWriteMethod() != null); } /** @@ -647,11 +644,9 @@ public static MethodParameter getWriteMethodParameter(PropertyDescriptor pd) { if (pd instanceof GenericTypeAwarePropertyDescriptor gpd) { return new MethodParameter(gpd.getWriteMethodParameter()); } - else { - Method writeMethod = pd.getWriteMethod(); - Assert.state(writeMethod != null, "No write method available"); - return new MethodParameter(writeMethod, 0); - } + Method writeMethod = pd.getWriteMethod(); + Assert.state(writeMethod != null, "No write method available"); + return new MethodParameter(writeMethod, 0); } /** @@ -710,7 +705,6 @@ public static boolean isSimpleValueType(Class type) { return ClassUtils.isSimpleValueType(type); } - /** * Copy the property values of the given source bean into the target bean. *

Note: The source and target classes do not have to match or even be derived @@ -826,21 +820,23 @@ private static void copyProperties(Object source, Object target, @Nullable Class if (writeMethod != null && (ignoredProps == null || !ignoredProps.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = (sourceResults != null ? sourceResults.getPropertyDescriptor(targetPd.getName()) : targetPd); - if (sourcePd != null) { - Method readMethod = sourcePd.getReadMethod(); - if (readMethod != null) { - if (isAssignable(writeMethod, readMethod, sourcePd, targetPd)) { - try { - ReflectionUtils.makeAccessible(readMethod); - Object value = readMethod.invoke(source); - ReflectionUtils.makeAccessible(writeMethod); - writeMethod.invoke(target, value); - } - catch (Throwable ex) { - throw new FatalBeanException( - "Could not copy property '" + targetPd.getName() + "' from source to target", ex); - } - } + if (sourcePd == null) { + continue; + } + Method readMethod = sourcePd.getReadMethod(); + if (readMethod == null) { + continue; + } + if (isAssignable(writeMethod, readMethod, sourcePd, targetPd)) { + try { + ReflectionUtils.makeAccessible(readMethod); + Object value = readMethod.invoke(source); + ReflectionUtils.makeAccessible(writeMethod); + writeMethod.invoke(target, value); + } + catch (Throwable ex) { + throw new FatalBeanException( + "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } }