Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3880 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Keith Donald 15 years ago
parent
commit
97033d66fb
  1. 4
      org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java
  2. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java
  3. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java
  4. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  5. 20
      org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java
  6. 18
      org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
  7. 8
      org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionTestsUsingCoreConversionService.java

4
org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java

@ -190,7 +190,7 @@ public class FormattingConversionServiceTests {
@Test @Test
public void testParseEmptyString() throws ParseException { public void testParseEmptyString() throws ParseException {
formattingService.addFormatterForFieldType(Number.class, new NumberFormatter()); 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 @Test
@ -205,7 +205,7 @@ public class FormattingConversionServiceTests {
@Test @Test
public void testParseEmptyStringDefault() throws ParseException { public void testParseEmptyStringDefault() throws ParseException {
assertNull(formattingService.convert("", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); assertNull(formattingService.convert("", TypeDescriptor.valueOf(Integer.class)));
} }

8
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java

@ -64,4 +64,10 @@ public interface ConversionService {
*/ */
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); 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);
}

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

@ -56,7 +56,7 @@ public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { 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 @Override

8
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -188,6 +188,14 @@ public class GenericConversionService implements ConversionService, ConverterReg
return result; 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() { public String toString() {
List<String> converterStrings = new ArrayList<String>(); List<String> converterStrings = new ArrayList<String>();
for (Map<Class<?>, MatchableConverters> targetConverters : this.converters.values()) { for (Map<Class<?>, MatchableConverters> targetConverters : this.converters.values()) {

20
org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java

@ -287,8 +287,7 @@ public class DefaultConversionTests {
@Test @Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception { public void convertArrayToCollectionGenericTypeConversion() throws Exception {
List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, TypeDescriptor List<Integer> result = (List<Integer>) conversionService.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
assertEquals(new Integer("1"), result.get(0)); assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1)); assertEquals(new Integer("2"), result.get(1));
assertEquals(new Integer("3"), result.get(2)); assertEquals(new Integer("3"), result.get(2));
@ -298,7 +297,7 @@ public class DefaultConversionTests {
public void testSpr7766() throws Exception { public void testSpr7766() throws Exception {
ConverterRegistry registry = ((ConverterRegistry) conversionService); ConverterRegistry registry = ((ConverterRegistry) conversionService);
registry.addConverter(new ColorConverter()); 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(2, colors.size());
assertEquals(Color.WHITE, colors.get(0)); assertEquals(Color.WHITE, colors.get(0));
assertEquals(Color.BLACK, colors.get(1)); assertEquals(Color.BLACK, colors.get(1));
@ -531,8 +530,7 @@ public class DefaultConversionTests {
foo.add("1"); foo.add("1");
foo.add("2"); foo.add("2");
foo.add("3"); foo.add("3");
List<Integer> bar = (List<Integer>) conversionService.convert(foo, TypeDescriptor.forObject(foo), List<Integer> bar = (List<Integer>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericList")));
new TypeDescriptor(getClass().getField("genericList")));
assertEquals(new Integer(1), bar.get(0)); assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1)); assertEquals(new Integer(2), bar.get(1));
assertEquals(new Integer(3), bar.get(2)); assertEquals(new Integer(3), bar.get(2));
@ -540,8 +538,7 @@ public class DefaultConversionTests {
@Test @Test
public void convertCollectionToCollectionNull() throws Exception { public void convertCollectionToCollectionNull() throws Exception {
List<Integer> bar = (List<Integer>) conversionService.convert(null, List<Integer> bar = (List<Integer>) conversionService.convert(null, new TypeDescriptor(getClass().getField("genericList")));
TypeDescriptor.valueOf(LinkedHashSet.class), new TypeDescriptor(getClass().getField("genericList")));
assertNull(bar); assertNull(bar);
} }
@ -551,8 +548,7 @@ public class DefaultConversionTests {
foo.add("1"); foo.add("1");
foo.add("2"); foo.add("2");
foo.add("3"); foo.add("3");
List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(LinkedHashSet.class), TypeDescriptor List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(List.class));
.valueOf(List.class));
assertEquals("1", bar.get(0)); assertEquals("1", bar.get(0));
assertEquals("2", bar.get(1)); assertEquals("2", bar.get(1));
assertEquals("3", bar.get(2)); assertEquals("3", bar.get(2));
@ -565,8 +561,7 @@ public class DefaultConversionTests {
map.put("2", "2"); map.put("2", "2");
map.put("3", "3"); map.put("3", "3");
Collection values = map.values(); Collection values = map.values();
List<Integer> bar = (List<Integer>) conversionService.convert(values, List<Integer> bar = (List<Integer>) conversionService.convert(values, new TypeDescriptor(getClass().getField("genericList")));
TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList")));
assertEquals(3, bar.size()); assertEquals(3, bar.size());
assertEquals(new Integer(1), bar.get(0)); assertEquals(new Integer(1), bar.get(0));
assertEquals(new Integer(2), bar.get(1)); assertEquals(new Integer(2), bar.get(1));
@ -580,8 +575,7 @@ public class DefaultConversionTests {
Map<String, String> foo = new HashMap<String, String>(); Map<String, String> foo = new HashMap<String, String>();
foo.put("1", "BAR"); foo.put("1", "BAR");
foo.put("2", "BAZ"); foo.put("2", "BAZ");
Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, Map<String, FooEnum> map = (Map<String, FooEnum>) conversionService.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, map.get(1)); assertEquals(FooEnum.BAR, map.get(1));
assertEquals(FooEnum.BAZ, map.get(2)); assertEquals(FooEnum.BAZ, map.get(2));
} }

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

@ -109,12 +109,13 @@ public class GenericConversionServiceTests {
} }
public void convertNullTargetClass() { public void convertNullTargetClass() {
assertNull(conversionService.convert("3", null)); assertNull(conversionService.convert("3", (Class) null));
assertNull(conversionService.convert("3", TypeDescriptor.NULL));
} }
@Test @Test
public void convertNullTypeDescriptor() { public void convertNullTypeDescriptor() {
assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL)); assertNull(conversionService.convert("3", TypeDescriptor.NULL));
} }
@Test @Test
@ -147,10 +148,8 @@ public class GenericConversionServiceTests {
assertTrue(conversionService.canConvert(String.class, boolean.class)); assertTrue(conversionService.canConvert(String.class, boolean.class));
Boolean b = conversionService.convert("true", boolean.class); Boolean b = conversionService.convert("true", boolean.class);
assertEquals(Boolean.TRUE, b); assertEquals(Boolean.TRUE, b);
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
.valueOf(boolean.class))); b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(boolean.class));
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor
.valueOf(boolean.class));
assertEquals(Boolean.TRUE, b); assertEquals(Boolean.TRUE, b);
} }
@ -260,8 +259,7 @@ public class GenericConversionServiceTests {
GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService(); GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
Map<String, String> input = new LinkedHashMap<String, String>(); Map<String, String> input = new LinkedHashMap<String, String>();
input.put("key", "value"); input.put("key", "value");
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), Object converted = conversionService.convert(input, new TypeDescriptor(getClass().getField("wildcardMap")));
new TypeDescriptor(getClass().getField("wildcardMap")));
assertEquals(input, converted); assertEquals(input, converted);
} }
@ -344,7 +342,7 @@ public class GenericConversionServiceTests {
source.add("3"); source.add("3");
TypeDescriptor td = new TypeDescriptor(getClass().getField("list")); TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {
conversionService.convert(source, TypeDescriptor.forObject(source), td); conversionService.convert(source, td);
} }
watch.stop(); watch.stop();
watch.start("convert 4,000,000 manually"); watch.start("convert 4,000,000 manually");
@ -371,7 +369,7 @@ public class GenericConversionServiceTests {
source.put("3", "3"); source.put("3", "3");
TypeDescriptor td = new TypeDescriptor(getClass().getField("map")); TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {
conversionService.convert(source, TypeDescriptor.forObject(source), td); conversionService.convert(source, td);
} }
watch.stop(); watch.stop();
watch.start("convert 4,000,000 manually"); watch.start("convert 4,000,000 manually");

8
org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionTestsUsingCoreConversionService.java

@ -16,14 +16,16 @@
package org.springframework.expression.spel; 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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static junit.framework.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
@ -141,7 +143,7 @@ public class ExpressionTestsUsingCoreConversionService extends ExpressionTestCas
private final ConversionService service = ConversionServiceFactory.createDefaultConversionService(); private final ConversionService service = ConversionServiceFactory.createDefaultConversionService();
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException { 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) { public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {

Loading…
Cancel
Save