Improved fix for SPR-6850 by dealing with bounds separately from normal types

This commit is contained in:
Ramnivas Laddad 2010-04-01 23:37:08 +00:00
parent 7f90e3bcf7
commit 66d2c6698b
2 changed files with 20 additions and 14 deletions

View File

@ -42,13 +42,8 @@ public abstract class TypeUtils {
* @return true if rhs is assignable to lhs * @return true if rhs is assignable to lhs
*/ */
public static boolean isAssignable(Type lhsType, Type rhsType) { public static boolean isAssignable(Type lhsType, Type rhsType) {
if (rhsType == null) { Assert.notNull(lhsType, "Left-hand side type must not be null");
return true; Assert.notNull(rhsType, "Right-hand side type must not be null");
}
if (lhsType == null) {
return false;
}
// all types are assignable to themselves and to class Object // all types are assignable to themselves and to class Object
if (lhsType.equals(rhsType) || lhsType.equals(Object.class)) { if (lhsType.equals(rhsType) || lhsType.equals(Object.class)) {
@ -175,13 +170,13 @@ public abstract class TypeUtils {
for (Type lBound : lUpperBounds) { for (Type lBound : lUpperBounds) {
for (Type rBound : rUpperBounds) { for (Type rBound : rUpperBounds) {
if (!isAssignable(lBound, rBound)) { if (!isAssignableBound(lBound, rBound)) {
return false; return false;
} }
} }
for (Type rBound : rLowerBounds) { for (Type rBound : rLowerBounds) {
if (!isAssignable(lBound, rBound)) { if (!isAssignableBound(lBound, rBound)) {
return false; return false;
} }
} }
@ -189,13 +184,13 @@ public abstract class TypeUtils {
for (Type lBound : lLowerBounds) { for (Type lBound : lLowerBounds) {
for (Type rBound : rUpperBounds) { for (Type rBound : rUpperBounds) {
if (!isAssignable(rBound, lBound)) { if (!isAssignableBound(rBound, lBound)) {
return false; return false;
} }
} }
for (Type rBound : rLowerBounds) { for (Type rBound : rLowerBounds) {
if (!isAssignable(rBound, lBound)) { if (!isAssignableBound(rBound, lBound)) {
return false; return false;
} }
} }
@ -203,13 +198,13 @@ public abstract class TypeUtils {
} }
else { else {
for (Type lBound : lUpperBounds) { for (Type lBound : lUpperBounds) {
if (!isAssignable(lBound, rhsType)) { if (!isAssignableBound(lBound, rhsType)) {
return false; return false;
} }
} }
for (Type lBound : lLowerBounds) { for (Type lBound : lLowerBounds) {
if (!isAssignable(rhsType, lBound)) { if (!isAssignableBound(rhsType, lBound)) {
return false; return false;
} }
} }
@ -217,4 +212,16 @@ public abstract class TypeUtils {
return true; return true;
} }
public static boolean isAssignableBound(Type lhsType, Type rhsType) {
if (rhsType == null) {
return true;
}
if (lhsType == null) {
return false;
}
return isAssignable(lhsType, rhsType);
}
} }

View File

@ -66,7 +66,6 @@ public class TypeUtilsTests {
assertTrue(TypeUtils.isAssignable(List.class, LinkedList.class)); assertTrue(TypeUtils.isAssignable(List.class, LinkedList.class));
assertFalse(TypeUtils.isAssignable(List.class, Collection.class)); assertFalse(TypeUtils.isAssignable(List.class, Collection.class));
assertFalse(TypeUtils.isAssignable(List.class, HashSet.class)); assertFalse(TypeUtils.isAssignable(List.class, HashSet.class));
assertFalse(TypeUtils.isAssignable(null, Object.class));
} }
@Test @Test