提交 ed611be8 编写于 作者: K Keith Donald

conversion utils helper for common code

上级 871fc14f
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import org.springframework.core.convert.ConverterNotFoundException;
......@@ -35,12 +37,14 @@ final class ArrayToArrayGenericConverter implements GenericConverter {
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
Object target = Array.newInstance(targetElementType.getType(), Array.getLength(source));
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetElementType);
}
for (int i = 0; i < Array.getLength(target); i++) {
Array.set(target, i, converter.convert(Array.get(source, i), sourceElementType, targetElementType));
Object sourceElement = Array.get(source, i);
Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
Array.set(target, i, targetElement);
}
return target;
}
......
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.util.Collection;
......@@ -29,7 +31,7 @@ final class ArrayToCollectionGenericConverter implements GenericConverter {
public ArrayToCollectionGenericConverter(GenericConversionService conversionService) {
this.conversionService = conversionService;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
int length = Array.getLength(source);
Collection collection = CollectionFactory.createCollection(targetType.getType(), length);
......@@ -43,9 +45,11 @@ final class ArrayToCollectionGenericConverter implements GenericConverter {
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetElementType);
}
}
for (int i = 0; i < length; i++) {
collection.add(converter.convert(Array.get(source, i), sourceElementType, targetElementType));
Object sourceElement = Array.get(source, i);
Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
collection.add(targetElement);
}
}
return collection;
......
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import org.springframework.core.convert.ConverterNotFoundException;
......@@ -27,7 +29,7 @@ final class ArrayToObjectGenericConverter implements GenericConverter {
public ArrayToObjectGenericConverter(GenericConversionService conversionService) {
this.conversionService = conversionService;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
int length = Array.getLength(source);
if (length == 0) {
......@@ -41,7 +43,7 @@ final class ArrayToObjectGenericConverter implements GenericConverter {
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetType);
}
return converter.convert(Array.get(source, 0), sourceElementType, targetType);
return invokeConverter(converter, Array.get(source, 0), sourceElementType, targetType);
}
}
}
......
......@@ -15,6 +15,9 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
......@@ -29,7 +32,7 @@ final class CollectionToArrayGenericConverter implements GenericConverter {
public CollectionToArrayGenericConverter(GenericConversionService conversionService) {
this.conversionService = conversionService;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Collection sourceCollection = (Collection) source;
TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
......@@ -38,7 +41,7 @@ final class CollectionToArrayGenericConverter implements GenericConverter {
}
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size());
int i = 0;
int i = 0;
if (sourceElementType == TypeDescriptor.NULL || sourceElementType.isAssignableTo(targetElementType)) {
for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) {
Array.set(array, i, it.next());
......@@ -47,21 +50,14 @@ final class CollectionToArrayGenericConverter implements GenericConverter {
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetElementType);
}
}
for (Iterator it = sourceCollection.iterator(); it.hasNext(); i++) {
Array.set(array, i, converter.convert(it.next(), sourceElementType, targetElementType));
Object sourceElement = it.next();
Object targetElement = invokeConverter(converter, sourceElement, sourceElementType, targetElementType);
Array.set(array, i, targetElement);
}
}
return array;
}
private TypeDescriptor getElementType(Collection collection) {
for (Object element : collection) {
if (element != null) {
return TypeDescriptor.valueOf(element.getClass());
}
}
return TypeDescriptor.NULL;
}
}
......@@ -15,6 +15,9 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.getElementType;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Collection;
import org.springframework.core.CollectionFactory;
......@@ -40,30 +43,20 @@ final class CollectionToCollectionGenericConverter implements GenericConverter {
if (sourceType.isAssignableTo(targetType)) {
return sourceCollection;
} else {
Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection
.size());
targetCollection.addAll(sourceCollection);
return targetCollection;
Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
target.addAll(sourceCollection);
return target;
}
}
Collection targetCollection = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetElementType);
}
for (Object element : sourceCollection) {
targetCollection.add(converter.convert(element, sourceElementType, targetElementType));
}
return targetCollection;
}
private TypeDescriptor getElementType(Collection collection) {
for (Object element : collection) {
if (element != null) {
return TypeDescriptor.valueOf(element.getClass());
}
target.add(invokeConverter(converter, element, sourceElementType, targetElementType));
}
return TypeDescriptor.NULL;
return target;
}
}
\ No newline at end of file
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Collection;
import org.springframework.core.convert.ConverterNotFoundException;
......@@ -45,7 +47,7 @@ final class CollectionToObjectGenericConverter implements GenericConverter {
if (converter == null) {
throw new ConverterNotFoundException(sourceElementType, targetType);
}
return converter.convert(firstElement, sourceElementType, targetType);
return invokeConverter(converter, firstElement, sourceElementType, targetType);
}
}
}
......
package org.springframework.core.convert.support;
import java.util.Collection;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
final class ConversionUtils {
public static Object invokeConverter(GenericConverter converter, Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
try {
return converter.convert(source, sourceType, targetType);
}
catch (Exception ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
}
public static TypeDescriptor getElementType(Collection collection) {
for (Object element : collection) {
if (element != null) {
return TypeDescriptor.valueOf(element.getClass());
}
}
return TypeDescriptor.NULL;
}
}
......@@ -16,6 +16,8 @@
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
......@@ -30,7 +32,6 @@ import java.util.Map;
import java.util.Set;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
......@@ -58,7 +59,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
};
private final Map<Class, Map<Class, GenericConverter>> sourceTypeConverters = new HashMap<Class, Map<Class, GenericConverter>>();
private ConversionService parent;
......@@ -69,12 +69,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
};
public GenericConversionService() {
initGenericConverters();
}
/**
* Registers the converters in the set provided.
* JavaBean-friendly alternative to calling {@link #addConverter(Converter)}.
......@@ -111,7 +109,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
return this.parent;
}
// implementing ConverterRegistry
public void addConverter(Converter converter) {
......@@ -140,7 +137,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
getSourceMap(sourceType).remove(targetType);
}
// implementing ConversionService
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
......@@ -177,15 +173,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (converter == null) {
throw new ConverterNotFoundException(sourceType, targetType);
}
try {
return converter.convert(source, sourceType, targetType);
}
catch (ConversionFailedException ex) {
throw ex;
}
catch (Exception ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
return invokeConverter(converter, source, sourceType, targetType);
}
// subclassing hooks
......@@ -203,6 +191,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
addGenericConverter(Map.class, Map.class, new MapToMapGenericConverter(this));
addGenericConverter(Object.class, Object[].class, new ObjectToArrayGenericConverter(this));
addGenericConverter(Object.class, Collection.class, new ObjectToCollectionGenericConverter(this));
addGenericConverter(String.class, Object[].class, new StringToArrayGenericConverter(this));
}
/**
......@@ -242,21 +231,17 @@ public class GenericConversionService implements ConversionService, ConverterReg
GenericConverter converter = findConverterByClassPair(sourceType.getObjectType(), targetType.getObjectType());
if (converter != null) {
return converter;
}
else if (this.parent != null && this.parent.canConvert(sourceType, targetType)) {
} else if (this.parent != null && this.parent.canConvert(sourceType, targetType)) {
return this.parentConverterAdapter;
}
else {
} else {
if (sourceType.isAssignableTo(targetType)) {
return NO_OP_CONVERTER;
}
else {
} else {
return null;
}
}
}
// internal helpers
private List<Class> getRequiredTypeInfo(Object converter) {
......@@ -298,12 +283,10 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (typeInfo.size() == 2) {
return typeInfo;
}
}
else if (Converter.class.isAssignableFrom((Class) rawType)) {
} else if (Converter.class.isAssignableFrom((Class) rawType)) {
return getConverterTypeInfo((Class) rawType);
}
}
else if (Converter.class.isAssignableFrom((Class) ifc)) {
} else if (Converter.class.isAssignableFrom((Class) ifc)) {
return getConverterTypeInfo((Class) ifc);
}
}
......@@ -330,8 +313,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
Map<Class, GenericConverter> objectConverters = getConvertersForSource(Object.class);
return getConverter(objectConverters, targetType);
}
else {
} else {
LinkedList<Class> classQueue = new LinkedList<Class>();
classQueue.addFirst(sourceType);
while (!classQueue.isEmpty()) {
......@@ -393,8 +375,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
return converters.get(Object.class);
}
else {
} else {
LinkedList<Class> classQueue = new LinkedList<Class>();
classQueue.addFirst(targetType);
while (!classQueue.isEmpty()) {
......@@ -408,8 +389,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (componentType.getSuperclass() != null) {
classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
}
}
else {
} else {
if (currentClass.getSuperclass() != null) {
classQueue.addFirst(currentClass.getSuperclass());
}
......@@ -423,7 +403,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
private static class ConverterAdapter implements GenericConverter {
private Converter converter;
......@@ -438,7 +417,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
private static class ConverterFactoryAdapter implements GenericConverter {
private ConverterFactory converterFactory;
......
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Map;
import org.springframework.core.CollectionFactory;
......@@ -114,7 +116,7 @@ final class MapToMapGenericConverter implements GenericConverter {
public Object convertKey(Object sourceKey) {
if (sourceKey != null && this.keyConverter != null) {
return this.keyConverter.convert(sourceKey, this.sourceKeyType, this.targetKeyType);
return invokeConverter(this.keyConverter, sourceKey, this.sourceKeyType, this.targetKeyType);
} else {
return sourceKey;
}
......@@ -122,7 +124,7 @@ final class MapToMapGenericConverter implements GenericConverter {
public Object convertValue(Object sourceValue) {
if (sourceValue != null && this.valueConverter != null) {
return this.valueConverter.convert(sourceValue, this.sourceValueType, this.targetValueType);
return invokeConverter(this.valueConverter, sourceValue, this.sourceValueType, this.targetValueType);
} else {
return sourceValue;
}
......
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.lang.reflect.Array;
import org.springframework.core.convert.ConverterNotFoundException;
......@@ -27,18 +29,18 @@ final class ObjectToArrayGenericConverter implements GenericConverter {
public ObjectToArrayGenericConverter(GenericConversionService conversionService) {
this.conversionService = conversionService;
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
Object target = Array.newInstance(targetElementType.getType(), 1);
if (sourceType.isAssignableTo(targetElementType)) {
Array.set(target, 0, source);
Array.set(target, 0, source);
} else {
GenericConverter converter = this.conversionService.getConverter(sourceType, targetElementType);
if (converter == null) {
throw new ConverterNotFoundException(sourceType, targetElementType);
}
Array.set(target, 0, converter.convert(source, sourceType, targetElementType));
}
Array.set(target, 0, invokeConverter(converter, source, sourceType, targetElementType));
}
return target;
}
......
......@@ -15,6 +15,8 @@
*/
package org.springframework.core.convert.support;
import static org.springframework.core.convert.support.ConversionUtils.invokeConverter;
import java.util.Collection;
import org.springframework.core.CollectionFactory;
......@@ -39,7 +41,7 @@ final class ObjectToCollectionGenericConverter implements GenericConverter {
if (converter == null) {
throw new ConverterNotFoundException(sourceType, targetElementType);
}
target.add(converter.convert(source, sourceType, targetElementType));
target.add(invokeConverter(converter, source, sourceType, targetElementType));
}
return target;
}
......
......@@ -96,7 +96,7 @@ public class GenericConversionServiceTests {
}
@Test
public void convertNullConversionPointType() {
public void convertNullTypeDescriptor() {
assertEquals(null, converter.convert(3, TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
}
......@@ -304,7 +304,6 @@ public class GenericConversionServiceTests {
}
@Test
@Ignore
public void convertStringToArrayWithElementConversion() {
converter.addConverterFactory(new StringToNumberConverterFactory());
Integer[] result = converter.convert("1,2,3", Integer[].class);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册