parent
7a78092934
commit
af54ee9eba
|
|
@ -531,8 +531,8 @@ public abstract class ClassUtils {
|
|||
* Check if the right-hand side type may be assigned to the left-hand side
|
||||
* type, assuming setting by reflection. Considers primitive wrapper
|
||||
* classes as assignable to the corresponding primitive types.
|
||||
* @param lhsType the target type
|
||||
* @param rhsType the value type that should be assigned to the target type
|
||||
* @param lhsType the target type (left-hand side (LHS) type)
|
||||
* @param rhsType the value type (right-hand side (RHS) type) that should be assigned to the target type
|
||||
* @return if the target type is assignable from the value type
|
||||
* @see TypeUtils#isAssignable(java.lang.reflect.Type, java.lang.reflect.Type)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
|
|
@ -39,8 +39,8 @@ 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 lhsType the target type (left-hand side (LHS) type)
|
||||
* @param rhsType the value type (right-hand side (RHS) 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) {
|
||||
|
|
@ -133,35 +133,17 @@ public abstract class TypeUtils {
|
|||
}
|
||||
|
||||
private static boolean isAssignable(WildcardType lhsType, Type rhsType) {
|
||||
Type[] lUpperBounds = lhsType.getUpperBounds();
|
||||
Type[] lUpperBounds = getUpperBounds(lhsType);
|
||||
|
||||
// supply the implicit upper bound if none are specified
|
||||
if (lUpperBounds.length == 0) {
|
||||
lUpperBounds = new Type[] { Object.class };
|
||||
}
|
||||
|
||||
Type[] lLowerBounds = lhsType.getLowerBounds();
|
||||
|
||||
// supply the implicit lower bound if none are specified
|
||||
if (lLowerBounds.length == 0) {
|
||||
lLowerBounds = new Type[] { null };
|
||||
}
|
||||
Type[] lLowerBounds = getLowerBounds(lhsType);
|
||||
|
||||
if (rhsType instanceof WildcardType rhsWcType) {
|
||||
// both the upper and lower bounds of the right-hand side must be
|
||||
// completely enclosed in the upper and lower bounds of the left-
|
||||
// hand side.
|
||||
Type[] rUpperBounds = rhsWcType.getUpperBounds();
|
||||
Type[] rUpperBounds = getUpperBounds(rhsWcType);
|
||||
|
||||
if (rUpperBounds.length == 0) {
|
||||
rUpperBounds = new Type[] { Object.class };
|
||||
}
|
||||
|
||||
Type[] rLowerBounds = rhsWcType.getLowerBounds();
|
||||
|
||||
if (rLowerBounds.length == 0) {
|
||||
rLowerBounds = new Type[] { null };
|
||||
}
|
||||
Type[] rLowerBounds = getLowerBounds(rhsWcType);
|
||||
|
||||
for (Type lBound : lUpperBounds) {
|
||||
for (Type rBound : rUpperBounds) {
|
||||
|
|
@ -208,6 +190,26 @@ public abstract class TypeUtils {
|
|||
return true;
|
||||
}
|
||||
|
||||
private static Type[] getLowerBounds(WildcardType wildcardType) {
|
||||
Type[] lowerBounds = wildcardType.getLowerBounds();
|
||||
|
||||
// supply the implicit lower bound if none are specified
|
||||
if (lowerBounds.length == 0) {
|
||||
lowerBounds = new Type[] { null };
|
||||
}
|
||||
return lowerBounds;
|
||||
}
|
||||
|
||||
private static Type[] getUpperBounds(WildcardType wildcardType) {
|
||||
Type[] upperBounds = wildcardType.getUpperBounds();
|
||||
|
||||
// supply the implicit upper bound if none are specified
|
||||
if (upperBounds.length == 0) {
|
||||
upperBounds = new Type[] { Object.class };
|
||||
}
|
||||
return upperBounds;
|
||||
}
|
||||
|
||||
public static boolean isAssignableBound(@Nullable Type lhsType, @Nullable Type rhsType) {
|
||||
if (rhsType == null) {
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Reference in New Issue