pass full TypeDescriptor context through to ConversionService calls (SPR-7519)

This commit is contained in:
Juergen Hoeller
2010-09-08 17:26:02 +00:00
parent 6f69b7b752
commit c33df5977a
19 changed files with 334 additions and 178 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,25 +16,29 @@
package org.springframework.expression;
import java.util.List;
import org.springframework.core.convert.TypeDescriptor;
/**
* A constructor resolver attempts locate a constructor and returns a ConstructorExecutor that can be used to invoke
* that constructor. The ConstructorExecutor will be cached but if it 'goes stale' the resolvers will be called again.
*
*
* @author Andy Clement
* @since 3.0
*/
public interface ConstructorResolver {
/**
* Within the supplied context determine a suitable constructor on the supplied type that can handle the specified
* arguments. Return a ConstructorExecutor that can be used to invoke that constructor (or null if no constructor
* could be found).
* Within the supplied context determine a suitable constructor on the supplied type that can handle the
* specified arguments. Return a ConstructorExecutor that can be used to invoke that constructor
* (or <code>null</code> if no constructor could be found).
* @param context the current evaluation context
* @param typeName the type upon which to look for the constructor
* @param argumentTypes the arguments that the constructor must be able to handle
* @return a ConstructorExecutor that can invoke the constructor, or null if non found
*/
ConstructorExecutor resolve(EvaluationContext context, String typeName, Class<?>[] argumentTypes)
ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes)
throws AccessException;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,24 +16,29 @@
package org.springframework.expression;
import java.util.List;
import org.springframework.core.convert.TypeDescriptor;
/**
* A method resolver attempts locate a method and returns a command executor that can be used to invoke that method. The
* command executor will be cached but if it 'goes stale' the resolvers will be called again.
*
* A method resolver attempts locate a method and returns a command executor that can be used to invoke that method.
* The command executor will be cached but if it 'goes stale' the resolvers will be called again.
*
* @author Andy Clement
* @since 3.0
*/
public interface MethodResolver {
/**
* Within the supplied context determine a suitable method on the supplied object that can handle the specified
* arguments. Return a MethodExecutor that can be used to invoke that method (or null if no method
* could be found).
* Within the supplied context determine a suitable method on the supplied object that can handle the
* specified arguments. Return a MethodExecutor that can be used to invoke that method
* (or <code>null</code> if no method could be found).
* @param context the current evaluation context
* @param targetObject the object upon which the method is being called
* @param argumentTypes the arguments that the constructor must be able to handle
* @return a MethodExecutor that can invoke the method, or null if the method cannot be found
*/
MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, Class<?>[] argumentTypes) throws AccessException;
MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
List<TypeDescriptor> argumentTypes) throws AccessException;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,14 +30,6 @@ import org.springframework.core.convert.TypeDescriptor;
*/
public interface TypeConverter {
/**
* Return true if the type converter can convert the specified type to the desired target type.
* @param sourceType the type to be converted from
* @param targetType the type to be converted to
* @return true if that conversion can be performed
*/
boolean canConvert(Class<?> sourceType, Class<?> targetType);
/**
* Return true if the type converter can convert the specified type to the desired target type.
* @param sourceType a type descriptor that describes the source type

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.expression.spel.ast;
import java.lang.reflect.Array;
import java.util.List;
import java.util.ArrayList;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
@@ -48,14 +49,16 @@ import org.springframework.expression.spel.SpelNode;
*/
public class ConstructorReference extends SpelNodeImpl {
private boolean isArrayConstructor = false;
private SpelNodeImpl[] dimensions;
// TODO is this caching safe - passing the expression around will mean this executor is also being passed around
/**
* The cached executor that may be reused on subsequent evaluations.
*/
private volatile ConstructorExecutor cachedExecutor;
private boolean isArrayConstructor = false;
private SpelNodeImpl[] dimensions;
/**
* Create a constructor reference. The first argument is the type, the rest are the parameters to the constructor
@@ -76,40 +79,42 @@ public class ConstructorReference extends SpelNodeImpl {
this.dimensions = dimensions;
}
/**
* Implements getValue() - delegating to the code for building an array or a simple type.
*/
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
if (isArrayConstructor) {
if (this.isArrayConstructor) {
return createArray(state);
} else {
}
else {
return createNewInstance(state);
}
}
/**
* Create a new ordinary object and return it.
*
* @param state the expression state within which this expression is being evaluated
* @return the new object
* @throws EvaluationException if there is a problem creating the object
*/
private TypedValue createNewInstance(ExpressionState state) throws EvaluationException {
Object[] arguments = new Object[getChildCount() - 1];
Class<?>[] argumentTypes = new Class[getChildCount() - 1];
List<TypeDescriptor> argumentTypes = new ArrayList<TypeDescriptor>(getChildCount() - 1);
for (int i = 0; i < arguments.length; i++) {
TypedValue childValue = children[i + 1].getValueInternal(state);
TypedValue childValue = this.children[i + 1].getValueInternal(state);
Object value = childValue.getValue();
arguments[i] = value;
argumentTypes[i] = (value == null ? null : value.getClass());
argumentTypes.add(TypeDescriptor.forObject(value));
}
ConstructorExecutor executorToUse = this.cachedExecutor;
if (executorToUse != null) {
try {
return executorToUse.execute(state.getEvaluationContext(), arguments);
} catch (AccessException ae) {
}
catch (AccessException ae) {
// Two reasons this can occur:
// 1. the method invoked actually threw a real exception
// 2. the method invoked was not passed the arguments it expected and has become 'stale'
@@ -129,7 +134,7 @@ public class ConstructorReference extends SpelNodeImpl {
if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
} else {
String typename = (String) children[0].getValueInternal(state).getValue();
String typename = (String) this.children[0].getValueInternal(state).getValue();
throw new SpelEvaluationException(getStartPosition(), rootCause,
SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename, FormatHelper
.formatMethodForMessage("", argumentTypes));
@@ -142,13 +147,13 @@ public class ConstructorReference extends SpelNodeImpl {
}
// either there was no accessor or it no longer exists
String typename = (String) children[0].getValueInternal(state).getValue();
String typename = (String) this.children[0].getValueInternal(state).getValue();
executorToUse = findExecutorForConstructor(typename, argumentTypes, state);
try {
this.cachedExecutor = executorToUse;
TypedValue result = executorToUse.execute(state.getEvaluationContext(), arguments);
return result;
} catch (AccessException ae) {
return executorToUse.execute(state.getEvaluationContext(), arguments);
}
catch (AccessException ae) {
throw new SpelEvaluationException(getStartPosition(), ae, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM,
typename, FormatHelper.formatMethodForMessage("", argumentTypes));
@@ -158,14 +163,13 @@ public class ConstructorReference extends SpelNodeImpl {
/**
* Go through the list of registered constructor resolvers and see if any can find a constructor that takes the
* specified set of arguments.
*
* @param typename the type trying to be constructed
* @param argumentTypes the types of the arguments supplied that the constructor must take
* @param state the current state of the expression
* @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
* @throws SpelEvaluationException if there is a problem locating the constructor
*/
private ConstructorExecutor findExecutorForConstructor(String typename, Class<?>[] argumentTypes,
private ConstructorExecutor findExecutorForConstructor(String typename, List<TypeDescriptor> argumentTypes,
ExpressionState state) throws SpelEvaluationException {
EvaluationContext eContext = state.getEvaluationContext();
@@ -178,10 +182,11 @@ public class ConstructorReference extends SpelNodeImpl {
if (cEx != null) {
return cEx;
}
} catch (AccessException ex) {
}
catch (AccessException ex) {
throw new SpelEvaluationException(getStartPosition(), ex,
SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename, FormatHelper.formatMethodForMessage(
"", argumentTypes));
SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename,
FormatHelper.formatMethodForMessage("", argumentTypes));
}
}
}
@@ -208,13 +213,11 @@ public class ConstructorReference extends SpelNodeImpl {
/**
* Create an array and return it.
*
* @param state the expression state within which this expression is being evaluated
* @return the new array
* @throws EvaluationException if there is a problem creating the array
*/
private TypedValue createArray(ExpressionState state) throws EvaluationException {
// First child gives us the array type which will either be a primitive or reference type
Object intendedArrayType = getChild(0).getValue(state);
if (!(intendedArrayType instanceof String)) {
@@ -223,44 +226,44 @@ public class ConstructorReference extends SpelNodeImpl {
.formatClassNameForMessage(intendedArrayType.getClass()));
}
String type = (String) intendedArrayType;
Class<?> componentType = null;
Class<?> componentType;
TypeCode arrayTypeCode = TypeCode.forName(type);
if (arrayTypeCode == TypeCode.OBJECT) {
componentType = state.findType(type);
} else {
}
else {
componentType = arrayTypeCode.getType();
}
TypeDescriptor td = TypeDescriptor.valueOf(componentType);
Object newArray = null;
Object newArray;
if (!hasInitializer()) {
// Confirm all dimensions were specified (for example [3][][5] is missing the 2nd dimension)
for (int i = 0; i < dimensions.length; i++) {
if (dimensions[i] == null) {
for (SpelNodeImpl dimension : this.dimensions) {
if (dimension == null) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.MISSING_ARRAY_DIMENSION);
}
}
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
// Shortcut for 1 dimensional
if (dimensions.length == 1) {
TypedValue o = dimensions[0].getTypedValue(state);
if (this.dimensions.length == 1) {
TypedValue o = this.dimensions[0].getTypedValue(state);
int arraySize = ExpressionUtils.toInt(typeConverter, o);
newArray = Array.newInstance(componentType, arraySize);
} else {
}
else {
// Multi-dimensional - hold onto your hat!
int[] dims = new int[dimensions.length];
for (int d = 0; d < dimensions.length; d++) {
TypedValue o = dimensions[d].getTypedValue(state);
int[] dims = new int[this.dimensions.length];
for (int d = 0; d < this.dimensions.length; d++) {
TypedValue o = this.dimensions[d].getTypedValue(state);
dims[d] = ExpressionUtils.toInt(typeConverter, o);
}
newArray = Array.newInstance(componentType, dims);
}
} else {
}
else {
// There is an initializer
if (dimensions.length > 1) {
if (this.dimensions.length > 1) {
// There is an initializer but this is a multi-dimensional array (e.g. new int[][]{{1,2},{3,4}}) - this
// is not currently supported
throw new SpelEvaluationException(getStartPosition(),
@@ -269,8 +272,8 @@ public class ConstructorReference extends SpelNodeImpl {
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
InlineList initializer = (InlineList) getChild(1);
// If a dimension was specified, check it matches the initializer length
if (dimensions[0] != null) {
TypedValue dValue = dimensions[0].getTypedValue(state);
if (this.dimensions[0] != null) {
TypedValue dValue = this.dimensions[0].getTypedValue(state);
int i = ExpressionUtils.toInt(typeConverter, dValue);
if (i != initializer.getChildCount()) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.INITIALIZER_LENGTH_INCORRECT);
@@ -281,27 +284,35 @@ public class ConstructorReference extends SpelNodeImpl {
newArray = Array.newInstance(componentType, arraySize);
if (arrayTypeCode == TypeCode.OBJECT) {
populateReferenceTypeArray(state, newArray, typeConverter, initializer, componentType);
} else if (arrayTypeCode == TypeCode.INT) {
}
else if (arrayTypeCode == TypeCode.INT) {
populateIntArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.BOOLEAN) {
}
else if (arrayTypeCode == TypeCode.BOOLEAN) {
populateBooleanArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.CHAR) {
}
else if (arrayTypeCode == TypeCode.CHAR) {
populateCharArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.LONG) {
}
else if (arrayTypeCode == TypeCode.LONG) {
populateLongArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.SHORT) {
}
else if (arrayTypeCode == TypeCode.SHORT) {
populateShortArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.DOUBLE) {
}
else if (arrayTypeCode == TypeCode.DOUBLE) {
populateDoubleArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.FLOAT) {
}
else if (arrayTypeCode == TypeCode.FLOAT) {
populateFloatArray(state, newArray, typeConverter, initializer);
} else if (arrayTypeCode == TypeCode.BYTE) {
}
else if (arrayTypeCode == TypeCode.BYTE) {
populateByteArray(state, newArray, typeConverter, initializer);
} else {
}
else {
throw new IllegalStateException(arrayTypeCode.name());
}
}
return new TypedValue(newArray, td);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
package org.springframework.expression.spel.ast;
import java.util.List;
import org.springframework.core.convert.TypeDescriptor;
/**
* Utility methods (formatters, etc) used during parsing and evaluation.
*
@@ -29,15 +33,15 @@ public class FormatHelper {
* @param argumentTypes the types of the arguments to the method
* @return nicely formatted string, eg. foo(String,int)
*/
public static String formatMethodForMessage(String name, Class<?>... argumentTypes) {
public static String formatMethodForMessage(String name, List<TypeDescriptor> argumentTypes) {
StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("(");
for (int i = 0; i < argumentTypes.length; i++) {
for (int i = 0; i < argumentTypes.size(); i++) {
if (i > 0) {
sb.append(",");
}
sb.append(formatClassNameForMessage(argumentTypes[i]));
sb.append(formatClassNameForMessage(argumentTypes.get(i).getType()));
}
sb.append(")");
return sb.toString();

View File

@@ -16,8 +16,10 @@
package org.springframework.expression.spel.ast;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
@@ -38,12 +40,14 @@ public class MethodReference extends SpelNodeImpl {
private final String name;
private volatile MethodExecutor cachedExecutor;
private final boolean nullSafe;
private volatile MethodExecutor cachedExecutor;
public MethodReference(boolean nullSafe, String methodName, int pos, SpelNodeImpl... arguments) {
super(pos,arguments);
name = methodName;
this.name = methodName;
this.nullSafe = nullSafe;
}
@@ -58,14 +62,16 @@ public class MethodReference extends SpelNodeImpl {
try {
state.pushActiveContextObject(state.getRootContextObject());
arguments[i] = children[i].getValueInternal(state).getValue();
} finally {
}
finally {
state.popActiveContextObject();
}
}
if (currentContext.getValue() == null) {
if (nullSafe) {
if (this.nullSafe) {
return TypedValue.NULL;
} else {
}
else {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
FormatHelper.formatMethodForMessage(name, getTypes(arguments)));
}
@@ -123,20 +129,22 @@ public class MethodReference extends SpelNodeImpl {
// User exception was the root cause - exit now
if (rootCause instanceof RuntimeException) {
throw (RuntimeException)rootCause;
} else {
throw new ExpressionInvocationTargetException( getStartPosition(),
"A problem occurred when trying to execute method '"+this.name+"' on object of type '"+state.getActiveContextObject().getValue().getClass().getName()+"'",
}
else {
throw new ExpressionInvocationTargetException( getStartPosition(),
"A problem occurred when trying to execute method '" + this.name +
"' on object of type '" + state.getActiveContextObject().getValue().getClass().getName() + "'",
rootCause);
}
}
}
private Class<?>[] getTypes(Object... arguments) {
Class<?>[] argumentTypes = new Class[arguments.length];
for (int i = 0; i < arguments.length; i++) {
argumentTypes[i] = (arguments[i]==null?null:arguments[i].getClass());
private List<TypeDescriptor> getTypes(Object... arguments) {
List<TypeDescriptor> descriptors = new ArrayList<TypeDescriptor>(arguments.length);
for (Object argument : arguments) {
descriptors.add(TypeDescriptor.forObject(argument));
}
return argumentTypes;
return descriptors;
}
@Override
@@ -152,7 +160,7 @@ public class MethodReference extends SpelNodeImpl {
return sb.toString();
}
private MethodExecutor findAccessorForMethod(String name, Class<?>[] argumentTypes, ExpressionState state)
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes, ExpressionState state)
throws SpelEvaluationException {
TypedValue context = state.getActiveContextObject();

View File

@@ -50,25 +50,25 @@ public class ReflectionHelper {
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match
*/
static ArgumentsMatchInfo compareArguments(
Class[] expectedArgTypes, Class[] suppliedArgTypes, TypeConverter typeConverter) {
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
Assert.isTrue(expectedArgTypes.length == suppliedArgTypes.length,
Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
"Expected argument types and supplied argument types should be arrays of same length");
ArgsMatchKind match = ArgsMatchKind.EXACT;
List<Integer> argsRequiringConversion = null;
for (int i = 0; i < expectedArgTypes.length && match != null; i++) {
Class suppliedArg = suppliedArgTypes[i];
Class expectedArg = expectedArgTypes[i];
if (expectedArg != suppliedArg) {
for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
TypeDescriptor expectedArg = expectedArgTypes.get(i);
if (!expectedArg.equals(suppliedArg)) {
// The user may supply null - and that will be ok unless a primitive is expected
if (suppliedArg == null) {
if (suppliedArg == TypeDescriptor.NULL) {
if (expectedArg.isPrimitive()) {
match = null;
}
}
else {
if (ClassUtils.isAssignable(expectedArg, suppliedArg)) {
if (suppliedArg.isAssignableTo(expectedArg)) {
if (match != ArgsMatchKind.REQUIRES_CONVERSION) {
match = ArgsMatchKind.CLOSE;
}
@@ -113,11 +113,11 @@ public class ReflectionHelper {
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match
*/
static ArgumentsMatchInfo compareArgumentsVarargs(
Class[] expectedArgTypes, Class[] suppliedArgTypes, TypeConverter typeConverter) {
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
Assert.isTrue(expectedArgTypes != null && expectedArgTypes.length > 0,
Assert.isTrue(expectedArgTypes != null && expectedArgTypes.size() > 0,
"Expected arguments must at least include one array (the vargargs parameter)");
Assert.isTrue(expectedArgTypes[expectedArgTypes.length - 1].isArray(),
Assert.isTrue(expectedArgTypes.get(expectedArgTypes.size() - 1).isArray(),
"Final expected argument should be array type (the varargs parameter)");
ArgsMatchKind match = ArgsMatchKind.EXACT;
@@ -126,18 +126,18 @@ public class ReflectionHelper {
// Check up until the varargs argument:
// Deal with the arguments up to 'expected number' - 1 (that is everything but the varargs argument)
int argCountUpToVarargs = expectedArgTypes.length-1;
int argCountUpToVarargs = expectedArgTypes.size() - 1;
for (int i = 0; i < argCountUpToVarargs && match != null; i++) {
Class suppliedArg = suppliedArgTypes[i];
Class<?> expectedArg = expectedArgTypes[i];
if (suppliedArg==null) {
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
TypeDescriptor expectedArg = expectedArgTypes.get(i);
if (suppliedArg == TypeDescriptor.NULL) {
if (expectedArg.isPrimitive()) {
match=null;
match = null;
}
}
else {
if (expectedArg != suppliedArg) {
if (expectedArg.isAssignableFrom(suppliedArg) || ClassUtils.isAssignableValue(expectedArg, suppliedArg)) {
if (!expectedArg.equals(suppliedArg)) {
if (suppliedArg.isAssignableTo(expectedArg)) {
if (match != ArgsMatchKind.REQUIRES_CONVERSION) {
match = ArgsMatchKind.CLOSE;
}
@@ -160,32 +160,33 @@ public class ReflectionHelper {
return null;
}
// Special case: there is one parameter left and it is an array and it matches the varargs expected argument -
// that is a match, the caller has already built the array
if (suppliedArgTypes.length == expectedArgTypes.length &&
expectedArgTypes[expectedArgTypes.length - 1] == suppliedArgTypes[suppliedArgTypes.length - 1]) {
if (suppliedArgTypes.size() == expectedArgTypes.size() &&
expectedArgTypes.get(expectedArgTypes.size() - 1).equals(
suppliedArgTypes.get(suppliedArgTypes.size() - 1))) {
// Special case: there is one parameter left and it is an array and it matches the varargs
// expected argument - that is a match, the caller has already built the array. Proceed with it.
}
else {
// Now... we have the final argument in the method we are checking as a match and we have 0 or more other
// arguments left to pass to it.
Class varargsParameterType = expectedArgTypes[expectedArgTypes.length - 1].getComponentType();
Class varargsParameterType = expectedArgTypes.get(expectedArgTypes.size() - 1).getElementType();
// All remaining parameters must be of this type or convertable to this type
for (int i = expectedArgTypes.length - 1; i < suppliedArgTypes.length; i++) {
Class suppliedArg = suppliedArgTypes[i];
if (varargsParameterType != suppliedArg) {
if (suppliedArg==null) {
for (int i = expectedArgTypes.size() - 1; i < suppliedArgTypes.size(); i++) {
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
if (varargsParameterType != suppliedArg.getType()) {
if (suppliedArg == TypeDescriptor.NULL) {
if (varargsParameterType.isPrimitive()) {
match=null;
match = null;
}
}
else {
if (ClassUtils.isAssignable(varargsParameterType, suppliedArg)) {
if (ClassUtils.isAssignable(varargsParameterType, suppliedArg.getType())) {
if (match != ArgsMatchKind.REQUIRES_CONVERSION) {
match = ArgsMatchKind.CLOSE;
}
}
else if (typeConverter.canConvert(suppliedArg, varargsParameterType)) {
else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsParameterType))) {
if (argsRequiringConversion == null) {
argsRequiringConversion = new ArrayList<Integer>();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
package org.springframework.expression.spel.support;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.ArrayList;
import org.springframework.expression.AccessException;
import org.springframework.expression.ConstructorExecutor;
@@ -24,6 +26,8 @@ import org.springframework.expression.ConstructorResolver;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypeConverter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.MethodParameter;
/**
* A constructor resolver that uses reflection to locate the constructor that should be invoked
@@ -43,7 +47,7 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
* registered type converter.
* </ol>
*/
public ConstructorExecutor resolve(EvaluationContext context, String typename, Class<?>[] argumentTypes)
public ConstructorExecutor resolve(EvaluationContext context, String typename, List<TypeDescriptor> argumentTypes)
throws AccessException {
try {
TypeConverter typeConverter = context.getTypeConverter();
@@ -53,26 +57,33 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
int[] argsToConvert = null;
Constructor matchRequiringConversion = null;
for (Constructor ctor : ctors) {
Class[] paramTypes = ctor.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++) {
paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
}
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
if (ctor.isVarArgs() && argumentTypes.length >= (ctor.getParameterTypes().length - 1)) {
if (ctor.isVarArgs() && argumentTypes.size() >= (paramTypes.length - 1)) {
// *sigh* complicated
// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
// being provided should be
// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
// or the final parameter
// we are supplied does match exactly (it is an array already).
matchInfo = ReflectionHelper.compareArgumentsVarargs(ctor.getParameterTypes(), argumentTypes, typeConverter);
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
}
else if (ctor.getParameterTypes().length == argumentTypes.length) {
else if (paramTypes.length == argumentTypes.size()) {
// worth a closer look
matchInfo = ReflectionHelper.compareArguments(ctor.getParameterTypes(), argumentTypes, typeConverter);
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
}
if (matchInfo != null) {
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
return new ReflectiveConstructorExecutor(ctor, null);
} else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE) {
}
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE) {
closeMatch = ctor;
} else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.REQUIRES_CONVERSION) {
}
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.REQUIRES_CONVERSION) {
argsToConvert = matchInfo.argsRequiringConversion;
matchRequiringConversion = ctor;
}
@@ -80,9 +91,11 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
}
if (closeMatch != null) {
return new ReflectiveConstructorExecutor(closeMatch, null);
} else if (matchRequiringConversion != null) {
}
else if (matchRequiringConversion != null) {
return new ReflectiveConstructorExecutor(matchRequiringConversion, argsToConvert);
} else {
}
else {
return null;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
@@ -32,11 +34,9 @@ import org.springframework.expression.TypeConverter;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
/**
* A method resolver that uses reflection to locate the method that should be invoked
*
* A method resolver that uses reflection to locate the method that should be invoked.
*
* @author Andy Clement
* @since 3.0
*/
@@ -44,7 +44,7 @@ public class ReflectiveMethodResolver implements MethodResolver {
private static Method[] NO_METHODS = new Method[0];
private Map<Class<?>,MethodFilter> filters = null;
private Map<Class<?>, MethodFilter> filters = null;
/**
@@ -52,11 +52,12 @@ public class ReflectiveMethodResolver implements MethodResolver {
* <ol>
* <li>An exact match where the types of the arguments match the types of the constructor
* <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
* <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
* registered type converter.
* <li>A match where we are able to convert the arguments into those expected by the constructor,
* according to the registered type converter.
* </ol>
*/
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, Class<?>[] argumentTypes) throws AccessException {
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
List<TypeDescriptor> argumentTypes) throws AccessException {
try {
TypeConverter typeConverter = context.getTypeConverter();
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
@@ -87,13 +88,19 @@ public class ReflectiveMethodResolver implements MethodResolver {
continue;
}
if (method.getName().equals(name)) {
Class[] paramTypes = method.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
for (int i = 0; i < paramTypes.length; i++) {
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
}
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
if (method.isVarArgs() && argumentTypes.length >= (method.getParameterTypes().length - 1)) {
if (method.isVarArgs() && argumentTypes.size() >= (paramTypes.length - 1)) {
// *sigh* complicated
matchInfo = ReflectionHelper.compareArgumentsVarargs(method.getParameterTypes(), argumentTypes, typeConverter);
} else if (method.getParameterTypes().length == argumentTypes.length) {
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
}
else if (paramTypes.length == argumentTypes.size()) {
// name and parameter number match, check the arguments
matchInfo = ReflectionHelper.compareArguments(method.getParameterTypes(), argumentTypes, typeConverter);
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
}
if (matchInfo != null) {
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
@@ -131,14 +138,14 @@ public class ReflectiveMethodResolver implements MethodResolver {
}
public void registerMethodFilter(Class<?> type, MethodFilter filter) {
if (filters==null) {
filters = new HashMap<Class<?>,MethodFilter>();
if (this.filters == null) {
this.filters = new HashMap<Class<?>,MethodFilter>();
}
if (filter==null) {
filters.remove(type);
if (filter == null) {
this.filters.remove(type);
}
else {
filters.put(type,filter);
this.filters.put(type,filter);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,10 +57,6 @@ public class StandardTypeConverter implements TypeConverter {
}
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return this.conversionService.canConvert(sourceType, targetType);
}
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType);
}