Browse Source

Polish ClassUtils and TypeUtils

Closes gh-30282
pull/30420/head
David Szigecsan 2 years ago committed by Sam Brannen
parent
commit
af54ee9eba
  1. 4
      spring-core/src/main/java/org/springframework/util/ClassUtils.java
  2. 52
      spring-core/src/main/java/org/springframework/util/TypeUtils.java

4
spring-core/src/main/java/org/springframework/util/ClassUtils.java

@ -531,8 +531,8 @@ public abstract class ClassUtils { @@ -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)
*/

52
spring-core/src/main/java/org/springframework/util/TypeUtils.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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 { @@ -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();
if (rUpperBounds.length == 0) {
rUpperBounds = new Type[] { Object.class };
}
Type[] rUpperBounds = getUpperBounds(rhsWcType);
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 { @@ -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…
Cancel
Save