polishing

This commit is contained in:
Juergen Hoeller
2009-11-27 01:34:56 +00:00
parent bc06ffb6b8
commit cc0bd730eb
9 changed files with 129 additions and 105 deletions

View File

@@ -24,7 +24,6 @@ import org.springframework.core.NestedRuntimeException;
* @author Keith Donald
* @since 3.0
*/
@SuppressWarnings("serial")
public abstract class ConversionException extends NestedRuntimeException {
/**

View File

@@ -22,13 +22,13 @@ package org.springframework.core.convert;
* @author Keith Donald
* @since 3.0
*/
@SuppressWarnings("serial")
public final class ConversionFailedException extends ConversionException {
private final TypeDescriptor sourceType;
private final TypeDescriptor targetType;
/**
* Create a new conversion exception.
* @param value the value we tried to convert
@@ -37,11 +37,13 @@ public final class ConversionFailedException extends ConversionException {
* @param cause the cause of the conversion failure
*/
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
super(buildDefaultMessage(value, sourceType, targetType, cause), cause);
super("Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type [" +
targetType.getName() + "]; reason = '" + cause.getMessage() + "'", cause);
this.sourceType = sourceType;
this.targetType = targetType;
}
/**
* Return the source type we tried to convert the value from.
*/
@@ -56,10 +58,4 @@ public final class ConversionFailedException extends ConversionException {
return this.targetType;
}
private static String buildDefaultMessage(Object value, TypeDescriptor sourceType, TypeDescriptor targetType,
Throwable cause) {
return "Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type ["
+ targetType.getName() + "]; reason = '" + cause.getMessage() + "'";
}
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Context about a type to convert to.
@@ -42,6 +43,7 @@ public class TypeDescriptor {
*/
public static final TypeDescriptor NULL = new TypeDescriptor();
private Class<?> type;
private TypeDescriptor elementType;
@@ -52,6 +54,7 @@ public class TypeDescriptor {
private Annotation[] cachedFieldAnnotations;
/**
* Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a source such as a Map
@@ -99,6 +102,7 @@ public class TypeDescriptor {
this.elementType = elementType;
}
/**
* Return the wrapped MethodParameter, if any.
* <p>Note: Either MethodParameter or Field is available.
@@ -124,11 +128,14 @@ public class TypeDescriptor {
public Class<?> getType() {
if (this.type != null) {
return this.type;
} else if (this.field != null) {
}
else if (this.field != null) {
return this.field.getType();
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return this.methodParameter.getParameterType();
} else {
}
else {
return null;
}
}
@@ -139,7 +146,7 @@ public class TypeDescriptor {
*/
public Class<?> getObjectType() {
Class<?> type = getType();
return type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type;
return (type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type);
}
/**
@@ -147,8 +154,7 @@ public class TypeDescriptor {
* @param type the type to test against
*/
public boolean typeEquals(Class<?> type) {
Class<?> thisType = getType();
return thisType != null ? thisType.equals(type) : false;
return ObjectUtils.nullSafeEquals(getType(), type);
}
/**
@@ -158,7 +164,8 @@ public class TypeDescriptor {
Class<?> type = getType();
if (type != null) {
return getType().getName();
} else {
}
else {
return null;
}
}
@@ -198,14 +205,17 @@ public class TypeDescriptor {
* Return the element type as a type descriptor.
*/
public TypeDescriptor getElementTypeDescriptor() {
if (elementType != null) {
return elementType;
} else {
if (this.elementType != null) {
return this.elementType;
}
else {
if (isArray()) {
return TypeDescriptor.valueOf(getArrayComponentType());
} else if (isCollection()) {
}
else if (isCollection()) {
return TypeDescriptor.valueOf(getCollectionElementType());
} else {
}
else {
return TypeDescriptor.NULL;
}
}
@@ -232,9 +242,11 @@ public class TypeDescriptor {
public Class<?> getMapKeyType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getMapKeyFieldType(field);
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter);
} else {
}
else {
return null;
}
}
@@ -246,9 +258,11 @@ public class TypeDescriptor {
public Class<?> getMapValueType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getMapValueFieldType(this.field);
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter);
} else {
}
else {
return null;
}
}
@@ -276,9 +290,11 @@ public class TypeDescriptor {
this.cachedFieldAnnotations = this.field.getAnnotations();
}
return this.cachedFieldAnnotations;
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return this.methodParameter.getParameterAnnotations();
} else {
}
else {
return new Annotation[0];
}
}
@@ -324,6 +340,28 @@ public class TypeDescriptor {
return type != null && ClassUtils.isAssignable(targetType.getType(), type);
}
private Class<?> getArrayComponentType() {
return getType().getComponentType();
}
@SuppressWarnings("unchecked")
private Class<?> getCollectionElementType() {
if (this.type != null) {
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
}
else if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
}
else {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
}
}
private boolean isTypeAssignableTo(Class<?> clazz) {
Class<?> type = getType();
return (type != null && ClassUtils.isAssignable(clazz, type));
}
/**
* @return a textual representation of the type descriptor (eg. Map<String,Foo>) for use in messages
*/
@@ -332,7 +370,8 @@ public class TypeDescriptor {
if (isArray()) {
// TODO should properly handle multi dimensional arrays
stringValue.append(getArrayComponentType().getName()).append("[]");
} else {
}
else {
Class<?> clazz = getType();
if (clazz == null) {
return "null";
@@ -355,27 +394,23 @@ public class TypeDescriptor {
return stringValue.toString();
}
// internal helpers
private Class<?> getArrayComponentType() {
return getType().getComponentType();
}
@SuppressWarnings("unchecked")
private Class<?> getCollectionElementType() {
if (this.type != null) {
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
} else if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
} else {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
public String toString() {
if (this == TypeDescriptor.NULL) {
return "[TypeDescriptor.NULL]";
}
else {
StringBuilder builder = new StringBuilder();
builder.append("[TypeDescriptor ");
Annotation[] anns = getAnnotations();
for (Annotation ann : anns) {
builder.append("@").append(ann.annotationType().getName()).append(' ');
}
builder.append(getType().getName());
builder.append("]");
return builder.toString();
}
}
private boolean isTypeAssignableTo(Class<?> clazz) {
Class<?> type = getType();
return (type != null && ClassUtils.isAssignable(clazz, type));
}
// static factory methods
@@ -406,21 +441,5 @@ public class TypeDescriptor {
public static TypeDescriptor collection(Class<?> type, TypeDescriptor elementType) {
return new TypeDescriptor(type, elementType);
}
public String toString() {
if (this == TypeDescriptor.NULL) {
return "[TypeDescriptor.NULL]";
} else {
StringBuilder builder = new StringBuilder();
builder.append("[TypeDescriptor ");
Annotation[] anns = getAnnotations();
for (Annotation ann : anns) {
builder.append("@" + ann.annotationType().getName()).append(' ');
}
builder.append(getType().getName());
builder.append("]");
return builder.toString();
}
}
}

View File

@@ -62,8 +62,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
};
private final Map<Class<?>, Map<Class<?>, MatchableConverters>> converters = new HashMap<Class<?>, Map<Class<?>, MatchableConverters>>(
36);
private final Map<Class<?>, Map<Class<?>, MatchableConverters>> converters = new HashMap<Class<?>, Map<Class<?>, MatchableConverters>>(36);
// implementing ConverterRegistry
@@ -182,7 +181,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
GenericConverter converter = findConverterForClassPair(sourceType, targetType);
if (converter != null) {
return converter;
} else {
}
else {
return getDefaultConverter(sourceType, targetType);
}
}
@@ -199,11 +199,13 @@ public class GenericConversionService implements ConversionService, ConverterReg
protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (sourceType.isAssignableTo(targetType)) {
return NO_OP_CONVERTER;
} else {
}
else {
return null;
}
}
// internal helpers
private Class<?>[] getRequiredTypeInfo(Object converter, Class<?> genericIfc) {
@@ -256,7 +258,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
return getMatchingConverterForTarget(sourceType, targetType, objectConverters);
} else {
}
else {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(sourceObjectType);
while (!classQueue.isEmpty()) {
@@ -271,7 +274,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (componentType.getSuperclass() != null) {
classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
}
} else {
}
else {
Class<?>[] interfaces = currentClass.getInterfaces();
for (Class<?> ifc : interfaces) {
classQueue.addFirst(ifc);
@@ -295,6 +299,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
private GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType, TypeDescriptor targetType,
Map<Class<?>, MatchableConverters> converters) {
Class<?> targetObjectType = targetType.getObjectType();
if (targetObjectType.isInterface()) {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
@@ -312,7 +317,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
return matchConverter(converters.get(Object.class), sourceType, targetType);
} else {
}
else {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(targetObjectType);
while (!classQueue.isEmpty()) {
@@ -343,9 +349,11 @@ public class GenericConversionService implements ConversionService, ConverterReg
private GenericConverter matchConverter(MatchableConverters matchable, TypeDescriptor sourceFieldType,
TypeDescriptor targetFieldType) {
return matchable != null ? matchable.matchConverter(sourceFieldType, targetFieldType) : null;
}
@SuppressWarnings("unchecked")
private final class ConverterAdapter implements GenericConverter {
@@ -374,6 +382,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
@SuppressWarnings("unchecked")
private final class ConverterFactoryAdapter implements GenericConverter {
@@ -400,9 +409,9 @@ public class GenericConversionService implements ConversionService, ConverterReg
public String toString() {
return this.typeInfo[0].getName() + " -> " + this.typeInfo[1].getName() + " : " + this.converterFactory.toString();
}
}
private static class MatchableConverters {
private LinkedList<ConditionalGenericConverter> conditionalConverters;
@@ -458,7 +467,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
return this.defaultConverter.toString();
}
}
}
}
}

View File

@@ -213,7 +213,7 @@ public abstract class CollectionUtils {
* or <code>null</code> if none or more than one such value found
*/
@SuppressWarnings("unchecked")
public static <T> T findValueOfType(Collection collection, Class<T> type) {
public static <T> T findValueOfType(Collection<?> collection, Class<T> type) {
if (isEmpty(collection)) {
return null;
}
@@ -239,11 +239,11 @@ public abstract class CollectionUtils {
* @return a value of one of the given types found if there is a clear match,
* or <code>null</code> if none or more than one such value found
*/
public static Object findValueOfType(Collection collection, Class[] types) {
public static Object findValueOfType(Collection<?> collection, Class<?>[] types) {
if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
return null;
}
for (Class type : types) {
for (Class<?> type : types) {
Object value = findValueOfType(collection, type);
if (value != null) {
return value;
@@ -264,8 +264,7 @@ public abstract class CollectionUtils {
}
boolean hasCandidate = false;
Object candidate = null;
for (Iterator it = collection.iterator(); it.hasNext();) {
Object elem = it.next();
for (Object elem : collection) {
if (!hasCandidate) {
hasCandidate = true;
candidate = elem;
@@ -286,6 +285,7 @@ public abstract class CollectionUtils {
return new EnumerationIterator<E>(enumeration);
}
/**
* Iterator wrapping an Enumeration.
*/
@@ -298,15 +298,16 @@ public abstract class CollectionUtils {
}
public boolean hasNext() {
return enumeration.hasMoreElements();
return this.enumeration.hasMoreElements();
}
public E next() {
return enumeration.nextElement();
return this.enumeration.nextElement();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Not supported");
}
}
}