From fa2a58b9dbefe3a284e1a517e11f583b46c37ec8 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:46:47 +0200 Subject: [PATCH] Ensure varargs component type for MethodHandle is not null in SpEL This commit ensures that the varargs component type for a MethodHandle cannot be null in ReflectionHelper's convertAllMethodHandleArguments(...) method in SpEL. Closes gh-33193 --- .../expression/spel/support/ReflectionHelper.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java index 44426fcb317b..b030b5c6c8a2 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java @@ -380,12 +380,11 @@ public static boolean convertAllMethodHandleArguments(TypeConverter converter, O conversionOccurred |= (argument != arguments[i]); } - Class varArgClass = methodHandleType.lastParameterType().componentType(); + Class varArgClass = methodHandleType.lastParameterType(); ResolvableType varArgResolvableType = ResolvableType.forClass(varArgClass); - TypeDescriptor targetType = new TypeDescriptor(varArgResolvableType, varArgClass, null); + TypeDescriptor targetType = new TypeDescriptor(varArgResolvableType, varArgClass.componentType(), null); TypeDescriptor componentTypeDesc = targetType.getElementTypeDescriptor(); - // TODO Determine why componentTypeDesc can be null. - // Assert.state(componentTypeDesc != null, "Component type must not be null for a varargs array"); + Assert.state(componentTypeDesc != null, "Component type must not be null for a varargs array"); // If the target is varargs and there is just one more argument, then convert it here. if (varargsPosition == arguments.length - 1) { @@ -393,7 +392,7 @@ public static boolean convertAllMethodHandleArguments(TypeConverter converter, O TypeDescriptor sourceType = TypeDescriptor.forObject(argument); if (argument == null) { // Perform the equivalent of GenericConversionService.convertNullSource() for a single argument. - if (componentTypeDesc != null && componentTypeDesc.getObjectType() == Optional.class) { + if (componentTypeDesc.getObjectType() == Optional.class) { arguments[varargsPosition] = Optional.empty(); conversionOccurred = true; } @@ -402,7 +401,7 @@ public static boolean convertAllMethodHandleArguments(TypeConverter converter, O // convert it or wrap it in an array. For example, using StringToArrayConverter to // convert a String containing a comma would result in the String being split and // repackaged in an array when it should be used as-is. - else if (componentTypeDesc != null && !sourceType.isAssignableTo(componentTypeDesc)) { + else if (!sourceType.isAssignableTo(componentTypeDesc)) { arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType); } // Possible outcomes of the above if-else block: @@ -420,7 +419,7 @@ else if (componentTypeDesc != null && !sourceType.isAssignableTo(componentTypeDe else { for (int i = varargsPosition; i < arguments.length; i++) { Object argument = arguments[i]; - arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType); + arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), componentTypeDesc); conversionOccurred |= (argument != arguments[i]); } }