Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeUtils.isAssignable should return true for ParametrizedType assignable to Object [SPR-5390] #10063

Closed
spring-projects-issues opened this issue Jan 4, 2009 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Vincent De Rijcke opened SPR-5390 and commented

isAssignable( Object.class, /* parametrizedtype or genericarraytype */ ) should be true

Test case:
public class SpringTypeUtilsTest {
private static final List<Long> LIST_LONG = null;
private static final List<Long>[] LIST_LONG_ARRAY = null;

private Type getFieldType(String fieldName) {
    try {
        return SpringTypeUtilsTest.class.getDeclaredField(fieldName).getGenericType();
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Field does not exist", e);
    }
}

@Test
public void testParametrizedTypeAssignableToObject() {
    Object o = (List<Long>) null;  // fine in java - so assignable should return true 
    
    assertTrue(TypeUtils.isAssignable(Object.class, getFieldType("LIST_LONG")));
}

@Test
public void testGenericArrayTypeAssignableToObject() {
    // Note that java use a GenericArrayType for an Array used as a Wildcard boundaries or ParametrizedTypeArgument,
   // instead of using the Array class object
    Object o = (List<Long>[]) null;  // fine in java - so assignable should return true 
    
    assertTrue(TypeUtils.isAssignable(Object.class, getFieldType("LIST_LONG_ARRAY")));
}

}

Proposed fix of TypeUtils.java:

//UPDATE isAssignable(Type lhsType, Type rhsType)
public static boolean isAssignable(Type lhsType, Type rhsType) {
if (lhsType == null) {
throw new IllegalArgumentException("Left-hand side type must not be null");
}
if (rhsType == null) {
throw new IllegalArgumentException("Right-hand side type must not be null");
}

if (lhsType.equals(rhsType)) {
    return true;
}
if (lhsType instanceof Class && rhsType instanceof Class) {
    return isAssignable((Class<?>) lhsType, (Class<?>) rhsType);
}

// ADDED
if (lhsType == Object.class && ( rhsType instanceof ParameterizedType || rhsType instanceof GenericArrayType)) {
return true;
}
// END ADDED
if (lhsType instanceof ParameterizedType && rhsType instanceof ParameterizedType) {
return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
}
if (lhsType instanceof WildcardType) {
return isAssignable((WildcardType) lhsType, rhsType);
}
return false;
}


Affects: 2.5.6, 3.0 M1

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Thanks for the suggestion! I've simply added a check that accepts any right-hand side if the left-hand side is Object.class... since you can effectively assign anything then.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Actually, the more sophisticated form is to compare raw types in that case - so that it works not just for Object.class but e.g. for List.class on the left-hand side as well. I'll revise this accordingly.

Juergen

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 M2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants