Favor convertValue(Object, TypeDescriptor) where possible and TypedValue(Object); check with Andy on Selection and Projection TypedValue usage

This commit is contained in:
Keith Donald
2011-01-07 06:32:21 +00:00
parent 0fb631348e
commit 71e60f4551
12 changed files with 32 additions and 47 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.core.convert.support;
import java.util.Map;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
@@ -43,24 +41,4 @@ abstract class ConversionUtils {
}
}
public static TypeDescriptor[] getMapEntryTypes(Map<?, ?> sourceMap) {
Class<?> keyType = null;
Class<?> valueType = null;
for (Object entry : sourceMap.entrySet()) {
Map.Entry<?, ?> mapEntry = (Map.Entry<?, ?>) entry;
Object key = mapEntry.getKey();
if (keyType == null && key != null) {
keyType = key.getClass();
}
Object value = mapEntry.getValue();
if (valueType == null && value != null) {
valueType = value.getClass();
}
if (keyType!= null && valueType != null) {
break;
}
}
return new TypeDescriptor[] { TypeDescriptor.valueOf(keyType), TypeDescriptor.valueOf(valueType) };
}
}

View File

@@ -457,8 +457,7 @@ public class DefaultConversionTests {
@Test
public void convertStringToCollectionWithElementConversion() throws Exception {
List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(getClass().getField("genericList")));
List result = (List) conversionService.convert("1,2,3", new TypeDescriptor(getClass().getField("genericList")));
assertEquals(3, result.size());
assertEquals(new Integer(1), result.get(0));
assertEquals(new Integer(2), result.get(1));
@@ -494,8 +493,7 @@ public class DefaultConversionTests {
@Test
public void convertObjectToCollectionWithElementConversion() throws Exception {
List<Integer> result = (List<Integer>) conversionService.convert(3L, TypeDescriptor.valueOf(Long.class),
new TypeDescriptor(getClass().getField("genericList")));
List<Integer> result = (List<Integer>) conversionService.convert(3L, new TypeDescriptor(getClass().getField("genericList")));
assertEquals(1, result.size());
assertEquals(new Integer(3), result.get(0));
}