Polish whitespace for conversion service packages

This commit is contained in:
Phillip Webb 2012-10-26 14:09:26 -07:00
parent 61186b0b56
commit 01272fb0e6
27 changed files with 127 additions and 125 deletions

View File

@ -35,7 +35,7 @@ abstract class AbstractDescriptor {
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
this.type = type; this.type = type;
} }
public Class<?> getType() { public Class<?> getType() {
return this.type; return this.type;
@ -48,13 +48,13 @@ abstract class AbstractDescriptor {
} }
else if (isArray()) { else if (isArray()) {
Class<?> elementType = getType().getComponentType(); Class<?> elementType = getType().getComponentType();
return new TypeDescriptor(nested(elementType, 0)); return new TypeDescriptor(nested(elementType, 0));
} }
else { else {
return null; return null;
} }
} }
public TypeDescriptor getMapKeyTypeDescriptor() { public TypeDescriptor getMapKeyTypeDescriptor() {
if (isMap()) { if (isMap()) {
Class<?> keyType = resolveMapKeyType(); Class<?> keyType = resolveMapKeyType();
@ -64,7 +64,7 @@ abstract class AbstractDescriptor {
return null; return null;
} }
} }
public TypeDescriptor getMapValueTypeDescriptor() { public TypeDescriptor getMapValueTypeDescriptor() {
if (isMap()) { if (isMap()) {
Class<?> valueType = resolveMapValueType(); Class<?> valueType = resolveMapValueType();
@ -96,33 +96,33 @@ abstract class AbstractDescriptor {
throw new IllegalStateException("Not a collection, array, or map: cannot resolve nested value types"); throw new IllegalStateException("Not a collection, array, or map: cannot resolve nested value types");
} }
} }
// subclassing hooks // subclassing hooks
public abstract Annotation[] getAnnotations(); public abstract Annotation[] getAnnotations();
protected abstract Class<?> resolveCollectionElementType(); protected abstract Class<?> resolveCollectionElementType();
protected abstract Class<?> resolveMapKeyType(); protected abstract Class<?> resolveMapKeyType();
protected abstract Class<?> resolveMapValueType(); protected abstract Class<?> resolveMapValueType();
protected abstract AbstractDescriptor nested(Class<?> type, int typeIndex); protected abstract AbstractDescriptor nested(Class<?> type, int typeIndex);
// internal helpers // internal helpers
private boolean isCollection() { private boolean isCollection() {
return Collection.class.isAssignableFrom(getType()); return Collection.class.isAssignableFrom(getType());
} }
private boolean isArray() { private boolean isArray() {
return getType().isArray(); return getType().isArray();
} }
private boolean isMap() { private boolean isMap() {
return Map.class.isAssignableFrom(getType()); return Map.class.isAssignableFrom(getType());
} }
} }

View File

@ -29,9 +29,9 @@ class BeanPropertyDescriptor extends AbstractDescriptor {
private final Property property; private final Property property;
private final MethodParameter methodParameter; private final MethodParameter methodParameter;
private final Annotation[] annotations; private final Annotation[] annotations;
public BeanPropertyDescriptor(Property property) { public BeanPropertyDescriptor(Property property) {
super(property.getType()); super(property.getType());
@ -45,7 +45,7 @@ class BeanPropertyDescriptor extends AbstractDescriptor {
public Annotation[] getAnnotations() { public Annotation[] getAnnotations() {
return this.annotations; return this.annotations;
} }
@Override @Override
protected Class<?> resolveCollectionElementType() { protected Class<?> resolveCollectionElementType() {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter); return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
@ -65,10 +65,10 @@ class BeanPropertyDescriptor extends AbstractDescriptor {
protected AbstractDescriptor nested(Class<?> type, int typeIndex) { protected AbstractDescriptor nested(Class<?> type, int typeIndex) {
MethodParameter methodParameter = new MethodParameter(this.methodParameter); MethodParameter methodParameter = new MethodParameter(this.methodParameter);
methodParameter.increaseNestingLevel(); methodParameter.increaseNestingLevel();
methodParameter.setTypeIndexForCurrentLevel(typeIndex); methodParameter.setTypeIndexForCurrentLevel(typeIndex);
return new BeanPropertyDescriptor(type, this.property, methodParameter, this.annotations); return new BeanPropertyDescriptor(type, this.property, methodParameter, this.annotations);
} }
// internal // internal
@ -78,5 +78,5 @@ class BeanPropertyDescriptor extends AbstractDescriptor {
this.methodParameter = methodParameter; this.methodParameter = methodParameter;
this.annotations = annotations; this.annotations = annotations;
} }
} }

View File

@ -52,5 +52,5 @@ class ClassDescriptor extends AbstractDescriptor {
protected AbstractDescriptor nested(Class<?> type, int typeIndex) { protected AbstractDescriptor nested(Class<?> type, int typeIndex) {
return new ClassDescriptor(type); return new ClassDescriptor(type);
} }
} }

View File

@ -22,7 +22,7 @@ import org.springframework.core.NestedRuntimeException;
* Base class for exceptions thrown by the conversion system. * Base class for exceptions thrown by the conversion system.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public abstract class ConversionException extends NestedRuntimeException { public abstract class ConversionException extends NestedRuntimeException {

View File

@ -63,7 +63,7 @@ public interface ConversionService {
* @throws IllegalArgumentException if targetType is null * @throws IllegalArgumentException if targetType is null
*/ */
<T> T convert(Object source, Class<T> targetType); <T> T convert(Object source, Class<T> targetType);
/** /**
* Convert the source to targetType. * Convert the source to targetType.
* The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations. * The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.
@ -77,4 +77,4 @@ public interface ConversionService {
*/ */
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType); Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType);
} }

View File

@ -55,7 +55,7 @@ class FieldDescriptor extends AbstractDescriptor {
public Annotation[] getAnnotations() { public Annotation[] getAnnotations() {
return TypeDescriptor.nullSafeAnnotations(this.field.getAnnotations()); return TypeDescriptor.nullSafeAnnotations(this.field.getAnnotations());
} }
@Override @Override
protected Class<?> resolveCollectionElementType() { protected Class<?> resolveCollectionElementType() {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel, this.typeIndexesPerLevel); return GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel, this.typeIndexesPerLevel);

View File

@ -34,7 +34,7 @@ class ParameterDescriptor extends AbstractDescriptor {
super(methodParameter.getParameterType()); super(methodParameter.getParameterType());
if (methodParameter.getNestingLevel() != 1) { if (methodParameter.getNestingLevel() != 1) {
throw new IllegalArgumentException("MethodParameter argument must have its nestingLevel set to 1"); throw new IllegalArgumentException("MethodParameter argument must have its nestingLevel set to 1");
} }
this.methodParameter = methodParameter; this.methodParameter = methodParameter;
} }
@ -53,7 +53,7 @@ class ParameterDescriptor extends AbstractDescriptor {
return TypeDescriptor.nullSafeAnnotations(this.methodParameter.getParameterAnnotations()); return TypeDescriptor.nullSafeAnnotations(this.methodParameter.getParameterAnnotations());
} }
} }
@Override @Override
protected Class<?> resolveCollectionElementType() { protected Class<?> resolveCollectionElementType() {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter); return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);

View File

@ -50,7 +50,7 @@ public final class Property {
private final Method writeMethod; private final Method writeMethod;
private final String name; private final String name;
private final MethodParameter methodParameter; private final MethodParameter methodParameter;
private final Annotation[] annotations; private final Annotation[] annotations;
@ -112,7 +112,7 @@ public final class Property {
// package private // package private
MethodParameter getMethodParameter() { MethodParameter getMethodParameter() {
return this.methodParameter; return this.methodParameter;
} }
@ -123,7 +123,7 @@ public final class Property {
// internal helpers // internal helpers
private String resolveName() { private String resolveName() {
if (this.readMethod != null) { if (this.readMethod != null) {
int index = this.readMethod.getName().indexOf("get"); int index = this.readMethod.getName().indexOf("get");
@ -166,27 +166,27 @@ public final class Property {
} }
return write; return write;
} }
private MethodParameter resolveReadMethodParameter() { private MethodParameter resolveReadMethodParameter() {
if (getReadMethod() == null) { if (getReadMethod() == null) {
return null; return null;
} }
return resolveParameterType(new MethodParameter(getReadMethod(), -1)); return resolveParameterType(new MethodParameter(getReadMethod(), -1));
} }
private MethodParameter resolveWriteMethodParameter() { private MethodParameter resolveWriteMethodParameter() {
if (getWriteMethod() == null) { if (getWriteMethod() == null) {
return null; return null;
} }
return resolveParameterType(new MethodParameter(getWriteMethod(), 0)); return resolveParameterType(new MethodParameter(getWriteMethod(), 0));
} }
private MethodParameter resolveParameterType(MethodParameter parameter) { private MethodParameter resolveParameterType(MethodParameter parameter) {
// needed to resolve generic property types that parameterized by sub-classes e.g. T getFoo(); // needed to resolve generic property types that parameterized by sub-classes e.g. T getFoo();
GenericTypeResolver.resolveParameterType(parameter, getObjectType()); GenericTypeResolver.resolveParameterType(parameter, getObjectType());
return parameter; return parameter;
} }
private Annotation[] resolveAnnotations() { private Annotation[] resolveAnnotations() {
Map<Class<?>, Annotation> annMap = new LinkedHashMap<Class<?>, Annotation>(); Map<Class<?>, Annotation> annMap = new LinkedHashMap<Class<?>, Annotation>();
Method readMethod = getReadMethod(); Method readMethod = getReadMethod();
@ -238,4 +238,4 @@ public final class Property {
} }
} }
} }

View File

@ -47,5 +47,5 @@ public interface ConditionalGenericConverter extends GenericConverter {
* @return true if conversion should be performed, false otherwise * @return true if conversion should be performed, false otherwise
*/ */
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType); boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType);
} }

View File

@ -21,6 +21,8 @@ package org.springframework.core.convert.converter;
* Implementations of this interface are thread-safe and can be shared. * Implementations of this interface are thread-safe and can be shared.
* *
* @author Keith Donald * @author Keith Donald
* @param <S> The source type
* @param <T> The target type
* @since 3.0 * @since 3.0
*/ */
public interface Converter<S, T> { public interface Converter<S, T> {

View File

@ -20,7 +20,7 @@ package org.springframework.core.convert.converter;
* A factory for "ranged" converters that can convert objects from S to subtypes of R. * A factory for "ranged" converters that can convert objects from S to subtypes of R.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @param <S> The source type converters created by this factory can convert from * @param <S> The source type converters created by this factory can convert from
* @param <R> The target range (or base) type converters created by this factory can convert to; * @param <R> The target range (or base) type converters created by this factory can convert to;
* for example {@link Number} for a set of number subtypes. * for example {@link Number} for a set of number subtypes.

View File

@ -24,7 +24,7 @@ package org.springframework.core.convert.converter;
* @since 3.0 * @since 3.0
*/ */
public interface ConverterRegistry { public interface ConverterRegistry {
/** /**
* Add a plain converter to this registry. * Add a plain converter to this registry.
* The convertible sourceType/targetType pair is derived from the Converter's parameterized types. * The convertible sourceType/targetType pair is derived from the Converter's parameterized types.
@ -44,11 +44,11 @@ public interface ConverterRegistry {
* Add a generic converter to this registry. * Add a generic converter to this registry.
*/ */
void addConverter(GenericConverter converter); void addConverter(GenericConverter converter);
/** /**
* Add a ranged converter factory to this registry. * Add a ranged converter factory to this registry.
* The convertible sourceType/rangeType pair is derived from the ConverterFactory's parameterized types. * The convertible sourceType/rangeType pair is derived from the ConverterFactory's parameterized types.
* @throws IllegalArgumentException if the parameterized types could not be resolved. * @throws IllegalArgumentException if the parameterized types could not be resolved.
*/ */
void addConverterFactory(ConverterFactory<?, ?> converterFactory); void addConverterFactory(ConverterFactory<?, ?> converterFactory);

View File

@ -27,8 +27,8 @@ import org.springframework.util.ObjectUtils;
/** /**
* Converts an Array to another Array. * Converts an Array to another Array.
* First adapts the source array to a List, then delegates to {@link CollectionToArrayConverter} to perform the target array conversion. * First adapts the source array to a List, then delegates to {@link CollectionToArrayConverter} to perform the target array conversion.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -48,7 +48,7 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
return this.helperConverter.matches(sourceType, targetType); return this.helperConverter.matches(sourceType, targetType);
} }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType); return this.helperConverter.convert(Arrays.asList(ObjectUtils.toObjectArray(source)), sourceType, targetType);
} }

View File

@ -32,7 +32,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter;
* <p>First, creates a new Collection of the requested targetType. * <p>First, creates a new Collection of the requested targetType.
* Then adds each array element to the target collection. * Then adds each array element to the target collection.
* Will perform an element conversion from the source component type to the collection's parameterized type if necessary. * Will perform an element conversion from the source component type to the collection's parameterized type if necessary.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -57,7 +57,7 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return null; return null;
} }
int length = Array.getLength(source); int length = Array.getLength(source);
Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), length); Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), length);
if (targetType.getElementTypeDescriptor() == null) { if (targetType.getElementTypeDescriptor() == null) {
@ -73,7 +73,7 @@ final class ArrayToCollectionConverter implements ConditionalGenericConverter {
sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor()); sourceType.elementTypeDescriptor(sourceElement), targetType.getElementTypeDescriptor());
target.add(targetElement); target.add(targetElement);
} }
} }
return target; return target;
} }

View File

@ -26,7 +26,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter;
/** /**
* Converts an Array to an Object by returning the first array element after converting it to the desired targetType. * Converts an Array to an Object by returning the first array element after converting it to the desired targetType.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */

View File

@ -28,7 +28,7 @@ import org.springframework.util.ObjectUtils;
/** /**
* Converts an Array to a comma-delimited String. * Converts an Array to a comma-delimited String.
* This implementation first adapts the source Array to a List, then delegates to {@link CollectionToStringConverter} to perform the target String conversion. * This implementation first adapts the source Array to a List, then delegates to {@link CollectionToStringConverter} to perform the target String conversion.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */

View File

@ -36,7 +36,7 @@ import org.springframework.util.NumberUtils;
* @see java.lang.Float * @see java.lang.Float
* @see java.lang.Double * @see java.lang.Double
* @see java.math.BigDecimal * @see java.math.BigDecimal
* @see NumberUtils * @see NumberUtils
*/ */
final class CharacterToNumberFactory implements ConverterFactory<Character, Number> { final class CharacterToNumberFactory implements ConverterFactory<Character, Number> {
@ -47,11 +47,11 @@ final class CharacterToNumberFactory implements ConverterFactory<Character, Numb
private static final class CharacterToNumber<T extends Number> implements Converter<Character, T> { private static final class CharacterToNumber<T extends Number> implements Converter<Character, T> {
private final Class<T> targetType; private final Class<T> targetType;
public CharacterToNumber(Class<T> targetType) { public CharacterToNumber(Class<T> targetType) {
this.targetType = targetType; this.targetType = targetType;
} }
public T convert(Character source) { public T convert(Character source) {
return NumberUtils.convertNumberToTargetClass((short) source.charValue(), this.targetType); return NumberUtils.convertNumberToTargetClass((short) source.charValue(), this.targetType);
} }

View File

@ -61,4 +61,4 @@ final class CollectionToObjectConverter implements ConditionalGenericConverter {
return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType); return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
} }
} }

View File

@ -49,4 +49,4 @@ final class FallbackObjectToStringConverter implements ConditionalGenericConvert
return (source != null ? source.toString() : null); return (source != null ? source.toString() : null);
} }
} }

View File

@ -102,7 +102,7 @@ public class GenericConversionService implements ConfigurableConversionService {
GenericConverter.ConvertiblePair typeInfo = new GenericConverter.ConvertiblePair(sourceType, targetType); GenericConverter.ConvertiblePair typeInfo = new GenericConverter.ConvertiblePair(sourceType, targetType);
addConverter(new ConverterAdapter(typeInfo, converter)); addConverter(new ConverterAdapter(typeInfo, converter));
} }
public void addConverter(GenericConverter converter) { public void addConverter(GenericConverter converter) {
Set<GenericConverter.ConvertiblePair> convertibleTypes = converter.getConvertibleTypes(); Set<GenericConverter.ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
for (GenericConverter.ConvertiblePair convertibleType : convertibleTypes) { for (GenericConverter.ConvertiblePair convertibleType : convertibleTypes) {
@ -119,7 +119,7 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory)); addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory));
} }
public void removeConvertible(Class<?> sourceType, Class<?> targetType) { public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
getSourceConverterMap(sourceType).remove(targetType); getSourceConverterMap(sourceType).remove(targetType);
invalidateCache(); invalidateCache();
@ -131,7 +131,7 @@ public class GenericConversionService implements ConfigurableConversionService {
public boolean canConvert(Class<?> sourceType, Class<?> targetType) { public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
if (targetType == null) { if (targetType == null) {
throw new IllegalArgumentException("The targetType to convert to cannot be null"); throw new IllegalArgumentException("The targetType to convert to cannot be null");
} }
return canConvert(sourceType != null ? TypeDescriptor.valueOf(sourceType) : null, TypeDescriptor.valueOf(targetType)); return canConvert(sourceType != null ? TypeDescriptor.valueOf(sourceType) : null, TypeDescriptor.valueOf(targetType));
} }
@ -150,7 +150,7 @@ public class GenericConversionService implements ConfigurableConversionService {
public <T> T convert(Object source, Class<T> targetType) { public <T> T convert(Object source, Class<T> targetType) {
if (targetType == null) { if (targetType == null) {
throw new IllegalArgumentException("The targetType to convert to cannot be null"); throw new IllegalArgumentException("The targetType to convert to cannot be null");
} }
return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType)); return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
} }
@ -172,7 +172,7 @@ public class GenericConversionService implements ConfigurableConversionService {
return handleResult(sourceType, targetType, result); return handleResult(sourceType, targetType, result);
} }
else { else {
return handleConverterNotFound(source, sourceType, targetType); return handleConverterNotFound(source, sourceType, targetType);
} }
} }
@ -189,7 +189,7 @@ public class GenericConversionService implements ConfigurableConversionService {
public Object convert(Object source, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor targetType) {
return convert(source, TypeDescriptor.forObject(source), 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()) {
@ -203,7 +203,7 @@ public class GenericConversionService implements ConfigurableConversionService {
for (String converterString : converterStrings) { for (String converterString : converterStrings) {
builder.append("\t"); builder.append("\t");
builder.append(converterString); builder.append(converterString);
builder.append("\n"); builder.append("\n");
} }
return builder.toString(); return builder.toString();
} }
@ -243,7 +243,7 @@ public class GenericConversionService implements ConfigurableConversionService {
else { else {
converter = findConverterForClassPair(sourceType, targetType); converter = findConverterForClassPair(sourceType, targetType);
if (converter == null) { if (converter == null) {
converter = getDefaultConverter(sourceType, targetType); converter = getDefaultConverter(sourceType, targetType);
} }
if (converter != null) { if (converter != null) {
this.converterCache.put(key, converter); this.converterCache.put(key, converter);
@ -285,7 +285,7 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
return matchable; return matchable;
} }
private void invalidateCache() { private void invalidateCache() {
this.converterCache.clear(); this.converterCache.clear();
} }
@ -366,7 +366,7 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
} }
Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class); Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
return getMatchingConverterForTarget(sourceType, targetType, objectConverters); return getMatchingConverterForTarget(sourceType, targetType, objectConverters);
} }
} }
@ -473,9 +473,9 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
else { else {
throw new ConverterNotFoundException(sourceType, targetType); throw new ConverterNotFoundException(sourceType, targetType);
} }
} }
private Object handleResult(TypeDescriptor sourceType, TypeDescriptor targetType, Object result) { private Object handleResult(TypeDescriptor sourceType, TypeDescriptor targetType, Object result) {
if (result == null) { if (result == null) {
assertNotPrimitiveTargetType(sourceType, targetType); assertNotPrimitiveTargetType(sourceType, targetType);
@ -486,9 +486,9 @@ public class GenericConversionService implements ConfigurableConversionService {
if (targetType.isPrimitive()) { if (targetType.isPrimitive()) {
throw new ConversionFailedException(sourceType, targetType, null, throw new ConversionFailedException(sourceType, targetType, null,
new IllegalArgumentException("A null value cannot be assigned to a primitive type")); new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private final class ConverterAdapter implements GenericConverter { private final class ConverterAdapter implements GenericConverter {
@ -516,7 +516,7 @@ public class GenericConversionService implements ConfigurableConversionService {
} }
return this.converter.convert(source); return this.converter.convert(source);
} }
public String toString() { public String toString() {
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() + return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
" : " + this.converter.toString(); " : " + this.converter.toString();
@ -613,14 +613,14 @@ public class GenericConversionService implements ConfigurableConversionService {
private static final class ConverterCacheKey { private static final class ConverterCacheKey {
private final TypeDescriptor sourceType; private final TypeDescriptor sourceType;
private final TypeDescriptor targetType; private final TypeDescriptor targetType;
public ConverterCacheKey(TypeDescriptor sourceType, TypeDescriptor targetType) { public ConverterCacheKey(TypeDescriptor sourceType, TypeDescriptor targetType) {
this.sourceType = sourceType; this.sourceType = sourceType;
this.targetType = targetType; this.targetType = targetType;
} }
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) { if (this == other) {
return true; return true;
@ -631,11 +631,11 @@ public class GenericConversionService implements ConfigurableConversionService {
ConverterCacheKey otherKey = (ConverterCacheKey) other; ConverterCacheKey otherKey = (ConverterCacheKey) other;
return this.sourceType.equals(otherKey.sourceType) && this.targetType.equals(otherKey.targetType); return this.sourceType.equals(otherKey.sourceType) && this.targetType.equals(otherKey.targetType);
} }
public int hashCode() { public int hashCode() {
return this.sourceType.hashCode() * 29 + this.targetType.hashCode(); return this.sourceType.hashCode() * 29 + this.targetType.hashCode();
} }
public String toString() { public String toString() {
return "ConverterCacheKey [sourceType = " + this.sourceType + ", targetType = " + this.targetType + "]"; return "ConverterCacheKey [sourceType = " + this.sourceType + ", targetType = " + this.targetType + "]";
} }

View File

@ -84,7 +84,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
private static Method getValueOfMethodOn(Class<?> clazz, Class<?> sourceParameterType) { private static Method getValueOfMethodOn(Class<?> clazz, Class<?> sourceParameterType) {
return ClassUtils.getStaticMethod(clazz, "valueOf", sourceParameterType); return ClassUtils.getStaticMethod(clazz, "valueOf", sourceParameterType);
} }
private static Constructor<?> getConstructor(Class<?> clazz, Class<?> sourceParameterType) { private static Constructor<?> getConstructor(Class<?> clazz, Class<?> sourceParameterType) {
return ClassUtils.getConstructorIfAvailable(clazz, sourceParameterType); return ClassUtils.getConstructorIfAvailable(clazz, sourceParameterType);
} }

View File

@ -28,7 +28,7 @@ import org.springframework.util.StringUtils;
/** /**
* Converts a comma-delimited String to an Array. * Converts a comma-delimited String to an Array.
* Only matches if String.class can be converted to the target array element type. * Only matches if String.class can be converted to the target array element type.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */
@ -47,11 +47,11 @@ final class StringToArrayConverter implements ConditionalGenericConverter {
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor()); return this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor());
} }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return null; return null;
} }
String string = (String) source; String string = (String) source;
String[] fields = StringUtils.commaDelimitedListToStringArray(string); String[] fields = StringUtils.commaDelimitedListToStringArray(string);
Object target = Array.newInstance(targetType.getElementTypeDescriptor().getType(), fields.length); Object target = Array.newInstance(targetType.getElementTypeDescriptor().getType(), fields.length);
@ -63,4 +63,4 @@ final class StringToArrayConverter implements ConditionalGenericConverter {
return target; return target;
} }
} }

View File

@ -45,7 +45,7 @@ final class StringToBooleanConverter implements Converter<String, Boolean> {
falseValues.add("no"); falseValues.add("no");
falseValues.add("0"); falseValues.add("0");
} }
public Boolean convert(String source) { public Boolean convert(String source) {
String value = source.trim(); String value = source.trim();
if ("".equals(value)) { if ("".equals(value)) {

View File

@ -54,7 +54,7 @@ final class StringToCollectionConverter implements ConditionalGenericConverter {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) { if (source == null) {
return null; return null;
} }
@ -64,14 +64,14 @@ final class StringToCollectionConverter implements ConditionalGenericConverter {
if (targetType.getElementTypeDescriptor() == null) { if (targetType.getElementTypeDescriptor() == null) {
for (String field : fields) { for (String field : fields) {
target.add(field.trim()); target.add(field.trim());
} }
} else { } else {
for (String field : fields) { for (String field : fields) {
Object targetElement = this.conversionService.convert(field.trim(), sourceType, targetType.getElementTypeDescriptor()); Object targetElement = this.conversionService.convert(field.trim(), sourceType, targetType.getElementTypeDescriptor());
target.add(targetElement); target.add(targetElement);
} }
} }
return target; return target;
} }
} }

View File

@ -24,7 +24,7 @@ import org.springframework.core.convert.converter.Converter;
/** /**
* Converts a String to a Properties by calling Properties#load(java.io.InputStream). * Converts a String to a Properties by calling Properties#load(java.io.InputStream).
* Uses ISO-8559-1 encoding required by Properties. * Uses ISO-8559-1 encoding required by Properties.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
*/ */

View File

@ -68,13 +68,13 @@ public class CollectionToCollectionConverterTests {
} }
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType)); assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType); List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
assertFalse(list.equals(result)); assertFalse(list.equals(result));
assertEquals((Integer) 9, result.get(0)); assertEquals((Integer) 9, result.get(0));
assertEquals((Integer) 37, result.get(1)); assertEquals((Integer) 37, result.get(1));
} }
public ArrayList<Integer> scalarListTarget; public ArrayList<Integer> scalarListTarget;
@Test @Test
@ -148,7 +148,7 @@ public class CollectionToCollectionConverterTests {
} }
public List<List<List<Integer>>> objectToCollection; public List<List<List<Integer>>> objectToCollection;
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void stringToCollection() throws Exception { public void stringToCollection() throws Exception {
@ -157,7 +157,7 @@ public class CollectionToCollectionConverterTests {
list.add(Arrays.asList("37,23")); list.add(Arrays.asList("37,23"));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.addConverter(new StringToCollectionConverter(conversionService)); conversionService.addConverter(new StringToCollectionConverter(conversionService));
conversionService.addConverter(new ObjectToCollectionConverter(conversionService)); conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
conversionService.addConverter(new CollectionToObjectConverter(conversionService)); conversionService.addConverter(new CollectionToObjectConverter(conversionService));
TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection")); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
@ -204,10 +204,10 @@ public class CollectionToCollectionConverterTests {
List<String> resources = new ArrayList<String>(); List<String> resources = new ArrayList<String>();
resources.add(null); resources.add(null);
resources.add(null); resources.add(null);
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings")); TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
} }
public List<String> strings; public List<String> strings;
@Test(expected=ConversionFailedException.class) @Test(expected=ConversionFailedException.class)
@ -271,9 +271,9 @@ public class CollectionToCollectionConverterTests {
return null; return null;
} }
} }
public static class TestResource extends BaseResource { public static class TestResource extends BaseResource {
} }
@Test @Test

View File

@ -60,7 +60,7 @@ public class DefaultConversionTests {
public void testStringToCharacter() { public void testStringToCharacter() {
assertEquals(Character.valueOf('1'), conversionService.convert("1", Character.class)); assertEquals(Character.valueOf('1'), conversionService.convert("1", Character.class));
} }
@Test @Test
public void testStringToCharacterEmptyString() { public void testStringToCharacterEmptyString() {
assertEquals(null, conversionService.convert("", Character.class)); assertEquals(null, conversionService.convert("", Character.class));
@ -208,7 +208,7 @@ public class DefaultConversionTests {
public void testStringToEnum() throws Exception { public void testStringToEnum() throws Exception {
assertEquals(Foo.BAR, conversionService.convert("BAR", Foo.class)); assertEquals(Foo.BAR, conversionService.convert("BAR", Foo.class));
} }
@Test @Test
public void testStringToEnumEmptyString() { public void testStringToEnumEmptyString() {
assertEquals(null, conversionService.convert("", Foo.class)); assertEquals(null, conversionService.convert("", Foo.class));
@ -233,12 +233,12 @@ public class DefaultConversionTests {
String str = "test"; String str = "test";
assertSame(str, conversionService.convert(str, String.class)); assertSame(str, conversionService.convert(str, String.class));
} }
@Test @Test
public void testNumberToNumber() { public void testNumberToNumber() {
assertEquals(Long.valueOf(1), conversionService.convert(Integer.valueOf(1), Long.class)); assertEquals(Long.valueOf(1), conversionService.convert(Integer.valueOf(1), Long.class));
} }
@Test(expected=ConversionFailedException.class) @Test(expected=ConversionFailedException.class)
public void testNumberToNumberNotSupportedNumber() { public void testNumberToNumberNotSupportedNumber() {
conversionService.convert(Integer.valueOf(1), CustomNumber.class); conversionService.convert(Integer.valueOf(1), CustomNumber.class);
@ -265,9 +265,9 @@ public class DefaultConversionTests {
public long longValue() { public long longValue() {
return 0; return 0;
} }
} }
@Test @Test
public void testNumberToCharacter() { public void testNumberToCharacter() {
assertEquals(Character.valueOf('A'), conversionService.convert(Integer.valueOf(65), Character.class)); assertEquals(Character.valueOf('A'), conversionService.convert(Integer.valueOf(65), Character.class));
@ -312,11 +312,11 @@ public class DefaultConversionTests {
public class ColorConverter implements Converter<String, Color> { public class ColorConverter implements Converter<String, Color> {
public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); } public Color convert(String source) { if (!source.startsWith("#")) source = "#" + source; return Color.decode(source); }
} }
public void handlerMethod(List<Color> color) { public void handlerMethod(List<Color> color) {
} }
@Test @Test
public void convertArrayToCollectionImpl() { public void convertArrayToCollectionImpl() {
LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class); LinkedList<?> result = conversionService.convert(new String[] { "1", "2", "3" }, LinkedList.class);
@ -333,7 +333,7 @@ public class DefaultConversionTests {
public static enum FooEnum { public static enum FooEnum {
BAR, BAZ BAR, BAZ
} }
@Test @Test
public void convertArrayToString() { public void convertArrayToString() {
String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class); String result = conversionService.convert(new String[] { "1", "2", "3" }, String.class);
@ -505,7 +505,7 @@ public class DefaultConversionTests {
Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget"))); Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget")));
assertEquals(source, result); assertEquals(source, result);
} }
@Test @Test
public void convertObjectToCollection() { public void convertObjectToCollection() {
List<String> result = (List<String>) conversionService.convert(3L, List.class); List<String> result = (List<String>) conversionService.convert(3L, List.class);
@ -592,7 +592,7 @@ public class DefaultConversionTests {
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));
} }
@Test @Test
public void collection() { public void collection() {
List<String> strings = new ArrayList<String>(); List<String> strings = new ArrayList<String>();
@ -652,7 +652,7 @@ public class DefaultConversionTests {
assertEquals("baz", result.get("bar")); assertEquals("baz", result.get("bar"));
assertEquals("boop", result.get("baz")); assertEquals("boop", result.get("baz"));
} }
// generic object conversion // generic object conversion
@Test @Test
@ -664,7 +664,7 @@ public class DefaultConversionTests {
public void convertObjectToStringStringConstructorPresent() { public void convertObjectToStringStringConstructorPresent() {
assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class)); assertEquals("123456789", conversionService.convert(new SSN("123456789"), String.class));
} }
@Test @Test
public void convertObjectToStringNotSupported() { public void convertObjectToStringNotSupported() {
assertFalse(conversionService.canConvert(TestEntity.class, String.class)); assertFalse(conversionService.canConvert(TestEntity.class, String.class));
@ -685,17 +685,17 @@ public class DefaultConversionTests {
public void convertObjectToObjectNoValueOFMethodOrConstructor() { public void convertObjectToObjectNoValueOFMethodOrConstructor() {
conversionService.convert(new Long(3), SSN.class); conversionService.convert(new Long(3), SSN.class);
} }
public Object assignableTarget; public Object assignableTarget;
private static class SSN { private static class SSN {
private String value; private String value;
public SSN(String value) { public SSN(String value) {
this.value = value; this.value = value;
} }
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof SSN)) { if (!(o instanceof SSN)) {
return false; return false;
@ -703,24 +703,24 @@ public class DefaultConversionTests {
SSN ssn = (SSN) o; SSN ssn = (SSN) o;
return this.value.equals(ssn.value); return this.value.equals(ssn.value);
} }
public int hashCode() { public int hashCode() {
return value.hashCode(); return value.hashCode();
} }
public String toString() { public String toString() {
return value; return value;
} }
} }
private static class ISBN { private static class ISBN {
private String value; private String value;
private ISBN(String value) { private ISBN(String value) {
this.value = value; this.value = value;
} }
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof ISBN)) { if (!(o instanceof ISBN)) {
return false; return false;
@ -728,20 +728,20 @@ public class DefaultConversionTests {
ISBN isbn = (ISBN) o; ISBN isbn = (ISBN) o;
return this.value.equals(isbn.value); return this.value.equals(isbn.value);
} }
public int hashCode() { public int hashCode() {
return value.hashCode(); return value.hashCode();
} }
public String toString() { public String toString() {
return value; return value;
} }
public static ISBN valueOf(String value) { public static ISBN valueOf(String value) {
return new ISBN(value); return new ISBN(value);
} }
} }
@Test @Test
public void convertObjectToObjectFinderMethod() { public void convertObjectToObjectFinderMethod() {
TestEntity e = conversionService.convert(1L, TestEntity.class); TestEntity e = conversionService.convert(1L, TestEntity.class);
@ -763,18 +763,18 @@ public class DefaultConversionTests {
public static class TestEntity { public static class TestEntity {
private Long id; private Long id;
public TestEntity(Long id) { public TestEntity(Long id) {
this.id = id; this.id = id;
} }
public Long getId() { public Long getId() {
return id; return id;
} }
public static TestEntity findTestEntity(Long id) { public static TestEntity findTestEntity(Long id) {
return new TestEntity(id); return new TestEntity(id);
} }
} }
} }