Merge branch '6.2.x'

This commit is contained in:
Sam Brannen 2024-11-18 11:41:07 +01:00
commit e34189a793
6 changed files with 29 additions and 20 deletions

View File

@ -12,27 +12,27 @@ Java::
+ +
[source,java,indent=0,subs="verbatim,quotes"] [source,java,indent=0,subs="verbatim,quotes"]
---- ----
Inventor einstein = p.parseExpression( Inventor einstein = parser.parseExpression(
"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')") "new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
.getValue(Inventor.class); .getValue(Inventor.class);
// create new Inventor instance within the add() method of List // create new Inventor instance within the add() method of List
p.parseExpression( parser.parseExpression(
"Members.add(new org.spring.samples.spel.inventor.Inventor( "Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))")
'Albert Einstein', 'German'))").getValue(societyContext); .getValue(societyContext);
---- ----
Kotlin:: Kotlin::
+ +
[source,kotlin,indent=0,subs="verbatim,quotes"] [source,kotlin,indent=0,subs="verbatim,quotes"]
---- ----
val einstein = p.parseExpression( val einstein = parser.parseExpression(
"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')") "new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
.getValue(Inventor::class.java) .getValue(Inventor::class.java)
// create new Inventor instance within the add() method of List // create new Inventor instance within the add() method of List
p.parseExpression( parser.parseExpression(
"Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))") "Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))")
.getValue(societyContext) .getValue(societyContext)
---- ----
====== ======

View File

@ -110,8 +110,8 @@ potentially more efficient use cases if the `MethodHandle` target and parameters
been fully bound prior to registration; however, partially bound handles are also been fully bound prior to registration; however, partially bound handles are also
supported. supported.
Consider the `String#formatted(String, Object...)` instance method, which produces a Consider the `String#formatted(Object...)` instance method, which produces a message
message according to a template and a variable number of arguments. according to a template and a variable number of arguments.
You can register and use the `formatted` method as a `MethodHandle`, as the following You can register and use the `formatted` method as a `MethodHandle`, as the following
example shows: example shows:
@ -151,10 +151,10 @@ Kotlin::
---- ----
====== ======
As hinted above, binding a `MethodHandle` and registering the bound `MethodHandle` is also As mentioned above, binding a `MethodHandle` and registering the bound `MethodHandle` is
supported. This is likely to be more performant if both the target and all the arguments also supported. This is likely to be more performant if both the target and all the
are bound. In that case no arguments are necessary in the SpEL expression, as the arguments are bound. In that case no arguments are necessary in the SpEL expression, as
following example shows: the following example shows:
[tabs] [tabs]
====== ======
@ -168,9 +168,10 @@ Java::
String template = "This is a %s message with %s words: <%s>"; String template = "This is a %s message with %s words: <%s>";
Object varargs = new Object[] { "prerecorded", 3, "Oh Hello World!", "ignored" }; Object varargs = new Object[] { "prerecorded", 3, "Oh Hello World!", "ignored" };
MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted", MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted",
MethodType.methodType(String.class, Object[].class)) MethodType.methodType(String.class, Object[].class))
.bindTo(template) .bindTo(template)
.bindTo(varargs); //here we have to provide arguments in a single array binding // Here we have to provide the arguments in a single array binding:
.bindTo(varargs);
context.setVariable("message", mh); context.setVariable("message", mh);
// evaluates to "This is a prerecorded message with 3 words: <Oh Hello World!>" // evaluates to "This is a prerecorded message with 3 words: <Oh Hello World!>"
@ -189,9 +190,10 @@ Kotlin::
val varargs = arrayOf("prerecorded", 3, "Oh Hello World!", "ignored") val varargs = arrayOf("prerecorded", 3, "Oh Hello World!", "ignored")
val mh = MethodHandles.lookup().findVirtual(String::class.java, "formatted", val mh = MethodHandles.lookup().findVirtual(String::class.java, "formatted",
MethodType.methodType(String::class.java, Array<Any>::class.java)) MethodType.methodType(String::class.java, Array<Any>::class.java))
.bindTo(template) .bindTo(template)
.bindTo(varargs) //here we have to provide arguments in a single array binding // Here we have to provide the arguments in a single array binding:
.bindTo(varargs)
context.setVariable("message", mh) context.setVariable("message", mh)
// evaluates to "This is a prerecorded message with 3 words: <Oh Hello World!>" // evaluates to "This is a prerecorded message with 3 words: <Oh Hello World!>"

View File

@ -36,6 +36,8 @@ import static org.assertj.core.api.Assertions.assertThatException;
* Tests invocation of constructors. * Tests invocation of constructors.
* *
* @author Andy Clement * @author Andy Clement
* @see MethodInvocationTests
* @see VariableAndFunctionTests
*/ */
class ConstructorInvocationTests extends AbstractExpressionTests { class ConstructorInvocationTests extends AbstractExpressionTests {

View File

@ -48,6 +48,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Andy Clement * @author Andy Clement
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen * @author Sam Brannen
* @see ConstructorInvocationTests
* @see VariableAndFunctionTests
*/ */
class MethodInvocationTests extends AbstractExpressionTests { class MethodInvocationTests extends AbstractExpressionTests {

View File

@ -658,7 +658,8 @@ class SpelDocumentationTests extends AbstractExpressionTests {
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted", MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted",
MethodType.methodType(String.class, Object[].class)) MethodType.methodType(String.class, Object[].class))
.bindTo(template) .bindTo(template)
.bindTo(varargs); // here we have to provide arguments in a single array binding // Here we have to provide the arguments in a single array binding:
.bindTo(varargs);
context.registerFunction("message", methodHandle); context.registerFunction("message", methodHandle);
String message = parser.parseExpression("#message()").getValue(context, String.class); String message = parser.parseExpression("#message()").getValue(context, String.class);

View File

@ -32,6 +32,8 @@ import static org.springframework.expression.spel.SpelMessage.INCORRECT_NUMBER_O
* *
* @author Andy Clement * @author Andy Clement
* @author Sam Brannen * @author Sam Brannen
* @see ConstructorInvocationTests
* @see MethodInvocationTests
*/ */
class VariableAndFunctionTests extends AbstractExpressionTests { class VariableAndFunctionTests extends AbstractExpressionTests {