Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpEL cannot invoke varargs MethodHandle function with an array #33191

Closed
sbrannen opened this issue Jul 10, 2024 · 0 comments
Closed

SpEL cannot invoke varargs MethodHandle function with an array #33191

sbrannen opened this issue Jul 10, 2024 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jul 10, 2024

The Spring Expression Language (SpEL) currently cannot invoke a varargs MethodHandle function with an array, although that is supported for a varargs Method function.

Attempting to do so results in the array being supplied as a single argument to the MethodHandle.

For example, given a MethodHandle registered as a function named format that accepts (String, Object...), the SpEL expression #format('x', 'a', 'z') is supported, but the expression #format('x', new Object[]{'a', 'z'}) fails to evaluate since the MethodHandle is invoked with ('x', ['a', 'z']) whereas ('x', 'a', 'z') is required for a MethodHandle invocation.

@sbrannen sbrannen added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) labels Jul 10, 2024
@sbrannen sbrannen added this to the 6.1.11 milestone Jul 10, 2024
@sbrannen sbrannen self-assigned this Jul 10, 2024
@sbrannen sbrannen changed the title SpEL cannot invoke varargs MethodHandle function with an array SpEL cannot invoke varargs MethodHandle function with an array Jul 10, 2024
sbrannen added a commit that referenced this issue Jul 12, 2024
Prior to this commit, the Spring Expression Language (SpEL) could not
invoke a varargs MethodHandle function with a primitive array
containing the variable arguments, although that is supported for a
varargs Method function. Attempting to do so resulted in the first
element of the primitive array being supplied as a single argument to
the MethodHandle, effectively ignoring any variable arguments after the
first one.

This commit addresses this by updating the
convertAllMethodHandleArguments(...) method in ReflectionHelper as
follows when the user supplies the varargs already packaged in a
primitive array.

- Regarding conversion, use the wrapper type for a primitive varargs
  array, since we eventually need an Object array in order to invoke
  the MethodHandle in FunctionReference#executeFunctionViaMethodHandle().

- When deciding whether to convert a single element passed as varargs,
  we now check if the argument is an array that is assignable to the
  varargs array type.

- When converting an array supplied as the varargs, we now convert that
  array to the varargs array type instead of the varargs component type.

Note, however, that a SpEL expression cannot provide a primitive array
for an Object[] varargs target. This is due to the fact that the
ArrayToArrayConverter used by Spring's ConversionService does not
support conversion from a primitive array to Object[] -- for example,
from int[] to Object[].

See gh-33191
Closes gh-33198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant