Skip to content

Commit 58b07f5

Browse files
committed
SpEL passes full collection type context to ConversionService (SPR-7410)
1 parent 7cddb1c commit 58b07f5

File tree

7 files changed

+199
-134
lines changed

7 files changed

+199
-134
lines changed

org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.expression.spel.ast;
1818

19-
import java.lang.reflect.InvocationTargetException;
2019
import java.lang.reflect.Method;
2120
import java.lang.reflect.Modifier;
2221

@@ -37,9 +36,10 @@
3736
* function definition in an expression: "(#max = {|x,y|$x>$y?$x:$y};max(2,3))" Calling context defined function:
3837
* "#isEven(37)". Functions may also be static java methods, registered in the context prior to invocation of the
3938
* expression.
40-
*
41-
* Functions are very simplistic, the arguments are not part of the definition (right now), so the names must be unique.
42-
*
39+
*
40+
* <p>Functions are very simplistic, the arguments are not part of the definition (right now),
41+
* so the names must be unique.
42+
*
4343
* @author Andy Clement
4444
* @since 3.0
4545
*/
@@ -65,7 +65,8 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
6565
}
6666
try {
6767
return executeFunctionJLRMethod(state, (Method) o.getValue());
68-
} catch (SpelEvaluationException se) {
68+
}
69+
catch (SpelEvaluationException se) {
6970
se.setPosition(getStartPosition());
7071
throw se;
7172
}
@@ -79,42 +80,37 @@ public TypedValue getValueInternal(ExpressionState state) throws EvaluationExcep
7980
* @return the return value of the invoked Java method
8081
* @throws EvaluationException if there is any problem invoking the method
8182
*/
82-
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method m) throws EvaluationException {
83+
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
8384
Object[] functionArgs = getArguments(state);
8485

85-
if (!m.isVarArgs() && m.getParameterTypes().length != functionArgs.length) {
86-
throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, m
87-
.getParameterTypes().length);
86+
if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
87+
throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
88+
functionArgs.length, method.getParameterTypes().length);
8889
}
8990
// Only static methods can be called in this way
90-
if (!Modifier.isStatic(m.getModifiers())) {
91-
throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, m
91+
if (!Modifier.isStatic(method.getModifiers())) {
92+
throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, method
9293
.getDeclaringClass().getName()
93-
+ "." + m.getName(), name);
94+
+ "." + method.getName(), name);
9495
}
9596

9697
// Convert arguments if necessary and remap them for varargs if required
9798
if (functionArgs != null) {
9899
TypeConverter converter = state.getEvaluationContext().getTypeConverter();
99-
ReflectionHelper.convertAllArguments(m.getParameterTypes(), m.isVarArgs(), converter, functionArgs);
100+
ReflectionHelper.convertAllArguments(converter, functionArgs, method);
100101
}
101-
if (m.isVarArgs()) {
102-
functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(m.getParameterTypes(), functionArgs);
102+
if (method.isVarArgs()) {
103+
functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
103104
}
104105

105106
try {
106-
ReflectionUtils.makeAccessible(m);
107-
Object result = m.invoke(m.getClass(), functionArgs);
108-
return new TypedValue(result, new TypeDescriptor(new MethodParameter(m,-1)));
109-
} catch (IllegalArgumentException e) {
110-
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
111-
.getMessage());
112-
} catch (IllegalAccessException e) {
113-
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
114-
.getMessage());
115-
} catch (InvocationTargetException e) {
116-
throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
117-
.getMessage());
107+
ReflectionUtils.makeAccessible(method);
108+
Object result = method.invoke(method.getClass(), functionArgs);
109+
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method,-1)));
110+
}
111+
catch (Exception ex) {
112+
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
113+
this.name, ex.getMessage());
118114
}
119115
}
120116

org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.springframework.expression.spel.support;
1818

1919
import java.lang.reflect.Array;
20+
import java.lang.reflect.Method;
2021
import java.util.ArrayList;
2122
import java.util.List;
2223

24+
import org.springframework.core.MethodParameter;
2325
import org.springframework.core.convert.TypeDescriptor;
2426
import org.springframework.expression.EvaluationException;
2527
import org.springframework.expression.TypeConverter;
@@ -29,7 +31,7 @@
2931
import org.springframework.util.ClassUtils;
3032

3133
/**
32-
* Utility methods used by the reflection resolver code to discover the appropriae
34+
* Utility methods used by the reflection resolver code to discover the appropriate
3335
* methods/constructors and fields that should be used in expressions.
3436
*
3537
* @author Andy Clement
@@ -39,15 +41,15 @@
3941
public class ReflectionHelper {
4042

4143
/**
42-
* Compare argument arrays and return information about whether they match. A supplied type converter and
43-
* conversionAllowed flag allow for matches to take into account that a type may be transformed into a different
44-
* type by the converter.
44+
* Compare argument arrays and return information about whether they match. A supplied type converter
45+
* and conversionAllowed flag allow for matches to take into account that a type may be transformed
46+
* into a different type by the converter.
4547
* @param expectedArgTypes the array of types the method/constructor is expecting
4648
* @param suppliedArgTypes the array of types that are being supplied at the point of invocation
4749
* @param typeConverter a registered type converter
4850
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match
4951
*/
50-
public static ArgumentsMatchInfo compareArguments(
52+
static ArgumentsMatchInfo compareArguments(
5153
Class[] expectedArgTypes, Class[] suppliedArgTypes, TypeConverter typeConverter) {
5254

5355
Assert.isTrue(expectedArgTypes.length == suppliedArgTypes.length,
@@ -110,11 +112,13 @@ else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
110112
* @param typeConverter a registered type converter
111113
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match
112114
*/
113-
public static ArgumentsMatchInfo compareArgumentsVarargs(
115+
static ArgumentsMatchInfo compareArgumentsVarargs(
114116
Class[] expectedArgTypes, Class[] suppliedArgTypes, TypeConverter typeConverter) {
115117

116-
Assert.isTrue(expectedArgTypes!=null && expectedArgTypes.length>0, "Expected arguments must at least include one array (the vargargs parameter)");
117-
Assert.isTrue(expectedArgTypes[expectedArgTypes.length-1].isArray(), "Final expected argument should be array type (the varargs parameter)");
118+
Assert.isTrue(expectedArgTypes != null && expectedArgTypes.length > 0,
119+
"Expected arguments must at least include one array (the vargargs parameter)");
120+
Assert.isTrue(expectedArgTypes[expectedArgTypes.length - 1].isArray(),
121+
"Final expected argument should be array type (the varargs parameter)");
118122

119123
ArgsMatchKind match = ArgsMatchKind.EXACT;
120124
List<Integer> argsRequiringConversion = null;
@@ -214,76 +218,67 @@ else if (typeConverter.canConvert(suppliedArg, varargsParameterType)) {
214218
}
215219

216220
/**
217-
* Takes an input set of argument values and, following the positions specified in the int array, it converts
218-
* them to the types specified as the required parameter types. The arguments are converted 'in-place' in the
219-
* input array.
220-
* @param requiredParameterTypes the types that the caller would like to have
221-
* @param isVarargs whether the requiredParameterTypes is a varargs list
221+
* Takes an input set of argument values and, following the positions specified in the int array,
222+
* it converts them to the types specified as the required parameter types. The arguments are
223+
* converted 'in-place' in the input array.
222224
* @param converter the type converter to use for attempting conversions
223-
* @param argumentsRequiringConversion details which of the input arguments need conversion
224225
* @param arguments the actual arguments that need conversion
226+
* @param methodOrCtor the target Method or Constructor
227+
* @param argumentsRequiringConversion details which of the input arguments need conversion
228+
* @param varargsPosition the known position of the varargs argument, if any
225229
* @throws EvaluationException if a problem occurs during conversion
226230
*/
227-
public static void convertArguments(Class[] requiredParameterTypes, boolean isVarargs, TypeConverter converter,
228-
int[] argumentsRequiringConversion, Object[] arguments) throws EvaluationException {
229-
230-
Assert.notNull(argumentsRequiringConversion,"should not be called if no conversions required");
231-
Assert.notNull(arguments,"should not be called if no conversions required");
232-
233-
Class varargsType = null;
234-
if (isVarargs) {
235-
Assert.isTrue(requiredParameterTypes[requiredParameterTypes.length-1].isArray(),"if varargs then last parameter type must be array");
236-
varargsType = requiredParameterTypes[requiredParameterTypes.length - 1].getComponentType();
237-
}
238-
for (Integer argPosition : argumentsRequiringConversion) {
239-
Class<?> targetType = null;
240-
if (isVarargs && argPosition >= (requiredParameterTypes.length - 1)) {
241-
targetType = varargsType;
231+
static void convertArguments(TypeConverter converter, Object[] arguments, Object methodOrCtor,
232+
int[] argumentsRequiringConversion, Integer varargsPosition) throws EvaluationException {
233+
234+
for (int argPosition : argumentsRequiringConversion) {
235+
TypeDescriptor targetType;
236+
if (varargsPosition != null && argPosition >= varargsPosition) {
237+
MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, varargsPosition);
238+
targetType = new TypeDescriptor(methodParam, methodParam.getParameterType().getComponentType());
242239
}
243240
else {
244-
targetType = requiredParameterTypes[argPosition];
241+
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
245242
}
246-
arguments[argPosition] = converter.convertValue(arguments[argPosition], TypeDescriptor.forObject(arguments[argPosition]), TypeDescriptor.valueOf(targetType));
243+
arguments[argPosition] = converter.convertValue(
244+
arguments[argPosition], TypeDescriptor.forObject(arguments[argPosition]), targetType);
247245
}
248246
}
249247

250248
/**
251-
* Convert a supplied set of arguments into the requested types. If the parameterTypes are related to
249+
* Convert a supplied set of arguments into the requested types. If the parameterTypes are related to
252250
* a varargs method then the final entry in the parameterTypes array is going to be an array itself whose
253251
* component type should be used as the conversion target for extraneous arguments. (For example, if the
254252
* parameterTypes are {Integer, String[]} and the input arguments are {Integer, boolean, float} then both
255-
* the boolean and float must be converted to strings). This method does not repackage the arguments
253+
* the boolean and float must be converted to strings). This method does not repackage the arguments
256254
* into a form suitable for the varargs invocation
257-
* @param parameterTypes the types to be converted to
258-
* @param isVarargs whether parameterTypes relates to a varargs method
259255
* @param converter the converter to use for type conversions
260256
* @param arguments the arguments to convert to the requested parameter types
257+
* @param method the target Method
261258
* @throws SpelEvaluationException if there is a problem with conversion
262259
*/
263-
public static void convertAllArguments(Class[] parameterTypes, boolean isVarargs, TypeConverter converter,
264-
Object[] arguments) throws SpelEvaluationException {
265-
266-
Assert.notNull(arguments,"should not be called if nothing to convert");
267-
268-
Class varargsType = null;
269-
if (isVarargs) {
270-
Assert.isTrue(parameterTypes[parameterTypes.length-1].isArray(),"if varargs then last parameter type must be array");
271-
varargsType = parameterTypes[parameterTypes.length - 1].getComponentType();
260+
public static void convertAllArguments(TypeConverter converter, Object[] arguments, Method method) throws SpelEvaluationException {
261+
Integer varargsPosition = null;
262+
if (method.isVarArgs()) {
263+
Class[] paramTypes = method.getParameterTypes();
264+
varargsPosition = paramTypes.length - 1;
272265
}
273-
for (int i = 0; i < arguments.length; i++) {
274-
Class<?> targetType = null;
275-
if (isVarargs && i >= (parameterTypes.length - 1)) {
276-
targetType = varargsType;
266+
for (int argPosition = 0; argPosition < arguments.length; argPosition++) {
267+
TypeDescriptor targetType;
268+
if (varargsPosition != null && argPosition >= varargsPosition) {
269+
MethodParameter methodParam = new MethodParameter(method, varargsPosition);
270+
targetType = new TypeDescriptor(methodParam, methodParam.getParameterType().getComponentType());
277271
}
278272
else {
279-
targetType = parameterTypes[i];
273+
targetType = new TypeDescriptor(new MethodParameter(method, argPosition));
280274
}
281275
try {
282-
if (arguments[i] != null && arguments[i].getClass() != targetType) {
276+
Object argument = arguments[argPosition];
277+
if (argument != null && !targetType.getObjectType().isInstance(argument)) {
283278
if (converter == null) {
284-
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, arguments[i].getClass().getName(),targetType);
279+
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
285280
}
286-
arguments[i] = converter.convertValue(arguments[i], TypeDescriptor.forObject(arguments[i]), TypeDescriptor.valueOf(targetType));
281+
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
287282
}
288283
}
289284
catch (EvaluationException ex) {
@@ -292,7 +287,7 @@ public static void convertAllArguments(Class[] parameterTypes, boolean isVarargs
292287
throw (SpelEvaluationException)ex;
293288
}
294289
else {
295-
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,arguments[i].getClass().getName(),targetType);
290+
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,arguments[argPosition].getClass().getName(), targetType);
296291
}
297292
}
298293
}
@@ -313,14 +308,15 @@ public static Object[] setupArgumentsForVarargsInvocation(Class[] requiredParame
313308
int argumentCount = args.length;
314309

315310
// Check if repackaging is needed:
316-
if (parameterCount != args.length || requiredParameterTypes[parameterCount - 1] != (args[argumentCount - 1] == null ? null : args[argumentCount - 1].getClass())) {
311+
if (parameterCount != args.length ||
312+
requiredParameterTypes[parameterCount - 1] !=
313+
(args[argumentCount - 1] == null ? null : args[argumentCount - 1].getClass())) {
317314
int arraySize = 0; // zero size array if nothing to pass as the varargs parameter
318315
if (argumentCount >= parameterCount) {
319316
arraySize = argumentCount - (parameterCount - 1);
320317
}
321318
Object[] repackagedArguments = (Object[]) Array.newInstance(requiredParameterTypes[parameterCount - 1].getComponentType(),
322319
arraySize);
323-
324320
// Copy all but the varargs arguments
325321
for (int i = 0; i < arraySize; i++) {
326322
repackagedArguments[i] = args[parameterCount + i - 1];

org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectiveConstructorExecutor.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,36 +29,43 @@
2929
* A simple ConstructorExecutor implementation that runs a constructor using reflective invocation.
3030
*
3131
* @author Andy Clement
32+
* @author Juergen Hoeller
3233
* @since 3.0
3334
*/
3435
class ReflectiveConstructorExecutor implements ConstructorExecutor {
3536

3637
private final Constructor<?> ctor;
3738

39+
private final Integer varargsPosition;
40+
3841
// When the constructor was found, we will have determined if arguments need to be converted for it
3942
// to be invoked. Conversion won't be cheap so let's only do it if necessary.
4043
private final int[] argsRequiringConversion;
4144

4245

4346
public ReflectiveConstructorExecutor(Constructor<?> ctor, int[] argsRequiringConversion) {
4447
this.ctor = ctor;
48+
if (ctor.isVarArgs()) {
49+
Class[] paramTypes = ctor.getParameterTypes();
50+
this.varargsPosition = paramTypes.length - 1;
51+
}
52+
else {
53+
this.varargsPosition = null;
54+
}
4555
this.argsRequiringConversion = argsRequiringConversion;
4656
}
4757

4858
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
4959
try {
50-
if (argsRequiringConversion != null && arguments != null) {
51-
ReflectionHelper.convertArguments(this.ctor.getParameterTypes(),
52-
this.ctor.isVarArgs(), context.getTypeConverter(),
53-
this.argsRequiringConversion, arguments);
60+
if (this.argsRequiringConversion != null && arguments != null) {
61+
ReflectionHelper.convertArguments(context.getTypeConverter(), arguments,
62+
this.ctor, this.argsRequiringConversion, this.varargsPosition);
5463
}
5564
if (this.ctor.isVarArgs()) {
56-
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(
57-
this.ctor.getParameterTypes(), arguments);
65+
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.ctor.getParameterTypes(), arguments);
5866
}
5967
ReflectionUtils.makeAccessible(this.ctor);
60-
return new TypedValue(this.ctor.newInstance(arguments),
61-
TypeDescriptor.valueOf(this.ctor.getDeclaringClass()));
68+
return new TypedValue(this.ctor.newInstance(arguments), TypeDescriptor.valueOf(this.ctor.getDeclaringClass()));
6269
}
6370
catch (Exception ex) {
6471
throw new AccessException("Problem invoking constructor: " + this.ctor, ex);

0 commit comments

Comments
 (0)