revised "ClassUtils.isAssignable" semantics to cover primitives vs wrappers in both directions (SPR-7610)

This commit is contained in:
Juergen Hoeller 2010-10-01 21:50:25 +00:00
parent 9008e08171
commit d9b54a524d
2 changed files with 32 additions and 3 deletions

View File

@ -809,15 +809,29 @@ public abstract class ClassUtils {
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
Assert.notNull(lhsType, "Left-hand side type must not be null");
Assert.notNull(rhsType, "Right-hand side type must not be null");
return (lhsType.isAssignableFrom(rhsType) ||
lhsType.equals(primitiveWrapperTypeMap.get(rhsType)));
if (lhsType.isAssignableFrom(rhsType)) {
return true;
}
if (lhsType.isPrimitive()) {
Class resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) {
return true;
}
}
else {
Class resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
return true;
}
}
return false;
}
/**
* Determine if the given type is assignable from the given value,
* assuming setting by reflection. Considers primitive wrapper classes
* as assignable to the corresponding primitive types.
* @param type the target type
* @param type the target type
* @param value the value that should be assigned to the type
* @return if the type is assignable from the value
*/

View File

@ -227,6 +227,21 @@ public class ClassUtilsTests extends TestCase {
InnerClass.overloadedCalled);
}
public void testIsAssignable() {
assertTrue(ClassUtils.isAssignable(Object.class, Object.class));
assertTrue(ClassUtils.isAssignable(String.class, String.class));
assertTrue(ClassUtils.isAssignable(Object.class, String.class));
assertTrue(ClassUtils.isAssignable(Object.class, Integer.class));
assertTrue(ClassUtils.isAssignable(Number.class, Integer.class));
assertTrue(ClassUtils.isAssignable(Number.class, int.class));
assertTrue(ClassUtils.isAssignable(Integer.class, int.class));
assertTrue(ClassUtils.isAssignable(int.class, Integer.class));
assertFalse(ClassUtils.isAssignable(String.class, Object.class));
assertFalse(ClassUtils.isAssignable(Integer.class, Number.class));
assertFalse(ClassUtils.isAssignable(Integer.class, double.class));
assertFalse(ClassUtils.isAssignable(double.class, Integer.class));
}
public void testClassPackageAsResourcePath() {
String result = ClassUtils.classPackageAsResourcePath(Proxy.class);
assertTrue(result.equals("java/lang/reflect"));