initial JSR-303 Bean Validation support; revised ConversionService and FormatterRegistry

This commit is contained in:
Juergen Hoeller
2009-09-07 23:58:42 +00:00
parent f9f9b431a6
commit a86a698e5b
72 changed files with 1808 additions and 848 deletions

View File

@@ -37,7 +37,10 @@ import org.springframework.util.ClassUtils;
*/
public class TypeDescriptor {
public final static TypeDescriptor NULL = new TypeDescriptor((Class<?>) null);
/**
* Constant defining an 'empty' TypeDescriptor.
*/
public static final TypeDescriptor NULL = new TypeDescriptor();
private Class<?> type;
@@ -50,17 +53,20 @@ public class TypeDescriptor {
/**
* Creates a new descriptor for the given type.
* Use this constructor when a conversion point comes from a source such as a Map or collection, where no additional context is available.
* @param type the actual type
* Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a source such as a Map
* or Collection, where no additional context is available.
* @param type the actual type to wrap
*/
public TypeDescriptor(Class<?> type) {
Assert.notNull(type, "Type must not be null");
this.type = type;
}
/**
* Create a new type descriptor from a method or constructor parameter.
* Use this constructor when a target conversion point originates from a method parameter, such as a setter method argument.
* <p>Use this constructor when a target conversion point originates from a method parameter,
* such as a setter method argument.
* @param methodParameter the MethodParameter to wrap
*/
public TypeDescriptor(MethodParameter methodParameter) {
@@ -78,6 +84,12 @@ public class TypeDescriptor {
this.field = field;
}
/**
* Internal constructor for a NULL descriptor.
*/
private TypeDescriptor() {
}
/**
* Return the wrapped MethodParameter, if any.
@@ -212,7 +224,7 @@ public class TypeDescriptor {
public Annotation[] getAnnotations() {
if (this.field != null) {
if (this.cachedFieldAnnotations == null) {
this.cachedFieldAnnotations = field.getAnnotations();
this.cachedFieldAnnotations = this.field.getAnnotations();
}
return this.cachedFieldAnnotations;
}
@@ -317,7 +329,7 @@ public class TypeDescriptor {
*/
public static TypeDescriptor valueOf(Class type) {
// TODO needs a cache for common type descriptors
return new TypeDescriptor(type);
return (type != null ? new TypeDescriptor(type) : NULL);
}
/**

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.converter;
/**
@@ -25,7 +26,7 @@ package org.springframework.core.convert.converter;
* for example {@link Number} for a set of number subtypes.
*/
public interface ConverterFactory<S, R> {
/**
* Get the converter to convert from S to target type T, where T is also an instance of R.
* @param <T> the target type

View File

@@ -24,14 +24,14 @@ package org.springframework.core.convert.converter;
public interface ConverterRegistry {
/**
* Add a converter to this registry.
* Add a plain converter to this registry.
*/
void add(Converter<?, ?> converter);
void addConverter(Converter<?, ?> converter);
/**
* Add a converter factory to this registry.
* Add a factory-created converter to this registry.
*/
void add(ConverterFactory<?, ?> converterFactory);
void addConverter(ConverterFactory<?, ?> converterFactory);
/**
* Remove the conversion logic from the sourceType to the targetType.

View File

@@ -38,7 +38,7 @@ import org.springframework.util.NumberUtils;
* @see java.math.BigDecimal
* @see NumberUtils
*/
public class CharacterToNumberFactory implements ConverterFactory<Character, Number> {
class CharacterToNumberFactory implements ConverterFactory<Character, Number> {
public <T extends Number> Converter<Character, T> getConverter(Class<T> targetType) {
return new CharacterToNumber<T>(targetType);

View File

@@ -21,6 +21,7 @@ package org.springframework.core.convert.support;
* converters for a number of standard Java types like Class, Number, Boolean and so on.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
*/
public class DefaultConversionService extends GenericConversionService {
@@ -29,22 +30,22 @@ public class DefaultConversionService extends GenericConversionService {
* Create a new default conversion service, installing the default converters.
*/
public DefaultConversionService() {
add(new StringToByte());
add(new StringToBoolean());
add(new StringToCharacter());
add(new StringToShort());
add(new StringToInteger());
add(new StringToLong());
add(new StringToFloat());
add(new StringToDouble());
add(new StringToBigInteger());
add(new StringToBigDecimal());
add(new StringToLocale());
add(new NumberToCharacter());
add(new ObjectToString());
add(new StringToEnumFactory());
add(new NumberToNumberFactory());
add(new CharacterToNumberFactory());
addConverter(new StringToByte());
addConverter(new StringToBoolean());
addConverter(new StringToCharacter());
addConverter(new StringToShort());
addConverter(new StringToInteger());
addConverter(new StringToLong());
addConverter(new StringToFloat());
addConverter(new StringToDouble());
addConverter(new StringToBigInteger());
addConverter(new StringToBigDecimal());
addConverter(new StringToLocale());
addConverter(new NumberToCharacter());
addConverter(new ObjectToString());
addConverter(new StringToEnumFactory());
addConverter(new NumberToNumberFactory());
addConverter(new CharacterToNumberFactory());
}
}

View File

@@ -45,8 +45,8 @@ import org.springframework.util.ClassUtils;
* @author Keith Donald
* @author Juergen Hoeller
* @since 3.0
* @see #add(Converter)
* @see #add(ConverterFactory)
* @see #addConverter(Converter)
* @see #addConverter(ConverterFactory)
*/
public class GenericConversionService implements ConversionService, ConverterRegistry {
@@ -61,23 +61,23 @@ public class GenericConversionService implements ConversionService, ConverterReg
/**
* Registers the converters in the set provided.
* JavaBean-friendly alternative to calling {@link #add(Converter)}.
* @see #add(Converter)
* JavaBean-friendly alternative to calling {@link #addConverter(Converter)}.
* @see #addConverter(Converter)
*/
public void setConverters(Set<Converter> converters) {
for (Converter converter : converters) {
add(converter);
addConverter(converter);
}
}
/**
* Registers the converters in the set provided.
* JavaBean-friendly alternative to calling {@link #add(ConverterFactory)}.
* @see #add(ConverterFactory)
* JavaBean-friendly alternative to calling {@link #addConverter(ConverterFactory)}.
* @see #addConverter(ConverterFactory)
*/
public void setConverterFactories(Set<ConverterFactory> converters) {
for (ConverterFactory converterFactory : converters) {
add(converterFactory);
addConverter(converterFactory);
}
}
@@ -98,7 +98,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
// implementing ConverterRegistry
public void add(Converter converter) {
public void addConverter(Converter converter) {
List<Class> typeInfo = getRequiredTypeInfo(converter);
if (typeInfo == null) {
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your Converter<S, T> converts between");
@@ -108,7 +108,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
getSourceMap(sourceType).put(targetType, converter);
}
public void add(ConverterFactory<?, ?> converterFactory) {
public void addConverter(ConverterFactory<?, ?> converterFactory) {
List<Class> typeInfo = getRequiredTypeInfo(converterFactory);
if (typeInfo == null) {
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetType <T> your ConverterFactory<S, T> creates Converters to convert between");
@@ -153,12 +153,12 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
else {
throw new ConverterNotFoundException(source.getClass(), targetType.getType(),
"No converter found that can convert from sourceType [" + source.getClass().getName()
+ "] to targetType [" + targetType.getName() + "]");
"No converter found that can convert from source type [" + source.getClass().getName() +
"] to target type [" + targetType.getName() + "]");
}
}
ConversionExecutor getConversionExecutor(Class<?> sourceClass, TypeDescriptor targetType)
ConversionExecutor getConversionExecutor(final Class<?> sourceClass, final TypeDescriptor targetType)
throws ConverterNotFoundException {
Assert.notNull(sourceClass, "The sourceType to convert from is required");
@@ -175,8 +175,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
else if (targetType.isCollection()) {
if (targetType.isAbstractClass()) {
throw new IllegalArgumentException("Conversion target class [" + targetType.getName()
+ "] is invalid; cannot convert to abstract collection types--"
+ "request an interface or concrete implementation instead");
+ "] is invalid; cannot convert to abstract collection types. "
+ "Request an interface or a concrete implementation instead!");
}
return new ArrayToCollection(sourceType, targetType, this);
}
@@ -190,9 +190,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
else {
if (targetType.getType().equals(String.class)) {
// array to string
return null;
if (sourceType.getElementType().equals(String.class)) {
return new StringArrayToObject(targetType, this);
}
else {
// array to object
@@ -210,14 +209,15 @@ public class GenericConversionService implements ConversionService, ConverterReg
else if (targetType.isMap()) {
if (sourceType.getElementType().equals(String.class)) {
return new StringCollectionToMap(sourceType, targetType, this);
} else {
}
else {
// object collection to map
return null;
}
}
else {
if (targetType.getType().equals(String.class)) {
// collection to string;
// collection to string
return null;
}
else {
@@ -255,7 +255,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
if (targetType.isArray()) {
if (sourceType.getType().equals(String.class)) {
return new StringToArray(sourceType, targetType, this);
return new StringToArray(targetType, this);
}
else {
return new ObjectToArray(sourceType, targetType, this);
@@ -281,10 +281,22 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (sourceType.isAssignableTo(targetType)) {
return NoOpConversionExecutor.INSTANCE;
}
Converter converter = findRegisteredConverter(ClassUtils.resolvePrimitiveIfNecessary(sourceType.getType()), ClassUtils.resolvePrimitiveIfNecessary(targetType.getType()));
Converter converter = findRegisteredConverter(
ClassUtils.resolvePrimitiveIfNecessary(sourceClass),
ClassUtils.resolvePrimitiveIfNecessary(targetType.getType()));
if (converter != null) {
return new StaticConversionExecutor(sourceType, targetType, converter);
}
else if (this.parent instanceof GenericConversionService) {
return ((GenericConversionService) this.parent).getConversionExecutor(sourceClass, targetType);
}
else if (this.parent != null && this.parent.canConvert(sourceClass, targetType)){
return new ConversionExecutor() {
public Object execute(Object source) {
return parent.convert(source, targetType);
}
};
}
else {
return null;
}
@@ -356,7 +368,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
return sourceMap;
}
private Converter findRegisteredConverter(Class<?> sourceType, Class<?> targetType) {
protected <T> Converter findRegisteredConverter(Class<?> sourceType, Class<T> targetType) {
if (sourceType.isInterface()) {
LinkedList<Class> classQueue = new LinkedList<Class>();
classQueue.addFirst(sourceType);

View File

@@ -34,7 +34,7 @@ import org.springframework.util.NumberUtils;
* @see java.math.BigDecimal
* @see NumberUtils
*/
public class NumberToCharacter implements Converter<Number, Character> {
class NumberToCharacter implements Converter<Number, Character> {
public Character convert(Number source) {
return (char) source.shortValue();

View File

@@ -38,7 +38,7 @@ import org.springframework.util.NumberUtils;
* @see java.math.BigDecimal
* @see NumberUtils
*/
public class NumberToNumberFactory implements ConverterFactory<Number, Number> {
class NumberToNumberFactory implements ConverterFactory<Number, Number> {
public <T extends Number> Converter<Number, T> getConverter(Class<T> targetType) {
return new NumberToNumber<T>(targetType);

View File

@@ -30,7 +30,7 @@ import org.springframework.core.convert.TypeDescriptor;
*/
class ObjectToArray implements ConversionExecutor {
private final TypeDescriptor targetArrayType;
private final Class elementType;
private final ConversionExecutor elementConverter;
@@ -38,15 +38,15 @@ class ObjectToArray implements ConversionExecutor {
public ObjectToArray(TypeDescriptor sourceObjectType, TypeDescriptor targetArrayType,
GenericConversionService conversionService) {
this.targetArrayType = targetArrayType;
this.elementType = targetArrayType.getElementType();
this.elementConverter = conversionService.getConversionExecutor(
sourceObjectType.getType(), TypeDescriptor.valueOf(targetArrayType.getElementType()));
sourceObjectType.getType(), TypeDescriptor.valueOf(this.elementType));
}
public Object execute(Object source) throws ConversionFailedException {
Object array = Array.newInstance(targetArrayType.getElementType(), 1);
Object element = elementConverter.execute(source);
Object array = Array.newInstance(this.elementType, 1);
Object element = this.elementConverter.execute(source);
Array.set(array, 0, element);
return array;
}

View File

@@ -26,7 +26,7 @@ import org.springframework.core.convert.converter.Converter;
* @author Keith Donald
* @since 3.0
*/
public class ObjectToString implements Converter<Object, String> {
class ObjectToString implements Converter<Object, String> {
public String convert(Object source) {
return source.toString();

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Converts a String array to a single element.
*
* @author Juergen Hoeller
* @since 3.0
*/
class StringArrayToObject implements ConversionExecutor {
private final ConversionExecutor elementConverter;
public StringArrayToObject(TypeDescriptor targetType, GenericConversionService conversionService) {
this.elementConverter = conversionService.getConversionExecutor(String.class, targetType);
}
public Object execute(Object source) throws ConversionFailedException {
String str = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(source));
return this.elementConverter.execute(str);
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.core.convert.support;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.StringUtils;
/**
* Converts a comma-delimited string to an array.
@@ -27,15 +28,18 @@ import org.springframework.core.convert.TypeDescriptor;
@SuppressWarnings("unchecked")
class StringToArray implements ConversionExecutor {
private ArrayToArray converter;
private final ArrayToArray converter;
public StringToArray(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) {
converter = new ArrayToArray(TypeDescriptor.valueOf(String[].class), targetType, conversionService);
public StringToArray(TypeDescriptor targetType, GenericConversionService conversionService) {
this.converter = new ArrayToArray(TypeDescriptor.valueOf(String[].class), targetType, conversionService);
}
public Object execute(Object source) {
String string = (String) source;
String[] fields = string.split(",");
return converter.execute(fields);
String str = (String) source;
String[] fields = StringUtils.commaDelimitedListToStringArray(str);
return this.converter.execute(fields);
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import java.math.BigDecimal;
@@ -26,10 +27,10 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToBigDecimal implements Converter<String, BigDecimal> {
class StringToBigDecimal implements Converter<String, BigDecimal> {
public BigDecimal convert(String source) {
return NumberUtils.parseNumber(source, BigDecimal.class);
}
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import java.math.BigInteger;
@@ -26,10 +27,10 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToBigInteger implements Converter<String, BigInteger> {
class StringToBigInteger implements Converter<String, BigInteger> {
public BigInteger convert(String source) {
return NumberUtils.parseNumber(source, BigInteger.class);
}
}
}

View File

@@ -13,17 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import org.springframework.core.convert.converter.Converter;
/**
* Converts String to a Boolean..
* Converts String to a Boolean.
*
* @author Keith Donald
* @since 3.0
*/
public class StringToBoolean implements Converter<String, Boolean> {
class StringToBoolean implements Converter<String, Boolean> {
public Boolean convert(String source) {
if (source.equals("true")) {

View File

@@ -25,10 +25,10 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToByte implements Converter<String, Byte> {
class StringToByte implements Converter<String, Byte> {
public Byte convert(String source) {
return NumberUtils.parseNumber(source, Byte.class);
}
}
}

View File

@@ -24,7 +24,7 @@ import org.springframework.core.convert.converter.Converter;
* @author Keith Donald
* @since 3.0
*/
public class StringToCharacter implements Converter<String, Character> {
class StringToCharacter implements Converter<String, Character> {
public Character convert(String source) {
if (source.length() != 1) {

View File

@@ -21,22 +21,25 @@ import org.springframework.core.convert.TypeDescriptor;
/**
* Converts a comma-delimited string to a collection.
*
* @author Keith Donald
* @since 3.0
*/
@SuppressWarnings("unchecked")
class StringToCollection implements ConversionExecutor {
private ArrayToCollection converter;
private final ArrayToCollection converter;
public StringToCollection(TypeDescriptor sourceType, TypeDescriptor targetType, GenericConversionService conversionService) {
converter = new ArrayToCollection(sourceType, targetType, conversionService);
this.converter = new ArrayToCollection(sourceType, targetType, conversionService);
}
public Object execute(Object source) throws ConversionFailedException {
String string = (String) source;
String[] fields = string.split(",");
return converter.execute(fields);
return this.converter.execute(fields);
}
}

View File

@@ -25,7 +25,7 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToDouble implements Converter<String, Double> {
class StringToDouble implements Converter<String, Double> {
public Double convert(String source) {
return NumberUtils.parseNumber(source, Double.class);

View File

@@ -27,7 +27,7 @@ import org.springframework.core.convert.converter.ConverterInfo;
* @since 3.0
*/
@SuppressWarnings("unchecked")
public class StringToEnumFactory implements ConverterFactory<String, Enum> {
class StringToEnumFactory implements ConverterFactory<String, Enum> {
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnum(targetType);

View File

@@ -25,7 +25,7 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToFloat implements Converter<String, Float> {
class StringToFloat implements Converter<String, Float> {
public Float convert(String source) {
return NumberUtils.parseNumber(source, Float.class);

View File

@@ -25,7 +25,7 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToInteger implements Converter<String, Integer> {
class StringToInteger implements Converter<String, Integer> {
public Integer convert(String source) {
return NumberUtils.parseNumber(source, Integer.class);

View File

@@ -27,7 +27,7 @@ import org.springframework.util.StringUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToLocale implements Converter<String, Locale> {
class StringToLocale implements Converter<String, Locale> {
public Locale convert(String source) {
return StringUtils.parseLocaleString(source);

View File

@@ -25,7 +25,7 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToLong implements Converter<String, Long> {
class StringToLong implements Converter<String, Long> {
public Long convert(String source) {
return NumberUtils.parseNumber(source, Long.class);

View File

@@ -25,7 +25,7 @@ import org.springframework.util.NumberUtils;
* @author Keith Donald
* @since 3.0
*/
public class StringToShort implements Converter<String, Short> {
class StringToShort implements Converter<String, Short> {
public Short convert(String source) {
return NumberUtils.parseNumber(source, Short.class);

View File

@@ -776,7 +776,7 @@ public abstract class ClassUtils {
* @param clazz the class to check
* @return the original class, or a primitive wrapper for the original primitive type
*/
public static Class resolvePrimitiveIfNecessary(Class clazz) {
public static Class<?> resolvePrimitiveIfNecessary(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() ? primitiveTypeToWrapperMap.get(clazz) : clazz);
}

View File

@@ -356,7 +356,7 @@ public abstract class StringUtils {
}
int count = 0;
int pos = 0;
int idx = 0;
int idx;
while ((idx = str.indexOf(sub, pos)) != -1) {
++count;
pos = idx + sub.length();
@@ -997,7 +997,7 @@ public abstract class StringUtils {
}
else {
int pos = 0;
int delPos = 0;
int delPos;
while ((delPos = str.indexOf(delimiter, pos)) != -1) {
result.add(deleteAny(str.substring(pos, delPos), charsToDelete));
pos = delPos + delimiter.length();
@@ -1090,6 +1090,9 @@ public abstract class StringUtils {
if (ObjectUtils.isEmpty(arr)) {
return "";
}
if (arr.length == 1) {
return ObjectUtils.nullSafeToString(arr[0]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (i > 0) {

View File

@@ -42,7 +42,7 @@ public class GenericConversionServiceTests {
@Test
public void executeConversion() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
assertEquals(new Integer(3), converter.convert("3", Integer.class));
}
@@ -68,7 +68,7 @@ public class GenericConversionServiceTests {
@Test
public void addConverterNoSourceTargetClassInfoAvailable() {
try {
converter.add(new Converter() {
converter.addConverter(new Converter() {
public Object convert(Object source) throws Exception {
return source;
}
@@ -97,7 +97,7 @@ public class GenericConversionServiceTests {
@Test
public void convertWrongTypeArgument() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
try {
converter.convert("BOGUS", Integer.class);
fail("Should have failed");
@@ -108,7 +108,7 @@ public class GenericConversionServiceTests {
@Test
public void convertSuperSourceType() {
converter.add(new Converter<CharSequence, Integer>() {
converter.addConverter(new Converter<CharSequence, Integer>() {
public Integer convert(CharSequence source) throws Exception {
return Integer.valueOf(source.toString());
}
@@ -119,14 +119,14 @@ public class GenericConversionServiceTests {
@Test
public void convertObjectToPrimitive() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
Integer three = converter.convert("3", int.class);
assertEquals(3, three.intValue());
}
@Test
public void convertArrayToArray() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class);
assertEquals(new Integer(1), result[0]);
assertEquals(new Integer(2), result[1]);
@@ -135,7 +135,7 @@ public class GenericConversionServiceTests {
@Test
public void convertArrayToPrimitiveArray() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
int[] result = converter.convert(new String[] { "1", "2", "3" }, int[].class);
assertEquals(1, result[0]);
assertEquals(2, result[1]);
@@ -154,7 +154,7 @@ public class GenericConversionServiceTests {
@Test
public void convertArrayToListGenericTypeConversion() throws Exception {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
List<Integer> result = (List<Integer>) converter.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));
@@ -192,7 +192,7 @@ public class GenericConversionServiceTests {
@Test
public void convertListToArrayWithComponentConversion() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
@@ -210,8 +210,8 @@ public class GenericConversionServiceTests {
Map<String, String> foo = new HashMap<String, String>();
foo.put("1", "BAR");
foo.put("2", "BAZ");
converter.add(new StringToInteger());
converter.add(new StringToEnumFactory().getConverter(FooEnum.class));
converter.addConverter(new StringToInteger());
converter.addConverter(new StringToEnumFactory().getConverter(FooEnum.class));
Map<String, FooEnum> map = (Map<String, FooEnum>) converter.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(map.get(1), FooEnum.BAR);
assertEquals(map.get(2), FooEnum.BAZ);
@@ -228,7 +228,7 @@ public class GenericConversionServiceTests {
@Test
public void convertStringToArrayWithElementConversion() {
converter.add(new StringToInteger());
converter.addConverter(new StringToInteger());
Integer[] result = converter.convert("1,2,3", Integer[].class);
assertEquals(3, result.length);
assertEquals(new Integer(1), result[0]);