Commit Graph

843 Commits

Author SHA1 Message Date
Sam Brannen c1236a3340 Support varargs-only MethodHandle as SpEL function
Prior to this commit, if a MethodHandle was registered as a custom
function in the Spring Expression Language (SpEL) for a static method
that accepted only a variable argument list (for example,
`static String func(String... args)`), attempting to invoke the
registered function within a SpEL expression resulted in a
ClassCastException because the varargs array was unnecessarily wrapped
in an Object[].

This commit modifies the logic in FunctionReference's internal
executeFunctionViaMethodHandle() method to address that.

Closes gh-34109
2024-12-18 17:59:14 +01:00
Sam Brannen 807e1e6126 Document support for varargs invocations in SpEL
Closes gh-33332
2024-11-18 11:54:22 +01:00
Sam Brannen 7196f3f554 Polish SpEL documentation 2024-11-18 11:38:20 +01:00
Sam Brannen cca245020d Improve documentation for SpelCompilerMode
Closes gh-33223
2024-11-14 11:36:46 +01:00
Sam Brannen 9dabfdf0bf Remove obsolete AstUtilsTests
AstUtilsTests got accidentally reintroduced in 5b5a072351 after
AstUtils was renamed to AccessorUtils in e3ba957431.
2024-11-12 12:15:11 +01:00
Brian Clozel 5b5a072351 Merge branch '6.1.x' 2024-11-12 10:05:24 +01:00
Sam Brannen e3ba957431 Polish SpEL internals 2024-11-12 10:04:06 +01:00
Sam Brannen caec8f4f36 Revise Javadoc for PropertyAccessor & IndexAccessor regarding ordering
Closes gh-33862
2024-11-12 10:04:06 +01:00
Sam Brannen ae16a7fc08 Update tests for 6.2
See gh-33861
2024-11-12 10:04:06 +01:00
Sam Brannen 43e7921bcc Introduce tests for SpEL PropertyAccessor ordering
Closes gh-33861
2024-11-12 10:04:06 +01:00
Sam Brannen 9724f9b9c8 Introduce tests for SpEL PropertyAccessor ordering
Closes gh-33861
2024-11-08 16:19:12 +01:00
Sam Brannen 8202282975 Polishing 2024-11-08 15:34:54 +01:00
Juergen Hoeller c979eddab1 Consistent deprecation markers for 6.2 2024-10-30 16:45:00 +01:00
Sam Brannen 5532574f56 Remove unused casts and variables 2024-10-25 15:16:05 +02:00
Sam Brannen 626f4279f6 Merge branch '6.1.x' 2024-10-22 13:11:39 +02:00
Sam Brannen fb0a108254 Improve Javadoc for core SpEL APIs 2024-10-22 13:04:29 +02:00
Sam Brannen c98f314665 Throw ParseException for unsupported character in SpEL expression
Prior to this commit, the SpEL Tokenizer threw an IllegalStateException
when an unsupported character was detected in a SpEL expression;
however, the Javadoc for ExpressionParser.parseExpression() states that
it throws a ParseException if an exception occurred during parsing.

This commit addresses that issue by throwing a SpelParseException for
an unsupported character in a SpEL expression, using a new
UNSUPPORTED_CHARACTER enum constant in SpelMessage.

Closes gh-33767
2024-10-22 13:03:08 +02:00
Juergen Hoeller e89218b39a Merge branch '6.1.x' 2024-10-16 13:46:22 +02:00
Juergen Hoeller 11d4272ff4 Use Locale.ROOT consistently for toLower/toUpperCase
Closes gh-33708
2024-10-16 13:36:23 +02:00
Yanming Zhou 8941e2876e Replace 'e.g.' with 'for example' in documentation and comments
Closes gh-33515
2024-09-26 14:11:17 +02:00
Sam Brannen 529f311bd4 Polish and harmonize implementations of SpEL components in spring-context 2024-09-03 17:16:20 +02:00
Sam Brannen 47f88e123f Invoke init/destroy/SpEL methods via public types whenever possible
Prior to this commit, when invoking init methods and destroy methods
for beans as well as methods within Spring Expression Language (SpEL)
expressions via reflection, we invoked them based on the "interface
method" returned from ClassUtils.getInterfaceMethodIfPossible(). That
works well for finding methods defined in an interface, but it does not
find public methods defined in a public superclass.

For example, in a SpEL expression it was previously impossible to
invoke toString() on a non-public type from a different module. This
could be seen when attempting to invoke toString() on an unmodifiable
list created by Collections.unmodifiableList(...). Doing so resulted in
an InaccessibleObjectException.

Although users can address that by adding an appropriate --add-opens
declaration, such as --add-opens java.base/java.util=ALL-UNNAMED, it is
better if applications do not have to add an --add-opens declaration
for such use cases in SpEL. The same applies to init methods and
destroy methods for beans.

This commit therefore introduces a new
getPubliclyAccessibleMethodIfPossible() method in ClassUtils which
serves as a replacement for getInterfaceMethodIfPossible().

This new method finds the first publicly accessible method in the
supplied method's type hierarchy that has a method signature equivalent
to the supplied method. If the supplied method is public and declared
in a public type, the supplied method will be returned. Otherwise, this
method recursively searches the class hierarchy and implemented
interfaces for an equivalent method that is public and declared in a
public type. If a publicly accessible equivalent method cannot be
found, the supplied method will be returned, indicating that no such
equivalent method exists.

All usage of getInterfaceMethodIfPossible() has been replaced with
getPubliclyAccessibleMethodIfPossible() in spring-beans and
spring-expression. In addition, getInterfaceMethodIfPossible() has been
marked as deprecated in favor of the new method.

As a bonus, the introduction of getPubliclyAccessibleMethodIfPossible()
allows us to delete a fair amount of obsolete code within the SpEL
infrastructure.

See gh-29857
Closes gh-33216
2024-08-22 14:35:21 +02:00
Sam Brannen e50383e921 Improve Javadoc for SpEL's Expression API 2024-08-17 15:59:00 +02:00
Sam Brannen d749d2949d Use new features from JUnit Jupiter 5.11
This commit primarily migrates to the new argumentSet() feature but also
applies additional polishing to our use of parameterized tests.

See gh-33395
2024-08-16 13:48:19 +02:00
Sam Brannen 1aea01687a Merge branch '6.1.x' 2024-08-13 17:40:13 +03:00
Sam Brannen e74406afd0 Introduce withAssignmentDisabled() option for SimpleEvaluationContext
To support additional use cases, this commit introduces a
withAssignmentDisabled() method in the Builder for
SimpleEvaluationContext.

Closes gh-33319
2024-08-13 17:26:34 +03:00
Sam Brannen a55207e88f Add tests for read-only IndexAccessors in 6.2 2024-08-07 19:23:06 +03:00
Sam Brannen a698f66c3a Merge branch '6.1.x'
# Conflicts:
#	spring-expression/src/main/java/org/springframework/expression/spel/support/SimpleEvaluationContext.java
2024-08-06 13:56:57 +03:00
Sam Brannen 0127de5a7a Enforce read-only semantics in SpEL's SimpleEvaluationContext
SimpleEvaluationContext.forReadOnlyDataBinding() documents that it
creates a SimpleEvaluationContext for read-only access to public
properties; however, prior to this commit write access was not disabled
for indexed structures when using the assignment operator, the
increment operator, or the decrement operator.

In order to better align with the documented contract for
forReadOnlyDataBinding(), this commit makes it possible to disable
assignment in general in order to enforce read-only semantics for
SpEL's SimpleEvaluationContext when created via the
forReadOnlyDataBinding() factory method. Specifically:

- This commit introduces a new isAssignmentEnabled() "default" method
  in the EvaluationContext API, which returns true by default.

- SimpleEvaluationContext overrides isAssignmentEnabled(), returning
  false if the context was created via the forReadOnlyDataBinding()
  factory method.

- The Assign, OpDec, and OpInc AST nodes -- representing the assignment
  (=), increment (++), and decrement (--) operators, respectively --
  now throw a SpelEvaluationException if assignment is disabled for the
  current EvaluationContext.

Closes gh-33319
2024-08-06 13:54:11 +03:00
Sam Brannen 1e804d8d4f Merge branch '6.1.x' 2024-08-05 16:37:57 +03:00
Sam Brannen fcc99a67b6 Support lists for varargs invocations in SpEL
The changes made in conjunction with gh-33013 resulted in a regression
for varargs support in SpEL expressions. Specifically, before gh-33013
one could supply a list -- for example, an "inline list" -- as the
varargs array when invoking a varargs constructor, method, or function
within a SpEL expression. However, after gh-33013 an inline list (or
collection in general) is no longer converted to an array for varargs
invocations. Instead, the list is supplied as a single argument of the
resulting varargs array which breaks applications that depend on the
previous behavior.

Although it was never intended that one could supply a collection as
the set of varargs, we concede that this is a regression in existing
behavior, and this commit therefore restores support for supplying a
java.util.List as the varargs "array".

In addition, this commit introduces the same "list to array" conversion
support for MethodHandle-based functions that accept varargs.

Note, however, that this commit does not restore support for converting
arbitrary single objects to an array for varargs invocations if the
single object is already an instance of the varargs array's component
type.

See gh-33013
Closes gh-33315
2024-08-05 16:28:11 +03:00
Sam Brannen 8afff3359b Polishing 2024-08-05 15:01:41 +03:00
Sam Brannen f3ab3905b8 Merge branch '6.1.x' 2024-08-05 14:21:27 +03:00
Sam Brannen c57c2272a1 Throw exception for failure to set property as index in SpEL
Prior to this commit, the Indexer in the Spring Expression Language
(SpEL) silently ignored a failure to set a property via the indexed
property syntax (['<property name>'] = <new value>) – for example, if
property write access was disabled in the EvaluationContext.

This commit addresses this issue by properly throwing a
SpelEvaluationException in PropertyIndexingValueRef.setValue(Object) if
the property could not be set.

Closes gh-33310
2024-08-05 14:15:55 +03:00
Sam Brannen 8ec23a0fcc Polishing 2024-08-05 14:15:39 +03:00
Sam Brannen 74b0bb105e Merge branch '6.1.x' 2024-08-05 11:58:49 +03:00
Sam Brannen 8badae9d72 Convert primitive array to Object[] when invoking varargs method in SpEL
Prior to this commit, SpEL incorrectly wrapped a primitive array in an
Object[] array when invoking Object[] varargs methods and constructors.

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

- 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 convert that
  array to the varargs array type.

Closes gh-33317
2024-08-05 11:56:48 +03:00
Sam Brannen 46a4ad4a75 Merge branch '6.1.x' 2024-08-05 11:07:47 +03:00
Sam Brannen 444e7eeb74 Enable test for primitive array to Object[] conversion in SpEL
See gh-33212
2024-08-05 11:05:53 +03:00
Sam Brannen e547313fa6 Sync MapAccessor implementations 2024-07-23 18:27:52 +03:00
Sam Brannen 6ca8fbc2da Merge ArgumentsMatchInfo into ArgumentsMatchKind in SpEL
The internal ArgumentsMatchInfo type seems to have once had additional
methods beyond the functionality provided by the ArgumentsMatchKind
enum; however, that is no longer the case. Consequently, there is no
need to maintain both types.

This commit therefore merges the convenience methods from
ArgumentsMatchInfo into the ArgumentsMatchKind enum and removes the
unnecessary ArgumentsMatchInfo type.
2024-07-13 14:34:24 +02:00
Sam Brannen 59b9404956 Specify nullability @⁠Contract for TypeDescriptor.array() 2024-07-12 18:02:26 +02:00
Sam Brannen 09d8e4458c Merge branch '6.1.x' 2024-07-12 17:37:51 +02:00
Sam Brannen ae5dd54115 Add @⁠Disabled tests for primitive varargs array to Object[] conversion 2024-07-12 17:36:54 +02:00
Sam Brannen e088892fc1 Support MethodHandle invocation with primitive varargs array in SpEL
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
2024-07-12 17:36:54 +02:00
Sam Brannen c93598835c Merge branch '6.1.x' 2024-07-11 18:31:37 +02:00
Sam Brannen 48a6bd6ce7 Polishing 2024-07-11 18:30:52 +02:00
Sam Brannen f7d4742ab8 Merge branch '6.1.x' 2024-07-10 16:52:08 +02:00
Sam Brannen fa2a58b9db 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
2024-07-10 16:46:47 +02:00
Sam Brannen 4de2aadf72 Merge branch '6.1.x' 2024-07-10 16:22:17 +02:00