implemented collection/map converter conditional matching checks; updated SpEL to reflect this behavior

This commit is contained in:
Keith Donald
2011-06-07 20:00:28 +00:00
parent df460f4486
commit 1e39b0bbbc
22 changed files with 239 additions and 71 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.expression.spel.ast;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@@ -123,15 +124,12 @@ public class ConstructorReference extends SpelNodeImpl {
// In the first case we should not retry, in the second case we should see if there is a
// better suited method.
// To determine which situation it is, the AccessException will contain a cause - this
// will be the exception thrown by the reflective invocation. Inside this exception there
// may or may not be a root cause. If there is a root cause it is a user created exception.
// If there is no root cause it was a reflective invocation problem.
Throwable causeOfAccessException = ae.getCause();
Throwable rootCause = (causeOfAccessException == null ? null : causeOfAccessException.getCause());
if (rootCause != null) {
// To determine which situation it is, the AccessException will contain a cause.
// If the cause is an InvocationTargetException, a user exception was thrown inside the constructor.
// Otherwise the constructor could not be invoked.
if (ae.getCause() instanceof InvocationTargetException) {
// User exception was the root cause - exit now
Throwable rootCause = ae.getCause().getCause();
if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
} else {
@@ -139,7 +137,7 @@ public class ConstructorReference extends SpelNodeImpl {
throw new SpelEvaluationException(getStartPosition(), rootCause,
SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename, FormatHelper
.formatMethodForMessage("", argumentTypes));
}
}
}
// at this point we know it wasn't a user problem so worth a retry if a better candidate can be found

View File

@@ -16,6 +16,7 @@
package org.springframework.expression.spel.ast;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@@ -91,11 +92,9 @@ public class MethodReference extends SpelNodeImpl {
// In the first case we should not retry, in the second case we should see if there is a
// better suited method.
// To determine which situation it is, the AccessException will contain a cause - this
// will be the exception thrown by the reflective invocation. Inside this exception there
// may or may not be a root cause. If there is a root cause it is a user created exception.
// If there is no root cause it was a reflective invocation problem.
// To determine which situation it is, the AccessException will contain a cause.
// If the cause is an InvocationTargetException, a user exception was thrown inside the method.
// Otherwise the method could not be invoked.
throwSimpleExceptionIfPossible(state, ae);
// at this point we know it wasn't a user problem so worth a retry if a better candidate can be found
@@ -123,19 +122,17 @@ public class MethodReference extends SpelNodeImpl {
* throw the RuntimeException directly.
*/
private void throwSimpleExceptionIfPossible(ExpressionState state, AccessException ae) {
Throwable causeOfAccessException = ae.getCause();
Throwable rootCause = (causeOfAccessException==null?null:causeOfAccessException.getCause());
if (rootCause!=null) {
// User exception was the root cause - exit now
if (ae.getCause() instanceof InvocationTargetException) {
Throwable rootCause = ae.getCause().getCause();
if (rootCause instanceof RuntimeException) {
throw (RuntimeException)rootCause;
throw (RuntimeException) rootCause;
}
else {
throw new ExpressionInvocationTargetException( getStartPosition(),
throw new ExpressionInvocationTargetException(getStartPosition(),
"A problem occurred when trying to execute method '" + this.name +
"' on object of type '" + state.getActiveContextObject().getValue().getClass().getName() + "'",
rootCause);
}
}
}
}

View File

@@ -268,24 +268,36 @@ public class ReflectionHelper {
* @param converter the type converter to use for attempting conversions
* @param arguments the actual arguments that need conversion
* @param methodOrCtor the target Method or Constructor
* @param argumentsRequiringConversion details which of the input arguments need conversion
* @param argumentsRequiringConversion details which of the input arguments for sure need conversion
* @param varargsPosition the known position of the varargs argument, if any
* @throws EvaluationException if a problem occurs during conversion
*/
static void convertArguments(TypeConverter converter, Object[] arguments, Object methodOrCtor,
int[] argumentsRequiringConversion, Integer varargsPosition) throws EvaluationException {
for (int argPosition : argumentsRequiringConversion) {
TypeDescriptor targetType;
if (varargsPosition != null && argPosition >= varargsPosition) {
MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, varargsPosition);
targetType = TypeDescriptor.nested(methodParam, 1);
if (varargsPosition == null) {
for (int i = 0; i < arguments.length; i++) {
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
Object argument = arguments[i];
arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
}
else {
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
} else {
for (int i = 0; i < varargsPosition; i++) {
TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, i));
Object argument = arguments[i];
arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
}
MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, varargsPosition);
if (varargsPosition == arguments.length - 1) {
TypeDescriptor targetType = new TypeDescriptor(methodParam);
Object argument = arguments[varargsPosition];
arguments[varargsPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
} else {
TypeDescriptor targetType = TypeDescriptor.nested(methodParam, 1);
for (int i = varargsPosition; i < arguments.length; i++) {
Object argument = arguments[i];
arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
}
}
Object argument = arguments[argPosition];
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
}
}

View File

@@ -56,7 +56,7 @@ class ReflectiveConstructorExecutor implements ConstructorExecutor {
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
try {
if (this.argsRequiringConversion != null && arguments != null) {
if (arguments != null) {
ReflectionHelper.convertArguments(context.getTypeConverter(), arguments,
this.ctor, this.argsRequiringConversion, this.varargsPosition);
}

View File

@@ -57,7 +57,7 @@ class ReflectiveMethodExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
try {
if (this.argsRequiringConversion != null && arguments != null) {
if (arguments != null) {
ReflectionHelper.convertArguments(
context.getTypeConverter(), arguments, this.method,
this.argsRequiringConversion, this.varargsPosition);