Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 39 additions & 43 deletions spring-beans/src/main/java/org/springframework/beans/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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++;
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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.
* <p>Note: The source and target classes do not have to match or even be derived
Expand Down Expand Up @@ -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);
}
}
}
Expand Down