TypeUtils detects variations of ParameterizedType/GenericArrayType assignability as well
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.WildcardType;
|
||||
@@ -34,20 +35,54 @@ public abstract class TypeUtils {
|
||||
* Check if the right-hand side type may be assigned to the left-hand side
|
||||
* type following the Java generics rules.
|
||||
* @param lhsType the target type
|
||||
* @param rhsType the value type that should be assigned to the target type
|
||||
* @param rhsType the value type that should be assigned to the target type
|
||||
* @return true if rhs is assignable to lhs
|
||||
*/
|
||||
public static boolean isAssignable(Type lhsType, Type rhsType) {
|
||||
Assert.notNull(lhsType, "Left-hand side type must not be null");
|
||||
Assert.notNull(rhsType, "Right-hand side type must not be null");
|
||||
if (lhsType.equals(rhsType)) {
|
||||
if (lhsType.equals(rhsType) || lhsType.equals(Object.class)) {
|
||||
return true;
|
||||
}
|
||||
if (lhsType instanceof Class && rhsType instanceof Class) {
|
||||
return ClassUtils.isAssignable((Class) lhsType, (Class) rhsType);
|
||||
if (lhsType instanceof Class) {
|
||||
Class lhsClass = (Class) lhsType;
|
||||
if (rhsType instanceof Class) {
|
||||
return ClassUtils.isAssignable(lhsClass, (Class) rhsType);
|
||||
}
|
||||
else if (rhsType instanceof ParameterizedType){
|
||||
Type rhsRaw = ((ParameterizedType) rhsType).getRawType();
|
||||
if (rhsRaw instanceof Class) {
|
||||
return ClassUtils.isAssignable(lhsClass, (Class) rhsRaw);
|
||||
}
|
||||
}
|
||||
else if (lhsClass.isArray() && rhsType instanceof GenericArrayType){
|
||||
Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
|
||||
return isAssignable(lhsClass.getComponentType(), rhsComponent);
|
||||
}
|
||||
}
|
||||
if (lhsType instanceof ParameterizedType && rhsType instanceof ParameterizedType) {
|
||||
return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
|
||||
if (lhsType instanceof ParameterizedType) {
|
||||
if (rhsType instanceof Class) {
|
||||
Type lhsRaw = ((ParameterizedType) lhsType).getRawType();
|
||||
if (lhsRaw instanceof Class) {
|
||||
return ClassUtils.isAssignable((Class) lhsRaw, (Class) rhsType);
|
||||
}
|
||||
}
|
||||
else if (rhsType instanceof ParameterizedType) {
|
||||
return isAssignable((ParameterizedType) lhsType, (ParameterizedType) rhsType);
|
||||
}
|
||||
}
|
||||
if (lhsType instanceof GenericArrayType) {
|
||||
Type lhsComponent = ((GenericArrayType) lhsType).getGenericComponentType();
|
||||
if (rhsType instanceof Class) {
|
||||
Class rhsClass = (Class) rhsType;
|
||||
if (rhsClass.isArray()) {
|
||||
return isAssignable(lhsComponent, rhsClass.getComponentType());
|
||||
}
|
||||
}
|
||||
else if (rhsType instanceof GenericArrayType) {
|
||||
Type rhsComponent = ((GenericArrayType) rhsType).getGenericComponentType();
|
||||
return isAssignable(lhsComponent, rhsComponent);
|
||||
}
|
||||
}
|
||||
if (lhsType instanceof WildcardType) {
|
||||
return isAssignable((WildcardType) lhsType, rhsType);
|
||||
|
||||
Reference in New Issue
Block a user