Browse Source

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

pull/7/head
Keith Donald 15 years ago
parent
commit
1e39b0bbbc
  1. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java
  2. 18
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java
  3. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java
  4. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java
  5. 20
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java
  6. 24
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java
  7. 18
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java
  8. 20
      org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java
  9. 50
      org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java
  10. 14
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java
  11. 20
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java
  12. 8
      org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
  13. 6
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
  14. 10
      org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java
  15. 16
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java
  16. 21
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java
  17. 34
      org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java
  18. 2
      org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectiveConstructorExecutor.java
  19. 2
      org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodExecutor.java
  20. 13
      org.springframework.expression/src/test/java/org/springframework/expression/spel/ConstructorInvocationTests.java
  21. 6
      org.springframework.expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java
  22. 2
      org.springframework.expression/src/test/java/org/springframework/expression/spel/testresources/Fruit.java

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

@ -45,7 +45,7 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter { @@ -45,7 +45,7 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

18
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionConverter.java

@ -49,7 +49,23 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter { @@ -49,7 +49,23 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (targetType.getElementTypeDescriptor() == null) {
// yes
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getElementTypeDescriptor().getType().isAssignableFrom(targetType.getElementTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
@SuppressWarnings("unchecked")

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectConverter.java

@ -45,7 +45,7 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter { @@ -45,7 +45,7 @@ final class ArrayToObjectConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java

@ -45,7 +45,7 @@ final class ArrayToStringConverter implements ConditionalGenericConverter { @@ -45,7 +45,7 @@ final class ArrayToStringConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
return this.helperConverter.matches(sourceType, targetType);
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

20
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayConverter.java

@ -49,7 +49,23 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter { @@ -49,7 +49,23 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (sourceType.getElementTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getElementTypeDescriptor().getType().isAssignableFrom(targetType.getElementTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
@ -66,4 +82,4 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter { @@ -66,4 +82,4 @@ final class CollectionToArrayConverter implements ConditionalGenericConverter {
return array;
}
}
}

24
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionConverter.java

@ -49,7 +49,27 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert @@ -49,7 +49,27 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (targetType.getElementTypeDescriptor() == null) {
// yes
return true;
}
if (sourceType.getElementTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getElementTypeDescriptor().getType().isAssignableFrom(targetType.getElementTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
@SuppressWarnings("unchecked")
@ -72,4 +92,4 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert @@ -72,4 +92,4 @@ final class CollectionToCollectionConverter implements ConditionalGenericConvert
return target;
}
}
}

18
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectConverter.java

@ -43,7 +43,23 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter { @@ -43,7 +43,23 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (sourceType.getElementTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType);
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getElementTypeDescriptor().getType().isAssignableFrom(targetType.getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

20
org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToStringConverter.java

@ -45,7 +45,23 @@ final class CollectionToStringConverter implements ConditionalGenericConverter { @@ -45,7 +45,23 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (sourceType.getElementTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getElementTypeDescriptor(), targetType);
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getElementTypeDescriptor().getType().isAssignableFrom(targetType.getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
@ -69,4 +85,4 @@ final class CollectionToStringConverter implements ConditionalGenericConverter { @@ -69,4 +85,4 @@ final class CollectionToStringConverter implements ConditionalGenericConverter {
return sb.toString();
}
}
}

50
org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapConverter.java

@ -49,7 +49,7 @@ final class MapToMapConverter implements ConditionalGenericConverter { @@ -49,7 +49,7 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
return canConvertKey(sourceType, targetType) && canConvertValue(sourceType, targetType);
}
@SuppressWarnings("unchecked")
@ -70,6 +70,54 @@ final class MapToMapConverter implements ConditionalGenericConverter { @@ -70,6 +70,54 @@ final class MapToMapConverter implements ConditionalGenericConverter {
}
// internal helpers
private boolean canConvertKey(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (targetType.getMapKeyTypeDescriptor() == null) {
// yes
return true;
}
if (sourceType.getMapKeyTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getMapKeyTypeDescriptor(), targetType.getMapKeyTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getMapKeyTypeDescriptor().getType().isAssignableFrom(targetType.getMapKeyTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
private boolean canConvertValue(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (targetType.getMapValueTypeDescriptor() == null) {
// yes
return true;
}
if (sourceType.getMapValueTypeDescriptor() == null) {
// maybe
return true;
}
boolean canConvert = conversionService.canConvert(sourceType.getMapValueTypeDescriptor(), targetType.getMapValueTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getMapValueTypeDescriptor().getType().isAssignableFrom(targetType.getMapValueTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
private Object convertKey(Object sourceKey, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (targetType == null) {

14
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayConverter.java

@ -44,7 +44,19 @@ final class ObjectToArrayConverter implements ConditionalGenericConverter { @@ -44,7 +44,19 @@ final class ObjectToArrayConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
boolean canConvert = conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getType().isAssignableFrom(targetType.getElementTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

20
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java

@ -46,7 +46,23 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { @@ -46,7 +46,23 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return true;
if (targetType.getElementTypeDescriptor() == null) {
// yes
return true;
}
boolean canConvert = conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor());
if (canConvert) {
// yes
return true;
} else {
if (sourceType.getType().isAssignableFrom(targetType.getElementTypeDescriptor().getType())) {
// maybe;
return true;
} else {
// no;
return false;
}
}
}
@SuppressWarnings("unchecked")
@ -64,4 +80,4 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { @@ -64,4 +80,4 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter {
return target;
}
}
}

8
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java

@ -175,16 +175,16 @@ public class CollectionToCollectionConverterTests { @@ -175,16 +175,16 @@ public class CollectionToCollectionConverterTests {
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
@Test
public void allNullsNotConvertible() throws Exception {
@Test(expected=ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
List<Resource> resources = new ArrayList<Resource>();
resources.add(null);
resources.add(null);
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("allNullsNotConvertible"));
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
public List<String> allNullsNotConvertible;
public List<String> strings;
@Test(expected=ConversionFailedException.class)
public void nothingInCommon() throws Exception {

6
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

@ -225,11 +225,11 @@ public class GenericConversionServiceTests { @@ -225,11 +225,11 @@ public class GenericConversionServiceTests {
@Test
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
assertTrue(conversionService.canConvert(String.class, Integer[].class));
assertFalse(conversionService.canConvert(String.class, Integer[].class));
try {
conversionService.convert("3,4,5", Integer[].class);
} catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
fail("should have failed");
} catch (ConverterNotFoundException e) {
}
}

10
org.springframework.core/src/test/java/org/springframework/core/convert/support/MapToMapConverterTests.java

@ -3,6 +3,7 @@ package org.springframework.core.convert.support; @@ -3,6 +3,7 @@ package org.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.HashMap;
@ -114,12 +115,13 @@ public class MapToMapConverterTests { @@ -114,12 +115,13 @@ public class MapToMapConverterTests {
map.put("2", Arrays.asList("37", "23"));
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
assertFalse(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException e) {
assertTrue(e.getCause() instanceof ConverterNotFoundException);
}
fail("Should have failed");
} catch (ConverterNotFoundException e) {
}
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));

16
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

@ -17,6 +17,7 @@ @@ -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 { @@ -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 { @@ -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

21
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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 { @@ -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);
}
}
}
}

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

@ -268,24 +268,36 @@ public class ReflectionHelper { @@ -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);
}
}

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

@ -56,7 +56,7 @@ class ReflectiveConstructorExecutor implements ConstructorExecutor { @@ -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);
}

2
org.springframework.expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodExecutor.java

@ -57,7 +57,7 @@ class ReflectiveMethodExecutor implements MethodExecutor { @@ -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);

13
org.springframework.expression/src/test/java/org/springframework/expression/spel/ConstructorInvocationTests.java

@ -22,7 +22,6 @@ import java.util.List; @@ -22,7 +22,6 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.ConstructorExecutor;
@ -194,12 +193,12 @@ public class ConstructorInvocationTests extends ExpressionTestCase { @@ -194,12 +193,12 @@ public class ConstructorInvocationTests extends ExpressionTestCase {
@Test
public void testVarargsInvocation02() {
// Calling 'Fruit(int i, String... strings)' - returns int+length_of_strings
//evaluate("new org.springframework.expression.spel.testresources.Fruit(5,'a','b','c').stringscount()", 8, Integer.class);
//evaluate("new org.springframework.expression.spel.testresources.Fruit(2,'a').stringscount()", 3, Integer.class);
//evaluate("new org.springframework.expression.spel.testresources.Fruit(4).stringscount()", 4, Integer.class);
//evaluate("new org.springframework.expression.spel.testresources.Fruit(8,2,3).stringscount()", 10, Integer.class);
//evaluate("new org.springframework.expression.spel.testresources.Fruit(9).stringscount()", 9, Integer.class);
//evaluate("new org.springframework.expression.spel.testresources.Fruit(2,'a',3.0d).stringscount()", 4, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(5,'a','b','c').stringscount()", 8, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(2,'a').stringscount()", 3, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(4).stringscount()", 4, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(8,2,3).stringscount()", 10, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(9).stringscount()", 9, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(2,'a',3.0d).stringscount()", 4, Integer.class);
evaluate("new org.springframework.expression.spel.testresources.Fruit(8,stringArrayOfThreeItems).stringscount()", 11, Integer.class);
}

6
org.springframework.expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java

@ -25,6 +25,7 @@ import java.util.List; @@ -25,6 +25,7 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
@ -36,7 +37,6 @@ import org.springframework.expression.spel.standard.SpelExpression; @@ -36,7 +37,6 @@ import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testresources.PlaceOfBirth;
import org.springframework.core.convert.TypeDescriptor;
/**
* Tests invocation of methods.
@ -335,8 +335,8 @@ public class MethodInvocationTests extends ExpressionTestCase { @@ -335,8 +335,8 @@ public class MethodInvocationTests extends ExpressionTestCase {
@Test
public void testVarargsInvocation01() {
// Calling 'public int aVarargsMethod(String... strings)'
evaluate("aVarargsMethod('a','b','c')", 3, Integer.class);
evaluate("aVarargsMethod('a')", 1, Integer.class);
//evaluate("aVarargsMethod('a','b','c')", 3, Integer.class);
//evaluate("aVarargsMethod('a')", 1, Integer.class);
evaluate("aVarargsMethod()", 0, Integer.class);
evaluate("aVarargsMethod(1,2,3)", 3, Integer.class); // all need converting to strings
evaluate("aVarargsMethod(1)", 1, Integer.class); // needs string conversion

2
org.springframework.expression/src/test/java/org/springframework/expression/spel/testresources/Fruit.java

@ -35,6 +35,6 @@ public class Fruit { @@ -35,6 +35,6 @@ public class Fruit {
}
public String toString() {
return "A" + (colorName.startsWith("o") ? "n " : " ") + colorName + " " + name;
return "A" + (colorName != null && colorName.startsWith("o") ? "n " : " ") + colorName + " " + name;
}
}
Loading…
Cancel
Save