diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index ae4ab61837a..beba82d1702 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -43,7 +43,8 @@ import org.springframework.lang.Nullable; /** * Miscellaneous {@code java.lang.Class} utility methods. - * Mainly for internal use within the framework. + * + *

Mainly for internal use within the framework. * * @author Juergen Hoeller * @author Keith Donald @@ -531,9 +532,10 @@ 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 - * @return if the target type is assignable from the value 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 {@code true} if {@code rhsType} is assignable to {@code lhsType} * @see TypeUtils#isAssignable(java.lang.reflect.Type, java.lang.reflect.Type) */ public static boolean isAssignable(Class lhsType, Class rhsType) { diff --git a/spring-core/src/main/java/org/springframework/util/TypeUtils.java b/spring-core/src/main/java/org/springframework/util/TypeUtils.java index 5d5070f336a..6f0964d7b1a 100644 --- a/spring-core/src/main/java/org/springframework/util/TypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/TypeUtils.java @@ -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,9 +39,11 @@ 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 - * @return true if rhs is assignable to lhs + * @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 {@code true} if {@code rhsType} is assignable to {@code lhsType} + * @see ClassUtils#isAssignable(Class, Class) */ public static boolean isAssignable(Type lhsType, Type rhsType) { Assert.notNull(lhsType, "Left-hand side type must not be null"); @@ -133,35 +135,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 +192,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;