diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java index 58cd87a4890b1e57cef3e3d7329f6515c82f578b..66c145217a13a1f435f0adc41c7a03a31ebbccca 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToArrayGenericConverter.java @@ -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; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java index e3c1f72b664843381caa7d4ad62f3152d6f6cdaf..e201c3f385e51d0b7e35b3d47eb484753002317c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToCollectionGenericConverter.java @@ -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; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java index af24d61307aa5303347606073bcab7647509f7b3..c1f31c87c4fe958c011afec809ec45743aa31446 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ArrayToObjectGenericConverter.java @@ -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); } } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java index b2023f50349b1268e67800d12ba6162fb47b2f55..4c6e4ea6fc020199e91bec748d37deffc430da4c 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToArrayGenericConverter.java @@ -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; - } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java index 50e77d2a4979e71541956fef067061766020ebf5..9cc741a6aafbb393ba5cf3076b9ca579bf9d45f4 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToCollectionGenericConverter.java @@ -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 diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java index 58b2a40ea8e63d1c979d1f42cfb0d6f1ab460dae..c869933b26bb7b55918949f6e1dc97a8685e9765 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/CollectionToObjectGenericConverter.java @@ -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); } } } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..33f39f341ed78197d34cf99d67c9d5fd5a76126e --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionUtils.java @@ -0,0 +1,27 @@ +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; + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 28bd02d24e6644701705cf67de0136a0495e3cc2..e54da0edd728d4f4a6c98e899a499055bf63784e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -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> sourceTypeConverters = new HashMap>(); 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 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 objectConverters = getConvertersForSource(Object.class); return getConverter(objectConverters, targetType); - } - else { + } else { LinkedList classQueue = new LinkedList(); classQueue.addFirst(sourceType); while (!classQueue.isEmpty()) { @@ -393,8 +375,7 @@ public class GenericConversionService implements ConversionService, ConverterReg } } return converters.get(Object.class); - } - else { + } else { LinkedList classQueue = new LinkedList(); 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; diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapGenericConverter.java index 05c73358e25a48fc58ae1c6d8882e48908401ea3..93c2621901c4a46ae8e74a19f631d1bd009c0b2f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/MapToMapGenericConverter.java @@ -1,5 +1,7 @@ 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; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java index c691930dd57d79e54b94702382960a7f49b2fd8a..f442646d7b1dcff551c164b44904ae7c03bebb43 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToArrayGenericConverter.java @@ -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; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java index 1cd19d77fbea41c41c271f53b7e3c789b45ad81a..f64ffd2419d31b745bbfae83e5924330b0d7ebf8 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionGenericConverter.java @@ -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; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 1293759454ad85eb549fc9b875826b17c59e6564..7bc980037105176d4d5bbb314d96f97a83761673 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -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);