|
19 | 19 | import java.lang.invoke.MethodHandle;
|
20 | 20 | import java.lang.invoke.MethodHandles;
|
21 | 21 | import java.lang.invoke.MethodType;
|
| 22 | +import java.lang.reflect.Method; |
22 | 23 | import java.util.ArrayList;
|
23 | 24 | import java.util.Date;
|
24 | 25 | import java.util.GregorianCalendar;
|
|
39 | 40 | import org.springframework.expression.spel.support.StandardEvaluationContext;
|
40 | 41 | import org.springframework.expression.spel.testresources.Inventor;
|
41 | 42 | import org.springframework.expression.spel.testresources.PlaceOfBirth;
|
| 43 | +import org.springframework.util.ReflectionUtils; |
42 | 44 |
|
43 | 45 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 46 | import static org.assertj.core.api.Assertions.within;
|
@@ -554,45 +556,51 @@ void thisAndRootVariables() {
|
554 | 556 | @Nested
|
555 | 557 | class Functions {
|
556 | 558 |
|
| 559 | + private final ExpressionParser parser = new SpelExpressionParser(); |
| 560 | + |
| 561 | + private final StandardEvaluationContext context = new StandardEvaluationContext(); |
| 562 | + |
| 563 | + private final Method reverseStringMethod = ReflectionUtils.findMethod(StringUtils.class, "reverseString", String.class); |
| 564 | + |
557 | 565 | @Test
|
558 |
| - void functions() throws Exception { |
559 |
| - ExpressionParser parser = new SpelExpressionParser(); |
560 |
| - StandardEvaluationContext context = new StandardEvaluationContext(); |
561 |
| - context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", String.class)); |
| 566 | + void registerFunctionViaMethodWithStandardEvaluationContext() { |
| 567 | + context.registerFunction("reverseString", reverseStringMethod); |
562 | 568 |
|
563 |
| - String helloWorldReversed = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class); |
564 |
| - assertThat(helloWorldReversed).isEqualTo("dlrow olleh"); |
| 569 | + String result = parser.parseExpression("#reverseString('hello world')").getValue(context, String.class); |
| 570 | + assertThat(result).isEqualTo("dlrow olleh"); |
565 | 571 | }
|
566 | 572 |
|
567 | 573 | @Test
|
568 |
| - void methodHandlesNotBound() throws Exception { |
569 |
| - ExpressionParser parser = new SpelExpressionParser(); |
570 |
| - StandardEvaluationContext context = new StandardEvaluationContext(); |
| 574 | + void registerFunctionViaMethodWithSimpleEvaluationContext() { |
| 575 | + SimpleEvaluationContext simpleContext = SimpleEvaluationContext.forReadOnlyDataBinding().build(); |
| 576 | + simpleContext.setVariable("reverseString", reverseStringMethod); |
571 | 577 |
|
572 |
| - MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted", |
| 578 | + String result = parser.parseExpression("#reverseString('hello world')").getValue(simpleContext, String.class); |
| 579 | + assertThat(result).isEqualTo("dlrow olleh"); |
| 580 | + } |
| 581 | + |
| 582 | + @Test |
| 583 | + void registerFunctionViaMethodHandleNotBound() throws Exception { |
| 584 | + MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted", |
573 | 585 | MethodType.methodType(String.class, Object[].class));
|
574 |
| - context.setVariable("message", mh); |
| 586 | + context.registerFunction("message", methodHandle); |
575 | 587 |
|
576 | 588 | String message = parser.parseExpression("#message('Simple message: <%s>', 'Hello World', 'ignored')")
|
577 | 589 | .getValue(context, String.class);
|
578 | 590 | assertThat(message).isEqualTo("Simple message: <Hello World>");
|
579 | 591 | }
|
580 | 592 |
|
581 | 593 | @Test
|
582 |
| - void methodHandlesFullyBound() throws Throwable { |
583 |
| - ExpressionParser parser = new SpelExpressionParser(); |
584 |
| - StandardEvaluationContext context = new StandardEvaluationContext(); |
585 |
| - |
| 594 | + void registerFunctionViaMethodHandleFullyBound() throws Exception { |
586 | 595 | String template = "This is a %s message with %s words: <%s>";
|
587 | 596 | Object varargs = new Object[] { "prerecorded", 3, "Oh Hello World!", "ignored" };
|
588 |
| - MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "formatted", |
| 597 | + MethodHandle methodHandle = MethodHandles.lookup().findVirtual(String.class, "formatted", |
589 | 598 | MethodType.methodType(String.class, Object[].class))
|
590 | 599 | .bindTo(template)
|
591 |
| - .bindTo(varargs); //here we have to provide arguments in a single array binding |
592 |
| - context.setVariable("message", mh); |
| 600 | + .bindTo(varargs); // here we have to provide arguments in a single array binding |
| 601 | + context.registerFunction("message", methodHandle); |
593 | 602 |
|
594 |
| - String message = parser.parseExpression("#message()") |
595 |
| - .getValue(context, String.class); |
| 603 | + String message = parser.parseExpression("#message()").getValue(context, String.class); |
596 | 604 | assertThat(message).isEqualTo("This is a prerecorded message with 3 words: <Oh Hello World!>");
|
597 | 605 | }
|
598 | 606 | }
|
|
0 commit comments