removed ConversionService/TypeConverter convenience methods in order to restore 3.0's SPI (for backwards compatibility with implementers)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,7 +41,7 @@ public interface TypeConverter {
|
||||
/**
|
||||
* Convert (may coerce) a value from one type to another, for example from a boolean to a string.
|
||||
* The typeDescriptor parameter enables support for typed collections - if the caller really wishes they
|
||||
* can have a List<Integer> for example, rather than simply a List.
|
||||
* can have a List<Integer> for example, rather than simply a List.
|
||||
* @param value the value to be converted
|
||||
* @param sourceType a type descriptor that supplies extra information about the source object
|
||||
* @param targetType a type descriptor that supplies extra information about the requested result type
|
||||
@@ -50,8 +50,4 @@ public interface TypeConverter {
|
||||
*/
|
||||
Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
|
||||
|
||||
// 3.1 additions for encapsulation of TypeDescriptor.forObject(value);
|
||||
|
||||
Object convertValue(Object value, TypeDescriptor targetType);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -141,7 +141,8 @@ public class ExpressionState {
|
||||
}
|
||||
|
||||
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
|
||||
return this.relatedContext.getTypeConverter().convertValue(value.getValue(), targetTypeDescriptor);
|
||||
Object val = value.getValue();
|
||||
return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,8 +35,9 @@ import org.springframework.expression.spel.SpelMessage;
|
||||
import org.springframework.expression.spel.SpelNode;
|
||||
|
||||
/**
|
||||
* Represents the invocation of a constructor. Either a constructor on a regular type or construction of an array. When
|
||||
* an array is constructed, an initializer can be specified.
|
||||
* Represents the invocation of a constructor. Either a constructor on a regular type or
|
||||
* construction of an array. When an array is constructed, an initializer can be specified.
|
||||
*
|
||||
* <p>
|
||||
* Examples:<br>
|
||||
* new String('hello world')<br>
|
||||
@@ -322,7 +323,7 @@ public class ConstructorReference extends SpelNodeImpl {
|
||||
for (int i = 0; i < newObjectArray.length; i++) {
|
||||
SpelNode elementNode = initializer.getChild(i);
|
||||
Object arrayEntry = elementNode.getValue(state);
|
||||
newObjectArray[i] = typeConverter.convertValue(arrayEntry, toTypeDescriptor);
|
||||
newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -241,7 +241,8 @@ public class ReflectionHelper {
|
||||
else {
|
||||
targetType = new TypeDescriptor(MethodParameter.forMethodOrConstructor(methodOrCtor, argPosition));
|
||||
}
|
||||
arguments[argPosition] = converter.convertValue(arguments[argPosition], targetType);
|
||||
Object argument = arguments[argPosition];
|
||||
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +279,7 @@ public class ReflectionHelper {
|
||||
if (converter == null) {
|
||||
throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
|
||||
}
|
||||
arguments[argPosition] = converter.convertValue(argument, targetType);
|
||||
arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
|
||||
}
|
||||
}
|
||||
catch (EvaluationException ex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -218,7 +218,8 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||
TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
|
||||
if (typeDescriptor != null) {
|
||||
try {
|
||||
possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, typeDescriptor);
|
||||
possiblyConvertedNewValue = context.getTypeConverter().convertValue(
|
||||
newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
|
||||
}
|
||||
catch (EvaluationException evaluationException) {
|
||||
throw new AccessException("Type conversion failure",evaluationException);
|
||||
|
||||
@@ -75,8 +75,4 @@ public class StandardTypeConverter implements TypeConverter {
|
||||
}
|
||||
}
|
||||
|
||||
public Object convertValue(Object value, TypeDescriptor targetType) {
|
||||
return convertValue(value, TypeDescriptor.forObject(value), targetType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,16 +16,15 @@
|
||||
|
||||
package org.springframework.expression.spel;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
@@ -75,14 +74,14 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||
// ArrayList containing List<Integer> to List<String>
|
||||
Class<?> clazz = typeDescriptorForListOfString.getElementType();
|
||||
assertEquals(String.class,clazz);
|
||||
List l = (List) tcs.convertValue(listOfInteger, typeDescriptorForListOfString);
|
||||
List l = (List) tcs.convertValue(listOfInteger, TypeDescriptor.forObject(listOfInteger), typeDescriptorForListOfString);
|
||||
assertNotNull(l);
|
||||
|
||||
// ArrayList containing List<String> to List<Integer>
|
||||
clazz = typeDescriptorForListOfInteger.getElementType();
|
||||
assertEquals(Integer.class,clazz);
|
||||
|
||||
l = (List) tcs.convertValue(listOfString, typeDescriptorForListOfString);
|
||||
l = (List) tcs.convertValue(listOfString, TypeDescriptor.forObject(listOfString), typeDescriptorForListOfString);
|
||||
assertNotNull(l);
|
||||
}
|
||||
|
||||
@@ -121,8 +120,7 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||
assertTrue(evaluationContext.getTypeConverter()
|
||||
.canConvert(TypeDescriptor.valueOf(String.class), collectionType));
|
||||
// ... and it can be done successfully
|
||||
assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", collectionType).toString());
|
||||
|
||||
assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString());
|
||||
|
||||
evaluationContext.setVariable("target", new TestTarget());
|
||||
|
||||
@@ -133,6 +131,47 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||
|
||||
}
|
||||
|
||||
public static class Foo {
|
||||
|
||||
private Collection<Foo> foos;
|
||||
|
||||
public final String value;
|
||||
|
||||
public Foo(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setFoos(Collection<Foo> foos) {
|
||||
this.foos = foos;
|
||||
}
|
||||
|
||||
public Collection<Foo> getFoos() {
|
||||
return this.foos;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert() {
|
||||
Foo root = new Foo("bar");
|
||||
StandardEvaluationContext context = new StandardEvaluationContext(root);
|
||||
|
||||
Collection<String> foos = Collections.singletonList("baz");
|
||||
|
||||
// property access, works
|
||||
Expression expression = parser.parseExpression("foos");
|
||||
expression.setValue(context, foos);
|
||||
Foo baz = root.getFoos().iterator().next();
|
||||
assertEquals("baz", baz.value);
|
||||
|
||||
// method call, fails (ClassCastException)
|
||||
expression = parser.parseExpression("setFoos(#foos)");
|
||||
context.setVariable("foos", foos);
|
||||
expression.getValue(context);
|
||||
baz = root.getFoos().iterator().next();
|
||||
assertEquals("baz", baz.value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Type converter that uses the core conversion service.
|
||||
@@ -141,10 +180,6 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||
|
||||
private final ConversionService service = new DefaultConversionService();
|
||||
|
||||
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
|
||||
return this.service.convert(value, typeDescriptor);
|
||||
}
|
||||
|
||||
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
return this.service.canConvert(sourceType, targetType);
|
||||
}
|
||||
@@ -152,7 +187,6 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
|
||||
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) throws EvaluationException {
|
||||
return this.service.convert(value, sourceType, targetType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user