Fixed getTypeDifferenceWeight algorithm in ReflectionHelper, and removed unused argsRequiringConversion storage
Issue: SPR-11306
This commit is contained in:
parent
5f2106046c
commit
5d06bcec70
|
|
@ -18,7 +18,6 @@ package org.springframework.expression.spel.support;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
|
@ -46,11 +45,9 @@ public class ReflectionHelper {
|
||||||
* type converter and conversionAllowed flag allow for matches to take into account
|
* type converter and conversionAllowed flag allow for matches to take into account
|
||||||
* that a type may be transformed into a different type by the converter.
|
* that a type may be transformed into a different type by the converter.
|
||||||
* @param expectedArgTypes the array of types the method/constructor is expecting
|
* @param expectedArgTypes the array of types the method/constructor is expecting
|
||||||
* @param suppliedArgTypes the array of types that are being supplied at the point of
|
* @param suppliedArgTypes the array of types that are being supplied at the point of invocation
|
||||||
* invocation
|
|
||||||
* @param typeConverter a registered type converter
|
* @param typeConverter a registered type converter
|
||||||
* @return a MatchInfo object indicating what kind of match it was or null if it was
|
* @return a MatchInfo object indicating what kind of match it was or null if it was not a match
|
||||||
* not a match
|
|
||||||
*/
|
*/
|
||||||
static ArgumentsMatchInfo compareArguments(
|
static ArgumentsMatchInfo compareArguments(
|
||||||
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
|
List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {
|
||||||
|
|
@ -59,7 +56,6 @@ public class ReflectionHelper {
|
||||||
"Expected argument types and supplied argument types should be arrays of same length");
|
"Expected argument types and supplied argument types should be arrays of same length");
|
||||||
|
|
||||||
ArgsMatchKind match = ArgsMatchKind.EXACT;
|
ArgsMatchKind match = ArgsMatchKind.EXACT;
|
||||||
List<Integer> argsRequiringConversion = null;
|
|
||||||
for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
|
for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
|
||||||
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
|
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
|
||||||
TypeDescriptor expectedArg = expectedArgTypes.get(i);
|
TypeDescriptor expectedArg = expectedArgTypes.get(i);
|
||||||
|
|
@ -77,10 +73,6 @@ public class ReflectionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
|
else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
|
||||||
if (argsRequiringConversion == null) {
|
|
||||||
argsRequiringConversion = new ArrayList<Integer>();
|
|
||||||
}
|
|
||||||
argsRequiringConversion.add(i);
|
|
||||||
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -89,21 +81,7 @@ public class ReflectionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (match == null) {
|
return (match != null ? new ArgumentsMatchInfo(match) : null);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (match == ArgsMatchKind.REQUIRES_CONVERSION) {
|
|
||||||
int[] argsArray = new int[argsRequiringConversion.size()];
|
|
||||||
for (int i = 0; i < argsRequiringConversion.size(); i++) {
|
|
||||||
argsArray[i] = argsRequiringConversion.get(i);
|
|
||||||
}
|
|
||||||
return new ArgumentsMatchInfo(match, argsArray);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return new ArgumentsMatchInfo(match);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -111,25 +89,25 @@ public class ReflectionHelper {
|
||||||
*/
|
*/
|
||||||
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
|
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0,max=paramTypes.size(); i < max; i++) {
|
for (int i = 0; i < paramTypes.size(); i++) {
|
||||||
TypeDescriptor argType = argTypes.get(i);
|
|
||||||
TypeDescriptor paramType = paramTypes.get(i);
|
TypeDescriptor paramType = paramTypes.get(i);
|
||||||
|
TypeDescriptor argType = argTypes.get(i);
|
||||||
if (argType == null) {
|
if (argType == null) {
|
||||||
if (paramType.isPrimitive()) {
|
if (paramType.isPrimitive()) {
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!ClassUtils.isAssignable(paramType.getClass(), argType.getClass())) {
|
else {
|
||||||
return Integer.MAX_VALUE;
|
|
||||||
}
|
|
||||||
if (argType != null) {
|
|
||||||
Class<?> paramTypeClazz = paramType.getType();
|
Class<?> paramTypeClazz = paramType.getType();
|
||||||
|
if (!ClassUtils.isAssignable(paramTypeClazz, argType.getType())) {
|
||||||
|
return Integer.MAX_VALUE;
|
||||||
|
}
|
||||||
if (paramTypeClazz.isPrimitive()) {
|
if (paramTypeClazz.isPrimitive()) {
|
||||||
paramTypeClazz = Object.class;
|
paramTypeClazz = Object.class;
|
||||||
}
|
}
|
||||||
Class<?> superClass = argType.getClass().getSuperclass();
|
Class<?> superClass = argType.getType().getSuperclass();
|
||||||
while (superClass != null) {
|
while (superClass != null) {
|
||||||
if (paramType.equals(superClass)) {
|
if (paramTypeClazz.equals(superClass)) {
|
||||||
result = result + 2;
|
result = result + 2;
|
||||||
superClass = null;
|
superClass = null;
|
||||||
}
|
}
|
||||||
|
|
@ -167,7 +145,6 @@ public class ReflectionHelper {
|
||||||
"Final expected argument should be array type (the varargs parameter)");
|
"Final expected argument should be array type (the varargs parameter)");
|
||||||
|
|
||||||
ArgsMatchKind match = ArgsMatchKind.EXACT;
|
ArgsMatchKind match = ArgsMatchKind.EXACT;
|
||||||
List<Integer> argsRequiringConversion = null;
|
|
||||||
|
|
||||||
// Check up until the varargs argument:
|
// Check up until the varargs argument:
|
||||||
|
|
||||||
|
|
@ -189,10 +166,6 @@ public class ReflectionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
|
else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
|
||||||
if (argsRequiringConversion == null) {
|
|
||||||
argsRequiringConversion = new ArrayList<Integer>();
|
|
||||||
}
|
|
||||||
argsRequiringConversion.add(i);
|
|
||||||
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -214,29 +187,26 @@ public class ReflectionHelper {
|
||||||
// expected argument - that is a match, the caller has already built the array. Proceed with it.
|
// expected argument - that is a match, the caller has already built the array. Proceed with it.
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Now... we have the final argument in the method we are checking as a match and we have 0 or more other
|
// Now... we have the final argument in the method we are checking as a match and we have 0
|
||||||
// arguments left to pass to it.
|
// or more other arguments left to pass to it.
|
||||||
Class<?> varargsParameterType = expectedArgTypes.get(expectedArgTypes.size() - 1).getElementTypeDescriptor().getType();
|
Class<?> varargsParamType = expectedArgTypes.get(expectedArgTypes.size() - 1).getElementTypeDescriptor().getType();
|
||||||
|
|
||||||
// All remaining parameters must be of this type or convertable to this type
|
// All remaining parameters must be of this type or convertable to this type
|
||||||
for (int i = expectedArgTypes.size() - 1; i < suppliedArgTypes.size(); i++) {
|
for (int i = expectedArgTypes.size() - 1; i < suppliedArgTypes.size(); i++) {
|
||||||
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
|
TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
|
||||||
if (suppliedArg == null) {
|
if (suppliedArg == null) {
|
||||||
if (varargsParameterType.isPrimitive()) {
|
if (varargsParamType.isPrimitive()) {
|
||||||
match = null;
|
match = null;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
if (varargsParameterType != suppliedArg.getType()) {
|
else {
|
||||||
if (ClassUtils.isAssignable(varargsParameterType, suppliedArg.getType())) {
|
if (varargsParamType != suppliedArg.getType()) {
|
||||||
|
if (ClassUtils.isAssignable(varargsParamType, suppliedArg.getType())) {
|
||||||
if (match != ArgsMatchKind.REQUIRES_CONVERSION) {
|
if (match != ArgsMatchKind.REQUIRES_CONVERSION) {
|
||||||
match = ArgsMatchKind.CLOSE;
|
match = ArgsMatchKind.CLOSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsParameterType))) {
|
else if (typeConverter.canConvert(suppliedArg, TypeDescriptor.valueOf(varargsParamType))) {
|
||||||
if (argsRequiringConversion == null) {
|
|
||||||
argsRequiringConversion = new ArrayList<Integer>();
|
|
||||||
}
|
|
||||||
argsRequiringConversion.add(i);
|
|
||||||
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
match = ArgsMatchKind.REQUIRES_CONVERSION;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -247,21 +217,7 @@ public class ReflectionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (match == null) {
|
return (match != null ? new ArgumentsMatchInfo(match) : null);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (match == ArgsMatchKind.REQUIRES_CONVERSION) {
|
|
||||||
int[] argsArray = new int[argsRequiringConversion.size()];
|
|
||||||
for (int i = 0; i < argsRequiringConversion.size(); i++) {
|
|
||||||
argsArray[i] = argsRequiringConversion.get(i);
|
|
||||||
}
|
|
||||||
return new ArgumentsMatchInfo(match, argsArray);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return new ArgumentsMatchInfo(match);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -271,19 +227,20 @@ public class ReflectionHelper {
|
||||||
* @param converter the type converter to use for attempting conversions
|
* @param converter the type converter to use for attempting conversions
|
||||||
* @param arguments the actual arguments that need conversion
|
* @param arguments the actual arguments that need conversion
|
||||||
* @param methodOrCtor the target Method or Constructor
|
* @param methodOrCtor the target Method or Constructor
|
||||||
* @param argumentsRequiringConversion details which of the input arguments for sure need conversion
|
|
||||||
* @param varargsPosition the known position of the varargs argument, if any
|
* @param varargsPosition the known position of the varargs argument, if any
|
||||||
* @throws EvaluationException if a problem occurs during conversion
|
* @throws EvaluationException if a problem occurs during conversion
|
||||||
*/
|
*/
|
||||||
static void convertArguments(TypeConverter converter, Object[] arguments, Object methodOrCtor,
|
static void convertArguments(TypeConverter converter, Object[] arguments, Object methodOrCtor,
|
||||||
int[] argumentsRequiringConversion, Integer varargsPosition) throws EvaluationException {
|
Integer varargsPosition) throws EvaluationException {
|
||||||
|
|
||||||
if (varargsPosition == null) {
|
if (varargsPosition == null) {
|
||||||
for (int i = 0; i < arguments.length; i++) {
|
for (int i = 0; i < arguments.length; i++) {
|
||||||
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
|
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
|
||||||
Object argument = arguments[i];
|
Object argument = arguments[i];
|
||||||
arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
for (int i = 0; i < varargsPosition; i++) {
|
for (int i = 0; i < varargsPosition; i++) {
|
||||||
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
|
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
|
||||||
Object argument = arguments[i];
|
Object argument = arguments[i];
|
||||||
|
|
@ -294,7 +251,8 @@ public class ReflectionHelper {
|
||||||
TypeDescriptor targetType = new TypeDescriptor(methodParam);
|
TypeDescriptor targetType = new TypeDescriptor(methodParam);
|
||||||
Object argument = arguments[varargsPosition];
|
Object argument = arguments[varargsPosition];
|
||||||
arguments[varargsPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
arguments[varargsPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
TypeDescriptor targetType = TypeDescriptor.nested(methodParam, 1);
|
TypeDescriptor targetType = TypeDescriptor.nested(methodParam, 1);
|
||||||
for (int i = varargsPosition; i < arguments.length; i++) {
|
for (int i = varargsPosition; i < arguments.length; i++) {
|
||||||
Object argument = arguments[i];
|
Object argument = arguments[i];
|
||||||
|
|
@ -316,28 +274,31 @@ public class ReflectionHelper {
|
||||||
* @param method the target Method
|
* @param method the target Method
|
||||||
* @throws SpelEvaluationException if there is a problem with conversion
|
* @throws SpelEvaluationException if there is a problem with conversion
|
||||||
*/
|
*/
|
||||||
public static void convertAllArguments(TypeConverter converter, Object[] arguments, Method method) throws SpelEvaluationException {
|
public static void convertAllArguments(TypeConverter converter, Object[] arguments, Method method)
|
||||||
|
throws SpelEvaluationException {
|
||||||
|
|
||||||
Integer varargsPosition = null;
|
Integer varargsPosition = null;
|
||||||
if (method.isVarArgs()) {
|
if (method.isVarArgs()) {
|
||||||
Class<?>[] paramTypes = method.getParameterTypes();
|
Class<?>[] paramTypes = method.getParameterTypes();
|
||||||
varargsPosition = paramTypes.length - 1;
|
varargsPosition = paramTypes.length - 1;
|
||||||
}
|
}
|
||||||
for (int argPosition = 0; argPosition < arguments.length; argPosition++) {
|
for (int argPos = 0; argPos < arguments.length; argPos++) {
|
||||||
TypeDescriptor targetType;
|
TypeDescriptor targetType;
|
||||||
if (varargsPosition != null && argPosition >= varargsPosition) {
|
if (varargsPosition != null && argPos >= varargsPosition) {
|
||||||
MethodParameter methodParam = new MethodParameter(method, varargsPosition);
|
MethodParameter methodParam = new MethodParameter(method, varargsPosition);
|
||||||
targetType = TypeDescriptor.nested(methodParam, 1);
|
targetType = TypeDescriptor.nested(methodParam, 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
targetType = new TypeDescriptor(new MethodParameter(method, argPosition));
|
targetType = new TypeDescriptor(new MethodParameter(method, argPos));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Object argument = arguments[argPosition];
|
Object argument = arguments[argPos];
|
||||||
if (argument != null && !targetType.getObjectType().isInstance(argument)) {
|
if (argument != null && !targetType.getObjectType().isInstance(argument)) {
|
||||||
if (converter == null) {
|
if (converter == null) {
|
||||||
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
|
throw new SpelEvaluationException(
|
||||||
|
SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
|
||||||
}
|
}
|
||||||
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
arguments[argPos] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (EvaluationException ex) {
|
catch (EvaluationException ex) {
|
||||||
|
|
@ -346,7 +307,8 @@ public class ReflectionHelper {
|
||||||
throw (SpelEvaluationException)ex;
|
throw (SpelEvaluationException)ex;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,arguments[argPosition].getClass().getName(), targetType);
|
throw new SpelEvaluationException(ex,
|
||||||
|
SpelMessage.TYPE_CONVERSION_ERROR,arguments[argPos].getClass().getName(), targetType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -377,9 +339,8 @@ public class ReflectionHelper {
|
||||||
|
|
||||||
// Create an array for the varargs arguments
|
// Create an array for the varargs arguments
|
||||||
Object[] newArgs = new Object[parameterCount];
|
Object[] newArgs = new Object[parameterCount];
|
||||||
for (int i = 0; i < newArgs.length - 1; i++) {
|
System.arraycopy(args, 0, newArgs, 0, newArgs.length - 1);
|
||||||
newArgs[i] = args[i];
|
|
||||||
}
|
|
||||||
// Now sort out the final argument, which is the varargs one. Before entering this
|
// Now sort out the final argument, which is the varargs one. Before entering this
|
||||||
// method the arguments should have been converted to the box form of the required type.
|
// method the arguments should have been converted to the box form of the required type.
|
||||||
Class<?> componentType = requiredParameterTypes[parameterCount-1].getComponentType();
|
Class<?> componentType = requiredParameterTypes[parameterCount-1].getComponentType();
|
||||||
|
|
@ -450,12 +411,9 @@ public class ReflectionHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Object[] repackagedArguments = (Object[]) Array.newInstance(
|
Object[] repackagedArguments = (Object[]) Array.newInstance(componentType, arraySize);
|
||||||
componentType, arraySize);
|
|
||||||
// Copy all but the varargs arguments
|
// Copy all but the varargs arguments
|
||||||
for (int i = 0; i < arraySize; i++) {
|
System.arraycopy(args, parameterCount - 1, repackagedArguments, 0, arraySize);
|
||||||
repackagedArguments[i] = args[parameterCount + i - 1];
|
|
||||||
}
|
|
||||||
newArgs[newArgs.length - 1] = repackagedArguments;
|
newArgs[newArgs.length - 1] = repackagedArguments;
|
||||||
}
|
}
|
||||||
return newArgs;
|
return newArgs;
|
||||||
|
|
@ -465,32 +423,28 @@ public class ReflectionHelper {
|
||||||
|
|
||||||
|
|
||||||
public static enum ArgsMatchKind {
|
public static enum ArgsMatchKind {
|
||||||
// An exact match is where the parameter types exactly match what the method/constructor being invoked is expecting
|
|
||||||
|
/** An exact match is where the parameter types exactly match what the method/constructor being invoked is expecting */
|
||||||
EXACT,
|
EXACT,
|
||||||
// A close match is where the parameter types either exactly match or are assignment compatible with the method/constructor being invoked
|
|
||||||
|
/** A close match is where the parameter types either exactly match or are assignment compatible with the method/constructor being invoked */
|
||||||
CLOSE,
|
CLOSE,
|
||||||
// A conversion match is where the type converter must be used to transform some of the parameter types
|
|
||||||
|
/** A conversion match is where the type converter must be used to transform some of the parameter types */
|
||||||
REQUIRES_CONVERSION
|
REQUIRES_CONVERSION
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An instance of ArgumentsMatchInfo describes what kind of match was achieved between two sets of arguments - the set that a
|
* An instance of ArgumentsMatchInfo describes what kind of match was achieved between two sets of arguments -
|
||||||
* method/constructor is expecting and the set that are being supplied at the point of invocation. If the kind
|
* the set that a method/constructor is expecting and the set that are being supplied at the point of invocation.
|
||||||
* indicates that conversion is required for some of the arguments then the arguments that require conversion are
|
* If the kind indicates that conversion is required for some of the arguments then the arguments that require
|
||||||
* listed in the argsRequiringConversion array.
|
* conversion are listed in the argsRequiringConversion array.
|
||||||
*/
|
*/
|
||||||
public static class ArgumentsMatchInfo {
|
public static class ArgumentsMatchInfo {
|
||||||
|
|
||||||
public final ArgsMatchKind kind;
|
public final ArgsMatchKind kind;
|
||||||
|
|
||||||
public int[] argsRequiringConversion;
|
|
||||||
|
|
||||||
ArgumentsMatchInfo(ArgsMatchKind kind, int[] integers) {
|
|
||||||
this.kind = kind;
|
|
||||||
this.argsRequiringConversion = integers;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArgumentsMatchInfo(ArgsMatchKind kind) {
|
ArgumentsMatchInfo(ArgsMatchKind kind) {
|
||||||
this.kind = kind;
|
this.kind = kind;
|
||||||
}
|
}
|
||||||
|
|
@ -509,19 +463,7 @@ public class ReflectionHelper {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
return "ArgumentMatchInfo: " + this.kind;
|
||||||
sb.append("ArgumentMatch: ").append(this.kind);
|
|
||||||
if (this.argsRequiringConversion != null) {
|
|
||||||
sb.append(" (argsForConversion:");
|
|
||||||
for (int i = 0; i < this.argsRequiringConversion.length;i++) {
|
|
||||||
if (i > 0) {
|
|
||||||
sb.append(",");
|
|
||||||
}
|
|
||||||
sb.append(this.argsRequiringConversion[i]);
|
|
||||||
}
|
|
||||||
sb.append(")");
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,12 +38,8 @@ class ReflectiveConstructorExecutor implements ConstructorExecutor {
|
||||||
|
|
||||||
private final Integer varargsPosition;
|
private final Integer varargsPosition;
|
||||||
|
|
||||||
// When the constructor was found, we will have determined if arguments need to be converted for it
|
|
||||||
// to be invoked. Conversion won't be cheap so let's only do it if necessary.
|
|
||||||
private final int[] argsRequiringConversion;
|
|
||||||
|
|
||||||
|
public ReflectiveConstructorExecutor(Constructor<?> ctor) {
|
||||||
public ReflectiveConstructorExecutor(Constructor<?> ctor, int[] argsRequiringConversion) {
|
|
||||||
this.ctor = ctor;
|
this.ctor = ctor;
|
||||||
if (ctor.isVarArgs()) {
|
if (ctor.isVarArgs()) {
|
||||||
Class<?>[] paramTypes = ctor.getParameterTypes();
|
Class<?>[] paramTypes = ctor.getParameterTypes();
|
||||||
|
|
@ -52,15 +48,13 @@ class ReflectiveConstructorExecutor implements ConstructorExecutor {
|
||||||
else {
|
else {
|
||||||
this.varargsPosition = null;
|
this.varargsPosition = null;
|
||||||
}
|
}
|
||||||
this.argsRequiringConversion = argsRequiringConversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
|
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
|
||||||
try {
|
try {
|
||||||
if (arguments != null) {
|
if (arguments != null) {
|
||||||
ReflectionHelper.convertArguments(context.getTypeConverter(), arguments,
|
ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.ctor, this.varargsPosition);
|
||||||
this.ctor, this.argsRequiringConversion, this.varargsPosition);
|
|
||||||
}
|
}
|
||||||
if (this.ctor.isVarArgs()) {
|
if (this.ctor.isVarArgs()) {
|
||||||
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.ctor.getParameterTypes(), arguments);
|
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.ctor.getParameterTypes(), arguments);
|
||||||
|
|
|
||||||
|
|
@ -71,8 +71,6 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
|
||||||
});
|
});
|
||||||
|
|
||||||
Constructor<?> closeMatch = null;
|
Constructor<?> closeMatch = null;
|
||||||
int[] argsToConvert = null;
|
|
||||||
Constructor<?> matchRequiringConversion = null;
|
|
||||||
|
|
||||||
for (Constructor<?> ctor : ctors) {
|
for (Constructor<?> ctor : ctors) {
|
||||||
Class<?>[] paramTypes = ctor.getParameterTypes();
|
Class<?>[] paramTypes = ctor.getParameterTypes();
|
||||||
|
|
@ -81,7 +79,7 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
|
||||||
paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
|
paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
|
||||||
}
|
}
|
||||||
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
|
ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
|
||||||
if (ctor.isVarArgs() && argumentTypes.size() >= (paramTypes.length - 1)) {
|
if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
|
||||||
// *sigh* complicated
|
// *sigh* complicated
|
||||||
// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
|
// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
|
||||||
// being provided should be
|
// being provided should be
|
||||||
|
|
@ -96,26 +94,16 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
|
||||||
}
|
}
|
||||||
if (matchInfo != null) {
|
if (matchInfo != null) {
|
||||||
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
|
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
|
||||||
return new ReflectiveConstructorExecutor(ctor, null);
|
return new ReflectiveConstructorExecutor(ctor);
|
||||||
}
|
}
|
||||||
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE) {
|
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE ||
|
||||||
|
matchInfo.kind == ReflectionHelper.ArgsMatchKind.REQUIRES_CONVERSION) {
|
||||||
closeMatch = ctor;
|
closeMatch = ctor;
|
||||||
}
|
}
|
||||||
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.REQUIRES_CONVERSION) {
|
|
||||||
argsToConvert = matchInfo.argsRequiringConversion;
|
|
||||||
matchRequiringConversion = ctor;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (closeMatch != null) {
|
|
||||||
return new ReflectiveConstructorExecutor(closeMatch, null);
|
return (closeMatch != null ? new ReflectiveConstructorExecutor(closeMatch) : null);
|
||||||
}
|
|
||||||
else if (matchRequiringConversion != null) {
|
|
||||||
return new ReflectiveConstructorExecutor(matchRequiringConversion, argsToConvert);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (EvaluationException ex) {
|
catch (EvaluationException ex) {
|
||||||
throw new AccessException("Failed to resolve constructor", ex);
|
throw new AccessException("Failed to resolve constructor", ex);
|
||||||
|
|
|
||||||
|
|
@ -37,21 +37,16 @@ class ReflectiveMethodExecutor implements MethodExecutor {
|
||||||
|
|
||||||
private final Integer varargsPosition;
|
private final Integer varargsPosition;
|
||||||
|
|
||||||
// When the method was found, we will have determined if arguments need to be converted for it
|
|
||||||
// to be invoked. Conversion won't be cheap so let's only do it if necessary.
|
|
||||||
private final int[] argsRequiringConversion;
|
|
||||||
|
|
||||||
|
public ReflectiveMethodExecutor(Method method) {
|
||||||
public ReflectiveMethodExecutor(Method theMethod, int[] argumentsRequiringConversion) {
|
this.method = method;
|
||||||
this.method = theMethod;
|
if (method.isVarArgs()) {
|
||||||
if (theMethod.isVarArgs()) {
|
Class<?>[] paramTypes = method.getParameterTypes();
|
||||||
Class<?>[] paramTypes = theMethod.getParameterTypes();
|
|
||||||
this.varargsPosition = paramTypes.length - 1;
|
this.varargsPosition = paramTypes.length - 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.varargsPosition = null;
|
this.varargsPosition = null;
|
||||||
}
|
}
|
||||||
this.argsRequiringConversion = argumentsRequiringConversion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -59,9 +54,7 @@ class ReflectiveMethodExecutor implements MethodExecutor {
|
||||||
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
|
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
|
||||||
try {
|
try {
|
||||||
if (arguments != null) {
|
if (arguments != null) {
|
||||||
ReflectionHelper.convertArguments(
|
ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.method, this.varargsPosition);
|
||||||
context.getTypeConverter(), arguments, this.method,
|
|
||||||
this.argsRequiringConversion, this.varargsPosition);
|
|
||||||
}
|
}
|
||||||
if (this.method.isVarArgs()) {
|
if (this.method.isVarArgs()) {
|
||||||
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.method.getParameterTypes(), arguments);
|
arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.method.getParameterTypes(), arguments);
|
||||||
|
|
|
||||||
|
|
@ -141,12 +141,12 @@ public class ReflectiveMethodResolver implements MethodResolver {
|
||||||
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
|
matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
|
||||||
}
|
}
|
||||||
else if (paramTypes.length == argumentTypes.size()) {
|
else if (paramTypes.length == argumentTypes.size()) {
|
||||||
// name and parameter number match, check the arguments
|
// Name and parameter number match, check the arguments
|
||||||
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
|
matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
|
||||||
}
|
}
|
||||||
if (matchInfo != null) {
|
if (matchInfo != null) {
|
||||||
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
|
if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.EXACT) {
|
||||||
return new ReflectiveMethodExecutor(method, null);
|
return new ReflectiveMethodExecutor(method);
|
||||||
}
|
}
|
||||||
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE) {
|
else if (matchInfo.kind == ReflectionHelper.ArgsMatchKind.CLOSE) {
|
||||||
if (!this.useDistance) {
|
if (!this.useDistance) {
|
||||||
|
|
@ -154,8 +154,8 @@ public class ReflectiveMethodResolver implements MethodResolver {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
|
int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
|
||||||
if (matchDistance<closeMatchDistance) {
|
if (matchDistance < closeMatchDistance) {
|
||||||
// this is a better match
|
// This is a better match...
|
||||||
closeMatchDistance = matchDistance;
|
closeMatchDistance = matchDistance;
|
||||||
closeMatch = method;
|
closeMatch = method;
|
||||||
}
|
}
|
||||||
|
|
@ -165,20 +165,19 @@ public class ReflectiveMethodResolver implements MethodResolver {
|
||||||
if (matchRequiringConversion != null) {
|
if (matchRequiringConversion != null) {
|
||||||
multipleOptions = true;
|
multipleOptions = true;
|
||||||
}
|
}
|
||||||
argsToConvert = matchInfo.argsRequiringConversion;
|
|
||||||
matchRequiringConversion = method;
|
matchRequiringConversion = method;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (closeMatch != null) {
|
if (closeMatch != null) {
|
||||||
return new ReflectiveMethodExecutor(closeMatch, null);
|
return new ReflectiveMethodExecutor(closeMatch);
|
||||||
}
|
}
|
||||||
else if (matchRequiringConversion != null) {
|
else if (matchRequiringConversion != null) {
|
||||||
if (multipleOptions) {
|
if (multipleOptions) {
|
||||||
throw new SpelEvaluationException(SpelMessage.MULTIPLE_POSSIBLE_METHODS, name);
|
throw new SpelEvaluationException(SpelMessage.MULTIPLE_POSSIBLE_METHODS, name);
|
||||||
}
|
}
|
||||||
return new ReflectiveMethodExecutor(matchRequiringConversion, argsToConvert);
|
return new ReflectiveMethodExecutor(matchRequiringConversion);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,13 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.expression.spel.support;
|
package org.springframework.expression.spel.support;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
@ -30,6 +23,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.EvaluationContext;
|
||||||
import org.springframework.expression.ParseException;
|
import org.springframework.expression.ParseException;
|
||||||
|
|
@ -43,6 +37,8 @@ import org.springframework.expression.spel.ast.FormatHelper;
|
||||||
import org.springframework.expression.spel.standard.SpelExpression;
|
import org.springframework.expression.spel.standard.SpelExpression;
|
||||||
import org.springframework.expression.spel.support.ReflectionHelper.ArgsMatchKind;
|
import org.springframework.expression.spel.support.ReflectionHelper.ArgsMatchKind;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for any helper code.
|
* Tests for any helper code.
|
||||||
*
|
*
|
||||||
|
|
@ -63,7 +59,7 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
@Test
|
@Test
|
||||||
public void testFormatHelperForMethod() {
|
public void testFormatHelperForMethod() {
|
||||||
assertEquals("foo(java.lang.String)",FormatHelper.formatMethodForMessage("foo", String.class));
|
assertEquals("foo(java.lang.String)",FormatHelper.formatMethodForMessage("foo", String.class));
|
||||||
assertEquals("goo(java.lang.String,int[])",FormatHelper.formatMethodForMessage("goo", String.class,new int[1].getClass()));
|
assertEquals("goo(java.lang.String,int[])",FormatHelper.formatMethodForMessage("goo", String.class, new int[1].getClass()));
|
||||||
assertEquals("boo()",FormatHelper.formatMethodForMessage("boo"));
|
assertEquals("boo()",FormatHelper.formatMethodForMessage("boo"));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
@ -95,8 +91,8 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
// CompoundExpression value:2
|
// CompoundExpression value:2
|
||||||
// IntLiteral value:2
|
// IntLiteral value:2
|
||||||
// ===> Expression '3+4+5+6+7-2' - AST end
|
// ===> Expression '3+4+5+6+7-2' - AST end
|
||||||
assertTrue(s.indexOf("===> Expression '3+4+5+6+7-2' - AST start")!=-1);
|
assertTrue(s.contains("===> Expression '3+4+5+6+7-2' - AST start"));
|
||||||
assertTrue(s.indexOf(" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2")!=-1);
|
assertTrue(s.contains(" OpPlus value:((((3 + 4) + 5) + 6) + 7) #children:2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -108,44 +104,44 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReflectionHelperCompareArguments_ExactMatching() {
|
public void testReflectionHelperCompareArguments_ExactMatching() {
|
||||||
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
StandardTypeConverter tc = new StandardTypeConverter();
|
||||||
|
|
||||||
// Calling foo(String) with (String) is exact match
|
// Calling foo(String) with (String) is exact match
|
||||||
checkMatch(new Class[]{String.class},new Class[]{String.class},typeConverter,ArgsMatchKind.EXACT);
|
checkMatch(new Class[] {String.class}, new Class[] {String.class}, tc,ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Calling foo(String,Integer) with (String,Integer) is exact match
|
// Calling foo(String,Integer) with (String,Integer) is exact match
|
||||||
checkMatch(new Class[]{String.class,Integer.class},new Class[]{String.class,Integer.class},typeConverter,ArgsMatchKind.EXACT);
|
checkMatch(new Class[] {String.class, Integer.class}, new Class[] {String.class, Integer.class}, tc, ArgsMatchKind.EXACT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReflectionHelperCompareArguments_CloseMatching() {
|
public void testReflectionHelperCompareArguments_CloseMatching() {
|
||||||
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
StandardTypeConverter tc = new StandardTypeConverter();
|
||||||
|
|
||||||
// Calling foo(List) with (ArrayList) is close match (no conversion required)
|
// Calling foo(List) with (ArrayList) is close match (no conversion required)
|
||||||
checkMatch(new Class[]{ArrayList.class},new Class[]{List.class},typeConverter,ArgsMatchKind.CLOSE);
|
checkMatch(new Class[] {ArrayList.class}, new Class[] {List.class}, tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (Sub,String) on call to foo(Super,String) is close match
|
// Passing (Sub,String) on call to foo(Super,String) is close match
|
||||||
checkMatch(new Class[]{Sub.class,String.class},new Class[]{Super.class,String.class},typeConverter,ArgsMatchKind.CLOSE);
|
checkMatch(new Class[] {Sub.class, String.class}, new Class[] {Super.class, String.class}, tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (String,Sub) on call to foo(String,Super) is close match
|
// Passing (String,Sub) on call to foo(String,Super) is close match
|
||||||
checkMatch(new Class[]{String.class,Sub.class},new Class[]{String.class,Super.class},typeConverter,ArgsMatchKind.CLOSE);
|
checkMatch(new Class[] {String.class, Sub.class}, new Class[] {String.class, Super.class}, tc, ArgsMatchKind.CLOSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReflectionHelperCompareArguments_RequiresConversionMatching() {
|
public void testReflectionHelperCompareArguments_RequiresConversionMatching() {
|
||||||
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
StandardTypeConverter tc = new StandardTypeConverter();
|
||||||
|
|
||||||
// Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one
|
// Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one
|
||||||
checkMatch(new Class[]{String.class,Integer.TYPE},new Class[]{String.class,Integer.class},typeConverter,ArgsMatchKind.CLOSE,1);
|
checkMatch(new Class[] {String.class, Integer.TYPE}, new Class[] {String.class,Integer.class},tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (int,String) on call to foo(Integer,String) requires boxing conversion of argument zero
|
// Passing (int,String) on call to foo(Integer,String) requires boxing conversion of argument zero
|
||||||
checkMatch(new Class[]{Integer.TYPE,String.class},new Class[]{Integer.class, String.class},typeConverter,ArgsMatchKind.CLOSE,0);
|
checkMatch(new Class[] {Integer.TYPE, String.class}, new Class[] {Integer.class, String.class},tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (int,Sub) on call to foo(Integer,Super) requires boxing conversion of argument zero
|
// Passing (int,Sub) on call to foo(Integer,Super) requires boxing conversion of argument zero
|
||||||
checkMatch(new Class[]{Integer.TYPE,Sub.class},new Class[]{Integer.class, Super.class},typeConverter,ArgsMatchKind.CLOSE,0);
|
checkMatch(new Class[] {Integer.TYPE, Sub.class}, new Class[] {Integer.class, Super.class}, tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (int,Sub,boolean) on call to foo(Integer,Super,Boolean) requires boxing conversion of arguments zero and two
|
// Passing (int,Sub,boolean) on call to foo(Integer,Super,Boolean) requires boxing conversion of arguments zero and two
|
||||||
// TODO checkMatch(new Class[]{Integer.TYPE,Sub.class,Boolean.TYPE},new Class[]{Integer.class, Super.class,Boolean.class},typeConverter,ArgsMatchKind.REQUIRES_CONVERSION,0,2);
|
// TODO checkMatch(new Class[] {Integer.TYPE, Sub.class, Boolean.TYPE}, new Class[] {Integer.class, Super.class, Boolean.class}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -153,7 +149,7 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
||||||
|
|
||||||
// Passing (Super,String) on call to foo(Sub,String) is not a match
|
// Passing (Super,String) on call to foo(Sub,String) is not a match
|
||||||
checkMatch(new Class[]{Super.class,String.class},new Class[]{Sub.class,String.class},typeConverter,null);
|
checkMatch(new Class[] {Super.class,String.class}, new Class[] {Sub.class,String.class},typeConverter,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -163,49 +159,49 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
Class<?> integerArrayClass = new Integer[0].getClass();
|
Class<?> integerArrayClass = new Integer[0].getClass();
|
||||||
|
|
||||||
// Passing (String[]) on call to (String[]) is exact match
|
// Passing (String[]) on call to (String[]) is exact match
|
||||||
checkMatch2(new Class[]{stringArrayClass},new Class[]{stringArrayClass},tc,ArgsMatchKind.EXACT);
|
checkMatch2(new Class[] {stringArrayClass}, new Class[] {stringArrayClass}, tc, ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Passing (Integer, String[]) on call to (Integer, String[]) is exact match
|
// Passing (Integer, String[]) on call to (Integer, String[]) is exact match
|
||||||
checkMatch2(new Class[]{Integer.class,stringArrayClass},new Class[]{Integer.class,stringArrayClass},tc,ArgsMatchKind.EXACT);
|
checkMatch2(new Class[] {Integer.class, stringArrayClass}, new Class[] {Integer.class, stringArrayClass}, tc, ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Passing (String, Integer, String[]) on call to (String, String, String[]) is exact match
|
// Passing (String, Integer, String[]) on call to (String, String, String[]) is exact match
|
||||||
checkMatch2(new Class[]{String.class,Integer.class,stringArrayClass},new Class[]{String.class,Integer.class,stringArrayClass},tc,ArgsMatchKind.EXACT);
|
checkMatch2(new Class[] {String.class, Integer.class, stringArrayClass}, new Class[] {String.class,Integer.class, stringArrayClass}, tc, ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Passing (Sub, String[]) on call to (Super, String[]) is exact match
|
// Passing (Sub, String[]) on call to (Super, String[]) is exact match
|
||||||
checkMatch2(new Class[]{Sub.class,stringArrayClass},new Class[]{Super.class,stringArrayClass},tc,ArgsMatchKind.CLOSE);
|
checkMatch2(new Class[] {Sub.class, stringArrayClass}, new Class[] {Super.class,stringArrayClass}, tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (Integer, String[]) on call to (String, String[]) is exact match
|
// Passing (Integer, String[]) on call to (String, String[]) is exact match
|
||||||
checkMatch2(new Class[]{Integer.class,stringArrayClass},new Class[]{String.class,stringArrayClass},tc,ArgsMatchKind.REQUIRES_CONVERSION,0);
|
checkMatch2(new Class[] {Integer.class, stringArrayClass}, new Class[] {String.class, stringArrayClass}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
|
|
||||||
// Passing (Integer, Sub, String[]) on call to (String, Super, String[]) is exact match
|
// Passing (Integer, Sub, String[]) on call to (String, Super, String[]) is exact match
|
||||||
checkMatch2(new Class[]{Integer.class,Sub.class,String[].class},new Class[]{String.class,Super.class,String[].class},tc,ArgsMatchKind.REQUIRES_CONVERSION,0);
|
checkMatch2(new Class[] {Integer.class, Sub.class, String[].class}, new Class[] {String.class,Super .class, String[].class}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
|
|
||||||
// Passing (String) on call to (String[]) is exact match
|
// Passing (String) on call to (String[]) is exact match
|
||||||
checkMatch2(new Class[]{String.class},new Class[]{stringArrayClass},tc,ArgsMatchKind.EXACT);
|
checkMatch2(new Class[] {String.class}, new Class[] {stringArrayClass}, tc, ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Passing (Integer,String) on call to (Integer,String[]) is exact match
|
// Passing (Integer,String) on call to (Integer,String[]) is exact match
|
||||||
checkMatch2(new Class[]{Integer.class,String.class},new Class[]{Integer.class,stringArrayClass},tc,ArgsMatchKind.EXACT);
|
checkMatch2(new Class[] {Integer.class, String.class}, new Class[] {Integer.class, stringArrayClass}, tc, ArgsMatchKind.EXACT);
|
||||||
|
|
||||||
// Passing (String) on call to (Integer[]) is conversion match (String to Integer)
|
// Passing (String) on call to (Integer[]) is conversion match (String to Integer)
|
||||||
checkMatch2(new Class[]{String.class},new Class[]{integerArrayClass},tc,ArgsMatchKind.REQUIRES_CONVERSION,0);
|
checkMatch2(new Class[] {String.class}, new Class[] {integerArrayClass}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
|
|
||||||
// Passing (Sub) on call to (Super[]) is close match
|
// Passing (Sub) on call to (Super[]) is close match
|
||||||
checkMatch2(new Class[]{Sub.class},new Class[]{new Super[0].getClass()},tc,ArgsMatchKind.CLOSE);
|
checkMatch2(new Class[] {Sub.class}, new Class[] {new Super[0].getClass()}, tc, ArgsMatchKind.CLOSE);
|
||||||
|
|
||||||
// Passing (Super) on call to (Sub[]) is not a match
|
// Passing (Super) on call to (Sub[]) is not a match
|
||||||
checkMatch2(new Class[]{Super.class},new Class[]{new Sub[0].getClass()},tc,null);
|
checkMatch2(new Class[] {Super.class}, new Class[] {new Sub[0].getClass()}, tc, null);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Unconvertable.class,String.class},new Class[]{Sub.class,Super[].class},tc,null);
|
checkMatch2(new Class[] {Unconvertable.class, String.class}, new Class[] {Sub.class, Super[].class}, tc, null);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Integer.class,Integer.class,String.class},new Class[]{String.class,String.class,Super[].class},tc,null);
|
checkMatch2(new Class[] {Integer.class, Integer.class, String.class}, new Class[] {String.class, String.class, Super[].class}, tc, null);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Unconvertable.class,String.class},new Class[]{Sub.class,Super[].class},tc,null);
|
checkMatch2(new Class[] {Unconvertable.class, String.class}, new Class[] {Sub.class, Super[].class}, tc, null);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Integer.class,Integer.class,String.class},new Class[]{String.class,String.class,Super[].class},tc,null);
|
checkMatch2(new Class[] {Integer.class, Integer.class, String.class}, new Class[] {String.class, String.class, Super[].class}, tc, null);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Integer.class,Integer.class,Sub.class},new Class[]{String.class,String.class,Super[].class},tc,ArgsMatchKind.REQUIRES_CONVERSION,0,1);
|
checkMatch2(new Class[] {Integer.class, Integer.class, Sub.class}, new Class[] {String.class, String.class, Super[].class}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
|
|
||||||
checkMatch2(new Class[]{Integer.class,Integer.class,Integer.class},new Class[]{Integer.class,String[].class},tc,ArgsMatchKind.REQUIRES_CONVERSION,1,2);
|
checkMatch2(new Class[] {Integer.class, Integer.class, Integer.class}, new Class[] {Integer.class, String[].class}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
|
||||||
// what happens on (Integer,String) passed to (Integer[]) ?
|
// what happens on (Integer,String) passed to (Integer[]) ?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -216,23 +212,23 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
|
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
|
||||||
|
|
||||||
// basic conversion int>String
|
// basic conversion int>String
|
||||||
Object[] args = new Object[]{3};
|
Object[] args = new Object[] {3};
|
||||||
ReflectionHelper.convertArguments(tc, args, oneArg, new int[]{0}, null);
|
ReflectionHelper.convertArguments(tc, args, oneArg, null);
|
||||||
checkArguments(args, "3");
|
checkArguments(args, "3");
|
||||||
|
|
||||||
// varargs but nothing to convert
|
// varargs but nothing to convert
|
||||||
args = new Object[]{3};
|
args = new Object[] {3};
|
||||||
ReflectionHelper.convertArguments(tc, args, twoArg, new int[]{0}, 1);
|
ReflectionHelper.convertArguments(tc, args, twoArg, 1);
|
||||||
checkArguments(args, "3");
|
checkArguments(args, "3");
|
||||||
|
|
||||||
// varargs with nothing needing conversion
|
// varargs with nothing needing conversion
|
||||||
args = new Object[]{3,"abc","abc"};
|
args = new Object[] {3, "abc", "abc"};
|
||||||
ReflectionHelper.convertArguments(tc, args, twoArg, new int[]{0,1,2}, 1);
|
ReflectionHelper.convertArguments(tc, args, twoArg, 1);
|
||||||
checkArguments(args, "3","abc","abc");
|
checkArguments(args, "3","abc","abc");
|
||||||
|
|
||||||
// varargs with conversion required
|
// varargs with conversion required
|
||||||
args = new Object[]{3,false,3.0d};
|
args = new Object[] {3, false ,3.0d};
|
||||||
ReflectionHelper.convertArguments(tc, args, twoArg, new int[]{0,1,2}, 1);
|
ReflectionHelper.convertArguments(tc, args, twoArg, 1);
|
||||||
checkArguments(args, "3","false","3.0");
|
checkArguments(args, "3","false","3.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -243,22 +239,22 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
|
Method twoArg = TestInterface.class.getMethod("twoArg", String.class, String[].class);
|
||||||
|
|
||||||
// Simple conversion: int to string
|
// Simple conversion: int to string
|
||||||
Object[] args = new Object[]{3};
|
Object[] args = new Object[] {3};
|
||||||
ReflectionHelper.convertAllArguments(tc, args, oneArg);
|
ReflectionHelper.convertAllArguments(tc, args, oneArg);
|
||||||
checkArguments(args,"3");
|
checkArguments(args,"3");
|
||||||
|
|
||||||
// varargs conversion
|
// varargs conversion
|
||||||
args = new Object[]{3,false,3.0f};
|
args = new Object[] {3, false, 3.0f};
|
||||||
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
||||||
checkArguments(args,"3","false","3.0");
|
checkArguments(args,"3","false","3.0");
|
||||||
|
|
||||||
// varargs conversion but no varargs
|
// varargs conversion but no varargs
|
||||||
args = new Object[]{3};
|
args = new Object[] {3};
|
||||||
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
||||||
checkArguments(args,"3");
|
checkArguments(args,"3");
|
||||||
|
|
||||||
// missing converter
|
// missing converter
|
||||||
args = new Object[]{3,false,3.0f};
|
args = new Object[] {3, false, 3.0f};
|
||||||
try {
|
try {
|
||||||
ReflectionHelper.convertAllArguments(null, args, twoArg);
|
ReflectionHelper.convertAllArguments(null, args, twoArg);
|
||||||
fail("Should have failed because no converter supplied");
|
fail("Should have failed because no converter supplied");
|
||||||
|
|
@ -268,16 +264,16 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// null value
|
// null value
|
||||||
args = new Object[]{3,null,3.0f};
|
args = new Object[] {3, null, 3.0f};
|
||||||
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
ReflectionHelper.convertAllArguments(tc, args, twoArg);
|
||||||
checkArguments(args,"3",null,"3.0");
|
checkArguments(args,"3",null,"3.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetupArguments() {
|
public void testSetupArguments() {
|
||||||
Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[]{new String[0].getClass()},"a","b","c");
|
Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {new String[0].getClass()},"a","b","c");
|
||||||
|
|
||||||
assertEquals(1,newArray.length);
|
assertEquals(1, newArray.length);
|
||||||
Object firstParam = newArray[0];
|
Object firstParam = newArray[0];
|
||||||
assertEquals(String.class,firstParam.getClass().getComponentType());
|
assertEquals(String.class,firstParam.getClass().getComponentType());
|
||||||
Object[] firstParamArray = (Object[])firstParam;
|
Object[] firstParamArray = (Object[])firstParam;
|
||||||
|
|
@ -362,13 +358,15 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
try {
|
try {
|
||||||
optA.canWrite(ctx, t, "property");
|
optA.canWrite(ctx, t, "property");
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
optA.canWrite(ctx, t, "property2");
|
optA.canWrite(ctx, t, "property2");
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
assertEquals("hello",optA.read(ctx, t, "property").getValue());
|
assertEquals("hello",optA.read(ctx, t, "property").getValue());
|
||||||
|
|
@ -377,13 +375,15 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
try {
|
try {
|
||||||
optA.getSpecificTargetClasses();
|
optA.getSpecificTargetClasses();
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
optA.write(ctx,t,"property",null);
|
optA.write(ctx,t,"property",null);
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -393,13 +393,15 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
try {
|
try {
|
||||||
optA.canWrite(ctx, t, "field");
|
optA.canWrite(ctx, t, "field");
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
optA.canWrite(ctx, t, "field2");
|
optA.canWrite(ctx, t, "field2");
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
assertEquals(3,optA.read(ctx, t, "field").getValue());
|
assertEquals(3,optA.read(ctx, t, "field").getValue());
|
||||||
|
|
@ -408,17 +410,17 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
try {
|
try {
|
||||||
optA.getSpecificTargetClasses();
|
optA.getSpecificTargetClasses();
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
optA.write(ctx,t,"field",null);
|
optA.write(ctx,t,"field",null);
|
||||||
fail();
|
fail();
|
||||||
} catch (UnsupportedOperationException uoe) {
|
}
|
||||||
|
catch (UnsupportedOperationException uoe) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -479,64 +481,52 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
/**
|
/**
|
||||||
* Used to validate the match returned from a compareArguments call.
|
* Used to validate the match returned from a compareArguments call.
|
||||||
*/
|
*/
|
||||||
private void checkMatch(Class[] inputTypes, Class[] expectedTypes, StandardTypeConverter typeConverter,ArgsMatchKind expectedMatchKind,int... argsForConversion) {
|
private void checkMatch(Class[] inputTypes, Class[] expectedTypes, StandardTypeConverter typeConverter, ArgsMatchKind expectedMatchKind) {
|
||||||
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArguments(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
|
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArguments(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
|
||||||
if (expectedMatchKind==null) {
|
if (expectedMatchKind == null) {
|
||||||
assertNull("Did not expect them to match in any way", matchInfo);
|
assertNull("Did not expect them to match in any way", matchInfo);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
assertNotNull("Should not be a null match", matchInfo);
|
assertNotNull("Should not be a null match", matchInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expectedMatchKind==ArgsMatchKind.EXACT) {
|
if (expectedMatchKind == ArgsMatchKind.EXACT) {
|
||||||
assertTrue(matchInfo.isExactMatch());
|
assertTrue(matchInfo.isExactMatch());
|
||||||
assertNull(matchInfo.argsRequiringConversion);
|
}
|
||||||
} else if (expectedMatchKind==ArgsMatchKind.CLOSE) {
|
else if (expectedMatchKind == ArgsMatchKind.CLOSE) {
|
||||||
assertTrue(matchInfo.isCloseMatch());
|
assertTrue(matchInfo.isCloseMatch());
|
||||||
assertNull(matchInfo.argsRequiringConversion);
|
}
|
||||||
} else if (expectedMatchKind==ArgsMatchKind.REQUIRES_CONVERSION) {
|
else if (expectedMatchKind == ArgsMatchKind.REQUIRES_CONVERSION) {
|
||||||
assertTrue("expected to be a match requiring conversion, but was "+matchInfo,matchInfo.isMatchRequiringConversion());
|
assertTrue("expected to be a match requiring conversion, but was " + matchInfo, matchInfo.isMatchRequiringConversion());
|
||||||
if (argsForConversion==null) {
|
|
||||||
fail("there are arguments that need conversion");
|
|
||||||
}
|
|
||||||
assertEquals("The array of args that need conversion is different length to that expected",argsForConversion.length, matchInfo.argsRequiringConversion.length);
|
|
||||||
for (int a=0;a<argsForConversion.length;a++) {
|
|
||||||
assertEquals(argsForConversion[a],matchInfo.argsRequiringConversion[a]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to validate the match returned from a compareArguments call.
|
* Used to validate the match returned from a compareArguments call.
|
||||||
*/
|
*/
|
||||||
private void checkMatch2(Class[] inputTypes, Class[] expectedTypes, StandardTypeConverter typeConverter,ArgsMatchKind expectedMatchKind,int... argsForConversion) {
|
private void checkMatch2(Class[] inputTypes, Class[] expectedTypes, StandardTypeConverter typeConverter, ArgsMatchKind expectedMatchKind) {
|
||||||
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArgumentsVarargs(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
|
ReflectionHelper.ArgumentsMatchInfo matchInfo = ReflectionHelper.compareArgumentsVarargs(getTypeDescriptors(expectedTypes), getTypeDescriptors(inputTypes), typeConverter);
|
||||||
if (expectedMatchKind==null) {
|
if (expectedMatchKind == null) {
|
||||||
assertNull("Did not expect them to match in any way: "+matchInfo, matchInfo);
|
assertNull("Did not expect them to match in any way: "+matchInfo, matchInfo);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
assertNotNull("Should not be a null match", matchInfo);
|
assertNotNull("Should not be a null match", matchInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expectedMatchKind==ArgsMatchKind.EXACT) {
|
if (expectedMatchKind == ArgsMatchKind.EXACT) {
|
||||||
assertTrue(matchInfo.isExactMatch());
|
assertTrue(matchInfo.isExactMatch());
|
||||||
assertNull(matchInfo.argsRequiringConversion);
|
}
|
||||||
} else if (expectedMatchKind==ArgsMatchKind.CLOSE) {
|
else if (expectedMatchKind == ArgsMatchKind.CLOSE) {
|
||||||
assertTrue(matchInfo.isCloseMatch());
|
assertTrue(matchInfo.isCloseMatch());
|
||||||
assertNull(matchInfo.argsRequiringConversion);
|
}
|
||||||
} else if (expectedMatchKind==ArgsMatchKind.REQUIRES_CONVERSION) {
|
else if (expectedMatchKind == ArgsMatchKind.REQUIRES_CONVERSION) {
|
||||||
assertTrue("expected to be a match requiring conversion, but was "+matchInfo,matchInfo.isMatchRequiringConversion());
|
assertTrue("expected to be a match requiring conversion, but was " + matchInfo, matchInfo.isMatchRequiringConversion());
|
||||||
if (argsForConversion==null) {
|
|
||||||
fail("there are arguments that need conversion");
|
|
||||||
}
|
|
||||||
assertEquals("The array of args that need conversion is different length to that expected",argsForConversion.length, matchInfo.argsRequiringConversion.length);
|
|
||||||
for (int a=0;a<argsForConversion.length;a++) {
|
|
||||||
assertEquals(argsForConversion[a],matchInfo.argsRequiringConversion[a]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkArguments(Object[] args, Object... expected) {
|
private void checkArguments(Object[] args, Object... expected) {
|
||||||
assertEquals(expected.length,args.length);
|
assertEquals(expected.length,args.length);
|
||||||
for (int i=0;i<expected.length;i++) {
|
for (int i = 0; i < expected.length; i++) {
|
||||||
checkArgument(expected[i],args[i]);
|
checkArgument(expected[i],args[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue