extra factory method and type descriptor for NULL defined

This commit is contained in:
Andy Clement 2009-04-03 23:38:18 +00:00
parent 5a0522e203
commit bdecf6720e
1 changed files with 26 additions and 1 deletions

View File

@ -29,9 +29,17 @@ import org.springframework.util.Assert;
* Type metadata about a bindable target value.
*
* @author Keith Donald
* @author Andy Clement
*
* @since 3.0
*/
public class TypeDescriptor {
/**
* constant value typeDescriptor for the type of a null value
*/
public final static TypeDescriptor NULL_TYPE_DESCRIPTOR = new TypeDescriptor((Class<?>)null);
private MethodParameter methodParameter;
private Field field;
@ -79,8 +87,10 @@ public class TypeDescriptor {
return type;
} else if (field != null) {
return field.getType();
} else {
} else if (methodParameter!=null) {
return methodParameter.getParameterType();
} else {
return null;
}
}
@ -243,9 +253,24 @@ public class TypeDescriptor {
* @return the type descriptor
*/
public static TypeDescriptor valueOf(Class<? extends Object> type) {
// TODO needs a cache for common type descriptors
return 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) {
if (object==null) {
return NULL_TYPE_DESCRIPTOR;
} else {
return valueOf(object.getClass());
}
}
// internal helpers
private Class<?> getArrayComponentType() {