From e8b0e8e3d0e2f643bda9d851ee7b0dd5f2cddad7 Mon Sep 17 00:00:00 2001 From: Jabborov_Egamberdi_IUT <97242683+JabborovEgamberdi@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:16:24 +0500 Subject: [PATCH 1/2] Update BeanUtils.java Functional programming added, nested if logics are optimized --- .../org/springframework/beans/BeanUtils.java | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) 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..54fe7f12d60f 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); } } } From e7301648b01306ac308200a2ef399c7832250d8d Mon Sep 17 00:00:00 2001 From: Jabborov_Egamberdi_IUT <97242683+JabborovEgamberdi@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:46:59 +0500 Subject: [PATCH 2/2] Update BeanUtils.java Syntax error fixed --- .../src/main/java/org/springframework/beans/BeanUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 54fe7f12d60f..a575fa917e21 100644 --- a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java @@ -342,7 +342,7 @@ public static Method findDeclaredMethod(Class clazz, String methodName, Class catch (NoSuchMethodException ex) { return Optional.ofNullable(clazz.getSuperclass()) .map(superClass -> findDeclaredMethod(superClass.getSuperclass(), methodName, paramTypes)) - .orElse(null) + .orElse(null); } }