From af54ee9eba4e180a8cd59abf410edfd6c91b074f Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Tue, 4 Apr 2023 16:10:18 +0200 Subject: [PATCH 1/2] Polish ClassUtils and TypeUtils Closes gh-30282 --- .../org/springframework/util/ClassUtils.java | 4 +- .../org/springframework/util/TypeUtils.java | 52 ++++++++++--------- 2 files changed, 29 insertions(+), 27 deletions(-) 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..7464379e3e9 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -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) */ 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..d88dc2f9bd9 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,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(); - - 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 { 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; From 2d1ddbb65c8a7d76904e670b952ae78a349c31b8 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 20 Apr 2023 16:18:08 +0200 Subject: [PATCH 2/2] Polish contribution See gh-30282 --- .../main/java/org/springframework/util/ClassUtils.java | 8 +++++--- .../src/main/java/org/springframework/util/TypeUtils.java | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) 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 7464379e3e9..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 @@ -532,8 +533,9 @@ public abstract class ClassUtils { * type, assuming setting by reflection. Considers primitive wrapper * classes as assignable to the corresponding primitive types. * @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 + * @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 d88dc2f9bd9..6f0964d7b1a 100644 --- a/spring-core/src/main/java/org/springframework/util/TypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/TypeUtils.java @@ -40,8 +40,10 @@ 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 (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 + * @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");