|
| 1 | +[[expressions-varargs]] |
| 2 | += Varargs Invocations |
| 3 | + |
| 4 | +The Spring Expression Language supports |
| 5 | +https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html[varargs] |
| 6 | +invocations for xref:core/expressions/language-ref/constructors.adoc[constructors], |
| 7 | +xref:core/expressions/language-ref/methods.adoc[methods], and user-defined |
| 8 | +xref:core/expressions/language-ref/functions.adoc[functions]. |
| 9 | + |
| 10 | +The following example shows how to invoke the `java.lang.String#formatted(Object...)` |
| 11 | +_varargs_ method within an expression by supplying the variable argument list as separate |
| 12 | +arguments (`'blue', 1`). |
| 13 | + |
| 14 | +[tabs] |
| 15 | +====== |
| 16 | +Java:: |
| 17 | ++ |
| 18 | +[source,java,indent=0,subs="verbatim,quotes"] |
| 19 | +---- |
| 20 | + // evaluates to "blue is color #1" |
| 21 | + String expression = "'%s is color #%d'.formatted('blue', 1)"; |
| 22 | + String message = parser.parseExpression(expression).getValue(String.class); |
| 23 | +---- |
| 24 | +
|
| 25 | +Kotlin:: |
| 26 | ++ |
| 27 | +[source,kotlin,indent=0,subs="verbatim,quotes"] |
| 28 | +---- |
| 29 | + // evaluates to "blue is color #1" |
| 30 | + val expression = "'%s is color #%d'.formatted('blue', 1)" |
| 31 | + val message = parser.parseExpression(expression).getValue(String::class.java) |
| 32 | +---- |
| 33 | +====== |
| 34 | + |
| 35 | +A variable argument list can also be supplied as an array, as demonstrated in the |
| 36 | +following example (`new Object[] {'blue', 1}`). |
| 37 | + |
| 38 | +[tabs] |
| 39 | +====== |
| 40 | +Java:: |
| 41 | ++ |
| 42 | +[source,java,indent=0,subs="verbatim,quotes"] |
| 43 | +---- |
| 44 | + // evaluates to "blue is color #1" |
| 45 | + String expression = "'%s is color #%d'.formatted(new Object[] {'blue', 1})"; |
| 46 | + String message = parser.parseExpression(expression).getValue(String.class); |
| 47 | +---- |
| 48 | +
|
| 49 | +Kotlin:: |
| 50 | ++ |
| 51 | +[source,kotlin,indent=0,subs="verbatim,quotes"] |
| 52 | +---- |
| 53 | + // evaluates to "blue is color #1" |
| 54 | + val expression = "'%s is color #%d'.formatted(new Object[] {'blue', 1})" |
| 55 | + val message = parser.parseExpression(expression).getValue(String::class.java) |
| 56 | +---- |
| 57 | +====== |
| 58 | + |
| 59 | +As an alternative, a variable argument list can be supplied as a `java.util.List` – for |
| 60 | +example, as an xref:core/expressions/language-ref/inline-lists.adoc[inline list] |
| 61 | +(`{'blue', 1}`). The following example shows how to do that. |
| 62 | + |
| 63 | +[tabs] |
| 64 | +====== |
| 65 | +Java:: |
| 66 | ++ |
| 67 | +[source,java,indent=0,subs="verbatim,quotes"] |
| 68 | +---- |
| 69 | + // evaluates to "blue is color #1" |
| 70 | + String expression = "'%s is color #%d'.formatted({'blue', 1})"; |
| 71 | + String message = parser.parseExpression(expression).getValue(String.class); |
| 72 | +---- |
| 73 | +
|
| 74 | +Kotlin:: |
| 75 | ++ |
| 76 | +[source,kotlin,indent=0,subs="verbatim,quotes"] |
| 77 | +---- |
| 78 | + // evaluates to "blue is color #1" |
| 79 | + val expression = "'%s is color #%d'.formatted({'blue', 1})" |
| 80 | + val message = parser.parseExpression(expression).getValue(String::class.java) |
| 81 | +---- |
| 82 | +====== |
| 83 | + |
| 84 | +[[expressions-varargs-type-conversion]] |
| 85 | +== Varargs Type Conversion |
| 86 | + |
| 87 | +In contrast to the standard support for varargs invocations in Java, |
| 88 | +xref:core/expressions/evaluation.adoc#expressions-type-conversion[type conversion] may be |
| 89 | +applied to the individual arguments when invoking varargs constructors, methods, or |
| 90 | +functions in SpEL. |
| 91 | + |
| 92 | +For example, if we have registered a custom |
| 93 | +xref:core/expressions/language-ref/functions.adoc[function] in the `EvaluationContext` |
| 94 | +under the name `#reverseStrings` for a method with the signature |
| 95 | +`String reverseStrings(String... strings)`, we can invoke that function within a SpEL |
| 96 | +expression with any argument that can be converted to a `String`, as demonstrated in the |
| 97 | +following example. |
| 98 | + |
| 99 | +[tabs] |
| 100 | +====== |
| 101 | +Java:: |
| 102 | ++ |
| 103 | +[source,java,indent=0,subs="verbatim,quotes"] |
| 104 | +---- |
| 105 | + // evaluates to "3.0, 2.0, 1, SpEL" |
| 106 | + String expression = "#reverseStrings('SpEL', 1, 10F / 5, 3.0000)"; |
| 107 | + String message = parser.parseExpression(expression) |
| 108 | + .getValue(evaluationContext, String.class); |
| 109 | +---- |
| 110 | +
|
| 111 | +Kotlin:: |
| 112 | ++ |
| 113 | +[source,kotlin,indent=0,subs="verbatim,quotes"] |
| 114 | +---- |
| 115 | + // evaluates to "3.0, 2.0, 1, SpEL" |
| 116 | + val expression = "#reverseStrings('SpEL', 1, 10F / 5, 3.0000)" |
| 117 | + val message = parser.parseExpression(expression) |
| 118 | + .getValue(evaluationContext, String::class.java) |
| 119 | +---- |
| 120 | +====== |
| 121 | + |
| 122 | +Similarly, any array whose component type is a subtype of the required varargs type can |
| 123 | +be supplied as the variable argument list for a varargs invocation. For example, a |
| 124 | +`String[]` array can be supplied to a varargs invocation that accepts an `Object...` |
| 125 | +argument list. |
| 126 | + |
| 127 | +The following listing demonstrates that we can supply a `String[]` array to the |
| 128 | +`java.lang.String#formatted(Object...)` _varargs_ method. It also highlights that `1` |
| 129 | +will be automatically converted to `"1"`. |
| 130 | + |
| 131 | +[tabs] |
| 132 | +====== |
| 133 | +Java:: |
| 134 | ++ |
| 135 | +[source,java,indent=0,subs="verbatim,quotes"] |
| 136 | +---- |
| 137 | + // evaluates to "blue is color #1" |
| 138 | + String expression = "'%s is color #%s'.formatted(new String[] {'blue', 1})"; |
| 139 | + String message = parser.parseExpression(expression).getValue(String.class); |
| 140 | +---- |
| 141 | +
|
| 142 | +Kotlin:: |
| 143 | ++ |
| 144 | +[source,kotlin,indent=0,subs="verbatim,quotes"] |
| 145 | +---- |
| 146 | + // evaluates to "blue is color #1" |
| 147 | + val expression = "'%s is color #%s'.formatted(new String[] {'blue', 1})" |
| 148 | + val message = parser.parseExpression(expression).getValue(String::class.java) |
| 149 | +---- |
| 150 | +====== |
| 151 | + |
0 commit comments