use TypeDescriptor.forObject instead of constructor; enforce use of TypeDescriptor.valueOf through making the constructor private

This commit is contained in:
Juergen Hoeller 2009-12-15 20:18:31 +00:00
parent 7fb19d658b
commit dc99df2972
4 changed files with 107 additions and 114 deletions

View File

@ -194,7 +194,7 @@ class TypeConverterDelegate {
// No custom editor but custom ConversionService specified? // No custom editor but custom ConversionService specified?
ConversionService conversionService = this.propertyEditorRegistry.getConversionService(); ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
if (editor == null && conversionService != null && convertedValue != null) { if (editor == null && conversionService != null && convertedValue != null) {
TypeDescriptor sourceTypeDesc = new TypeDescriptor(convertedValue); TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(convertedValue);
if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) { if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
return (T) conversionService.convert(convertedValue, sourceTypeDesc, typeDescriptor); return (T) conversionService.convert(convertedValue, sourceTypeDesc, typeDescriptor);
} }

View File

@ -43,39 +43,38 @@ public class TypeDescriptor {
private static final Map<Class<?>, TypeDescriptor> typeDescriptorCache = new HashMap<Class<?>, TypeDescriptor>(); private static final Map<Class<?>, TypeDescriptor> typeDescriptorCache = new HashMap<Class<?>, TypeDescriptor>();
static { static {
typeDescriptorCache.put(boolean.class, new TypeDescriptor(boolean.class));
typeDescriptorCache.put(Boolean.class, new TypeDescriptor(Boolean.class));
typeDescriptorCache.put(byte.class, new TypeDescriptor(byte.class)); typeDescriptorCache.put(byte.class, new TypeDescriptor(byte.class));
typeDescriptorCache.put(Byte.class, new TypeDescriptor(Byte.class)); typeDescriptorCache.put(Byte.class, new TypeDescriptor(Byte.class));
typeDescriptorCache.put(char.class, new TypeDescriptor(char.class)); typeDescriptorCache.put(char.class, new TypeDescriptor(char.class));
typeDescriptorCache.put(Character.class, new TypeDescriptor(Character.class)); typeDescriptorCache.put(Character.class, new TypeDescriptor(Character.class));
typeDescriptorCache.put(boolean.class, new TypeDescriptor(boolean.class)); typeDescriptorCache.put(double.class, new TypeDescriptor(double.class));
typeDescriptorCache.put(Boolean.class, new TypeDescriptor(Boolean.class)); typeDescriptorCache.put(Double.class, new TypeDescriptor(Double.class));
typeDescriptorCache.put(short.class, new TypeDescriptor(short.class)); typeDescriptorCache.put(float.class, new TypeDescriptor(float.class));
typeDescriptorCache.put(Short.class, new TypeDescriptor(Short.class)); typeDescriptorCache.put(Float.class, new TypeDescriptor(Float.class));
typeDescriptorCache.put(int.class, new TypeDescriptor(int.class)); typeDescriptorCache.put(int.class, new TypeDescriptor(int.class));
typeDescriptorCache.put(Integer.class, new TypeDescriptor(Integer.class)); typeDescriptorCache.put(Integer.class, new TypeDescriptor(Integer.class));
typeDescriptorCache.put(long.class, new TypeDescriptor(long.class)); typeDescriptorCache.put(long.class, new TypeDescriptor(long.class));
typeDescriptorCache.put(Long.class, new TypeDescriptor(Long.class)); typeDescriptorCache.put(Long.class, new TypeDescriptor(Long.class));
typeDescriptorCache.put(float.class, new TypeDescriptor(float.class)); typeDescriptorCache.put(short.class, new TypeDescriptor(short.class));
typeDescriptorCache.put(Float.class, new TypeDescriptor(Float.class)); typeDescriptorCache.put(Short.class, new TypeDescriptor(Short.class));
typeDescriptorCache.put(double.class, new TypeDescriptor(double.class));
typeDescriptorCache.put(Double.class, new TypeDescriptor(Double.class));
typeDescriptorCache.put(String.class, new TypeDescriptor(String.class)); typeDescriptorCache.put(String.class, new TypeDescriptor(String.class));
} }
private Object value; private Object value;
private Class<?> type; private Class<?> type;
private TypeDescriptor elementType;
private MethodParameter methodParameter; private MethodParameter methodParameter;
private Field field; private Field field;
@ -83,17 +82,6 @@ public class TypeDescriptor {
private Annotation[] cachedFieldAnnotations; private Annotation[] cachedFieldAnnotations;
/**
* Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a plain source type,
* 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. * Create a new type descriptor from a method or constructor parameter.
* <p>Use this constructor when a target conversion point originates from a method parameter, * <p>Use this constructor when a target conversion point originates from a method parameter,
@ -115,18 +103,6 @@ public class TypeDescriptor {
this.field = field; this.field = field;
} }
/**
* Create a new descriptor for the type of the given value.
* <p>Use this constructor when a conversion point comes from a source such as a Map or
* Collection, where no additional context is available but elements can be introspected.
* @param type the actual type to wrap
*/
public TypeDescriptor(Object value) {
Assert.notNull(value, "Value must not be null");
this.value = value;
this.type = value.getClass();
}
// protected constructors for subclasses // protected constructors for subclasses
/** /**
@ -148,6 +124,30 @@ public class TypeDescriptor {
private TypeDescriptor() { private TypeDescriptor() {
} }
/**
* Create a new descriptor for the type of the given value.
* <p>Use this constructor when a conversion point comes from a source such as a Map or
* Collection, where no additional context is available but elements can be introspected.
* @param type the actual type to wrap
*/
private TypeDescriptor(Object value) {
Assert.notNull(value, "Value must not be null");
this.value = value;
this.type = value.getClass();
}
/**
* Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a plain source type,
* where no additional context is available.
* @param type the actual type to wrap
*/
private TypeDescriptor(Class<?> type) {
Assert.notNull(type, "Type must not be null");
this.type = type;
}
/** /**
* Return the wrapped MethodParameter, if any. * Return the wrapped MethodParameter, if any.
* <p>Note: Either MethodParameter or Field is available. * <p>Note: Either MethodParameter or Field is available.
@ -237,19 +237,14 @@ public class TypeDescriptor {
* Return the element type as a type descriptor. * Return the element type as a type descriptor.
*/ */
public TypeDescriptor getElementTypeDescriptor() { public TypeDescriptor getElementTypeDescriptor() {
if (this.elementType != null) { if (isArray()) {
return this.elementType; return TypeDescriptor.valueOf(getArrayComponentType());
}
else if (isCollection()) {
return TypeDescriptor.valueOf(getCollectionElementType());
} }
else { else {
if (isArray()) { return TypeDescriptor.NULL;
return TypeDescriptor.valueOf(getArrayComponentType());
}
else if (isCollection()) {
return TypeDescriptor.valueOf(getCollectionElementType());
}
else {
return TypeDescriptor.NULL;
}
} }
} }
@ -426,30 +421,7 @@ public class TypeDescriptor {
return builder.toString(); return builder.toString();
} }
} }
// static factory methods
/**
* Creates a new type descriptor for the given class.
* @param type the class
* @return the type descriptor
*/
public static TypeDescriptor valueOf(Class<?> type) {
if (type == null) {
return TypeDescriptor.NULL;
}
TypeDescriptor desc = typeDescriptorCache.get(type);
return desc != null ? desc : new TypeDescriptor(type);
}
/**
* Creates a new type descriptor for the class of the given object.
* @param object the object
* @return the type descriptor
*/
public static TypeDescriptor forObject(Object object) {
return (object == null ? NULL : valueOf(object.getClass()));
}
// internal helpers // internal helpers
@ -486,5 +458,38 @@ public class TypeDescriptor {
Class<?> type = getType(); Class<?> type = getType();
return (type != null && ClassUtils.isAssignable(clazz, type)); return (type != null && ClassUtils.isAssignable(clazz, type));
} }
}
// static factory methods
/**
* Create a new type descriptor for the given class.
* @param type the class
* @return the type descriptor
*/
public static TypeDescriptor valueOf(Class<?> type) {
if (type == null) {
return TypeDescriptor.NULL;
}
TypeDescriptor desc = typeDescriptorCache.get(type);
return (desc != null ? desc : new TypeDescriptor(type));
}
/**
* Create a new type descriptor for the class of the given object.
* @param object the object
* @return the type descriptor
*/
public static TypeDescriptor forObject(Object object) {
if (object == null) {
return NULL;
}
else if (object instanceof Collection || object instanceof Map) {
return new TypeDescriptor(object);
}
else {
return valueOf(object.getClass());
}
}
}

View File

@ -58,10 +58,8 @@ public abstract class ReflectionUtils {
* the supplied <code>name</code> and/or {@link Class type}. Searches all * the supplied <code>name</code> and/or {@link Class type}. Searches all
* superclasses up to {@link Object}. * superclasses up to {@link Object}.
* @param clazz the class to introspect * @param clazz the class to introspect
* @param name the name of the field (may be <code>null</code> if type is * @param name the name of the field (may be <code>null</code> if type is specified)
* specified) * @param type the type of the field (may be <code>null</code> if name is specified)
* @param type the type of the field (may be <code>null</code> if name is
* specified)
* @return the corresponding Field object, or <code>null</code> if not found * @return the corresponding Field object, or <code>null</code> if not found
*/ */
public static Field findField(Class<?> clazz, String name, Class<?> type) { public static Field findField(Class<?> clazz, String name, Class<?> type) {
@ -81,13 +79,11 @@ public abstract class ReflectionUtils {
} }
/** /**
* Set the field represented by the supplied {@link Field field object} on * Set the field represented by the supplied {@link Field field object} on the
* the specified {@link Object target object} to the specified * specified {@link Object target object} to the specified <code>value</code>.
* <code>value</code>. In accordance with {@link Field#set(Object, Object)} * In accordance with {@link Field#set(Object, Object)} semantics, the new value
* semantics, the new value is automatically unwrapped if the underlying * is automatically unwrapped if the underlying field has a primitive type.
* field has a primitive type. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}.
* <p>Thrown exceptions are handled via a call to
* {@link #handleReflectionException(Exception)}.
* @param field the field to set * @param field the field to set
* @param target the target object on which to set the field * @param target the target object on which to set the field
* @param value the value to set; may be <code>null</code> * @param value the value to set; may be <code>null</code>
@ -104,12 +100,11 @@ public abstract class ReflectionUtils {
} }
/** /**
* Get the field represented by the supplied {@link Field field object} on * Get the field represented by the supplied {@link Field field object} on the
* the specified {@link Object target object}. In accordance with * specified {@link Object target object}. In accordance with {@link Field#get(Object)}
* {@link Field#get(Object)} semantics, the returned value is automatically * semantics, the returned value is automatically wrapped if the underlying field
* wrapped if the underlying field has a primitive type. * has a primitive type.
* <p>Thrown exceptions are handled via a call to * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException(Exception)}.
* {@link #handleReflectionException(Exception)}.
* @param field the field to get * @param field the field to get
* @param target the target object from which to get the field * @param target the target object from which to get the field
* @return the field's current value * @return the field's current value
@ -126,9 +121,8 @@ public abstract class ReflectionUtils {
} }
/** /**
* Attempt to find a {@link Method} on the supplied class with the supplied * Attempt to find a {@link Method} on the supplied class with the supplied name
* name and no parameters. Searches all superclasses up to * and no parameters. Searches all superclasses up to <code>Object</code>.
* <code>Object</code>.
* <p>Returns <code>null</code> if no {@link Method} can be found. * <p>Returns <code>null</code> if no {@link Method} can be found.
* @param clazz the class to introspect * @param clazz the class to introspect
* @param name the name of the method * @param name the name of the method
@ -139,14 +133,13 @@ public abstract class ReflectionUtils {
} }
/** /**
* Attempt to find a {@link Method} on the supplied class with the supplied * Attempt to find a {@link Method} on the supplied class with the supplied name
* name and parameter types. Searches all superclasses up to * and parameter types. Searches all superclasses up to <code>Object</code>.
* <code>Object</code>.
* <p>Returns <code>null</code> if no {@link Method} can be found. * <p>Returns <code>null</code> if no {@link Method} can be found.
* @param clazz the class to introspect * @param clazz the class to introspect
* @param name the name of the method * @param name the name of the method
* @param paramTypes the parameter types of the method (may be * @param paramTypes the parameter types of the method
* <code>null</code> to indicate any signature) * (may be <code>null</code> to indicate any signature)
* @return the Method object, or <code>null</code> if none found * @return the Method object, or <code>null</code> if none found
*/ */
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) { public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
@ -167,11 +160,9 @@ public abstract class ReflectionUtils {
} }
/** /**
* Invoke the specified {@link Method} against the supplied target object * Invoke the specified {@link Method} against the supplied target object with no arguments.
* with no arguments. The target object can be <code>null</code> when * The target object can be <code>null</code> when invoking a static {@link Method}.
* invoking a static {@link Method}. * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException}.
* <p>Thrown exceptions are handled via a call to
* {@link #handleReflectionException}.
* @param method the method to invoke * @param method the method to invoke
* @param target the target object to invoke the method on * @param target the target object to invoke the method on
* @return the invocation result, if any * @return the invocation result, if any
@ -182,11 +173,10 @@ public abstract class ReflectionUtils {
} }
/** /**
* Invoke the specified {@link Method} against the supplied target object * Invoke the specified {@link Method} against the supplied target object with the
* with the supplied arguments. The target object can be <code>null</code> * supplied arguments. The target object can be <code>null</code> when invoking a
* when invoking a static {@link Method}. * static {@link Method}.
* <p>Thrown exceptions are handled via a call to * <p>Thrown exceptions are handled via a call to {@link #handleReflectionException}.
* {@link #handleReflectionException}.
* @param method the method to invoke * @param method the method to invoke
* @param target the target object to invoke the method on * @param target the target object to invoke the method on
* @param args the invocation arguments (may be <code>null</code>) * @param args the invocation arguments (may be <code>null</code>)
@ -548,7 +538,6 @@ public abstract class ReflectionUtils {
+ "] must be same or subclass as source class [" + src.getClass().getName() + "]"); + "] must be same or subclass as source class [" + src.getClass().getName() + "]");
} }
doWithFields(src.getClass(), new FieldCallback() { doWithFields(src.getClass(), new FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
makeAccessible(field); makeAccessible(field);
Object srcValue = field.get(src); Object srcValue = field.get(src);

View File

@ -16,12 +16,11 @@
package org.springframework.core.convert; package org.springframework.core.convert;
import java.util.List;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import java.util.List;
import org.junit.Test; import org.junit.Test;
/** /**
@ -53,7 +52,7 @@ public class TypeDescriptorTests {
@Test @Test
public void buildingArrayTypeDescriptors() throws Exception { public void buildingArrayTypeDescriptors() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(new int[0].getClass()); TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(int[].class);
assertTrue(typeDescriptor.isArray()); assertTrue(typeDescriptor.isArray());
assertEquals(Integer.TYPE,typeDescriptor.getElementType()); assertEquals(Integer.TYPE,typeDescriptor.getElementType());
} }