提交 0fb63134 编写于 作者: K Keith Donald

switched to better encapsulated convert(Object, TypeDescriptor) where possible

上级 bb4aa9cc
......@@ -190,7 +190,7 @@ public class FormattingConversionServiceTests {
@Test
public void testParseEmptyString() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter());
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(Integer.class)));
}
@Test
......@@ -205,7 +205,7 @@ public class FormattingConversionServiceTests {
@Test
public void testParseEmptyStringDefault() throws ParseException {
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
assertNull(formattingService.convert("", TypeDescriptor.valueOf(Integer.class)));
}
......
......@@ -64,4 +64,10 @@ public interface ConversionService {
*/
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
}
// 3.1 additions that encapsulate TypeDescriptor.forObject(source)
boolean canConvert(Object source, TypeDescriptor targetType);
Object convert(Object source, TypeDescriptor targetType);
}
\ No newline at end of file
......@@ -56,7 +56,7 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(this.conversionService.convert(text, TypeDescriptor.valueOf(String.class), this.targetDescriptor));
setValue(this.conversionService.convert(text, this.targetDescriptor));
}
@Override
......
......@@ -188,6 +188,14 @@ public class GenericConversionService implements ConversionService, ConverterReg
return result;
}
public boolean canConvert(Object source, TypeDescriptor targetType) {
return canConvert(TypeDescriptor.forObject(source), targetType);
}
public Object convert(Object source, TypeDescriptor targetType) {
return convert(source, TypeDescriptor.forObject(source), targetType);
}
public String toString() {
List<String> converterStrings = new ArrayList<String>();
for (Map<Class<?>, MatchableConverters> targetConverters : this.converters.values()) {
......
......@@ -287,8 +287,7 @@ public class DefaultConversionTests {
@Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1));
assertEquals(new Integer("3"), result.get(2));
......@@ -298,7 +297,7 @@ public class DefaultConversionTests {
public void testSpr7766() throws Exception {
ConverterRegistry registry = ((ConverterRegistry) conversionService);
registry.addConverter(new ColorConverter());
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
assertEquals(2, colors.size());
assertEquals(Color.WHITE, colors.get(0));
assertEquals(Color.BLACK, colors.get(1));
......@@ -531,8 +530,7 @@ public class DefaultConversionTests {
foo.add("1");
foo.add("2");
foo.add("3");
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo),
new TypeDescriptor(getClass().getField("genericList")));
List<Integer> bar = (List<Integer>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericList")));
assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1));
assertEquals(new Integer(3), bar.get(2));
......@@ -540,8 +538,7 @@ public class DefaultConversionTests {
@Test
public void convertCollectionToCollectionNull() throws Exception {
List<Integer> bar = (List<Integer>) conversionService.convert(null,
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
List<Integer> bar = (List<Integer>) conversionService.convert(null, new TypeDescriptor(getClass().getField("genericList")));
assertNull(bar);
}
......@@ -551,8 +548,7 @@ public class DefaultConversionTests {
foo.add("1");
foo.add("2");
foo.add("3");
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(LinkedHashSet.class), TypeDescriptor
.valueOf(List.class));
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(List.class));
assertEquals("1", bar.get(0));
assertEquals("2", bar.get(1));
assertEquals("3", bar.get(2));
......@@ -565,8 +561,7 @@ public class DefaultConversionTests {
map.put("2", "2");
map.put("3", "3");
Collection values = map.values();
List<Integer> bar = (List<Integer>) conversionService.convert(values,
TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
List<Integer> bar = (List<Integer>) conversionService.convert(values, new TypeDescriptor(getClass().getField("genericList")));
assertEquals(3, bar.size());
assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1));
......@@ -580,8 +575,7 @@ public class DefaultConversionTests {
Map<String, String> foo = new HashMap<String, String>();
foo.put("1", "BAR");
foo.put("2", "BAZ");
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo,
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, map.get(1));
assertEquals(FooEnum.BAZ, map.get(2));
}
......
......@@ -109,12 +109,13 @@ public class GenericConversionServiceTests {
}
public void convertNullTargetClass() {
assertNull(conversionService.convert("3", null));
assertNull(conversionService.convert("3", (Class) null));
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
}
@Test
public void convertNullTypeDescriptor() {
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
}
@Test
......@@ -147,10 +148,8 @@ public class GenericConversionServiceTests {
assertTrue(conversionService.canConvert(String.class, boolean.class));
Boolean b = conversionService.convert("true", boolean.class);
assertEquals(Boolean.TRUE, b);
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor
.valueOf(boolean.class)));
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor
.valueOf(boolean.class));
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(boolean.class));
assertEquals(Boolean.TRUE, b);
}
......@@ -260,8 +259,7 @@ public class GenericConversionServiceTests {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
Map<String, String> input = new LinkedHashMap<String, String>();
input.put("key", "value");
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input),
new TypeDescriptor(getClass().getField("wildcardMap")));
Object converted = conversionService.convert(input, new TypeDescriptor(getClass().getField("wildcardMap")));
assertEquals(input, converted);
}
......@@ -344,7 +342,7 @@ public class GenericConversionServiceTests {
source.add("3");
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
for (int i = 0; i < 1000000; i++) {
conversionService.convert(source, TypeDescriptor.forObject(source), td);
conversionService.convert(source, td);
}
watch.stop();
watch.start("convert 4,000,000 manually");
......@@ -371,7 +369,7 @@ public class GenericConversionServiceTests {
source.put("3", "3");
TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
for (int i = 0; i < 1000000; i++) {
conversionService.convert(source, TypeDescriptor.forObject(source), td);
conversionService.convert(source, td);
}
watch.stop();
watch.start("convert 4,000,000 manually");
......
......@@ -16,14 +16,16 @@
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.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;
......@@ -141,7 +143,7 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
private final ConversionService service = ConversionServiceFactory.createDefaultConversionService();
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
return this.service.convert(value, TypeDescriptor.forObject(value), typeDescriptor);
return this.service.convert(value, typeDescriptor);
}
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册