This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new d732cf0f8 Preserve equivalent generic upper bounds in TypeUtils (#1866)
d732cf0f8 is described below
commit d732cf0f83e5e8509d053e79779b440963d9bd56
Author: Amar Ghugare <[email protected]>
AuthorDate: Sun Nov 15 20:24:59 2026 +0530
Preserve equivalent generic upper bounds in TypeUtils (#1866)
Use type equality when identifying redundant upper bounds so that equal
but distinct reflection types do not eliminate each other. Retain the
existing reduction of genuinely broader bounds.
Cover JDK and built parameterized types, generic arrays, and wildcard
assignability with regression tests.
---
.../apache/commons/lang3/reflect/TypeUtils.java | 2 +-
.../commons/lang3/reflect/TypeUtilsTest.java | 44 ++++++++++++++++++++++
2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
index 852283424..e9cbcc349 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
@@ -1527,7 +1527,7 @@ public static Type[] normalizeUpperBounds(final Type[]
bounds) {
for (final Type type1 : bounds) {
boolean subtypeFound = false;
for (final Type type2 : bounds) {
- if (type1 != type2 && isAssignable(type2, type1, null)) {
+ if (!equals(type1, type2) && isAssignable(type2, type1, null))
{
subtypeFound = true;
break;
}
diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
index f98012483..7a859186f 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
@@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -579,6 +580,21 @@ static Stream<Arguments>
testIsAssignable_ClassWildcardBounds() {
Arguments.of("unboundedVariable", "superDependentVariable",
true));
}
+ static Stream<Arguments> testNormalizeUpperBoundsEquivalentTypes() throws
NoSuchFieldException {
+ final Type list = TypeUtils.parameterize(List.class, String.class);
+ final Type otherList = TypeUtils.parameterize(List.class,
String.class);
+ final Type jdkList =
HashCodeTypes.class.getDeclaredField("list").getGenericType();
+ final Type array = TypeUtils.genericArrayType(list);
+ final Type jdkArray =
HashCodeTypes.class.getDeclaredField("listArray").getGenericType();
+ return Stream.of(
+ Arguments.of(list, otherList),
+ Arguments.of(jdkList, list),
+ Arguments.of(list, jdkList),
+ Arguments.of(array, TypeUtils.genericArrayType(otherList)),
+ Arguments.of(jdkArray, array),
+ Arguments.of(array, jdkArray));
+ }
+
static Stream<Type> testTypeToString() {
// @formatter:off
return Stream.of(Comparator.class, Comparable.class, ArrayList.class,
HashMap.class)
@@ -1764,6 +1780,17 @@ void testIsAssignableWildcardWithMultipleUpperBounds() {
assertFalse(TypeUtils.isAssignable(targetCloneable, subject));
}
+ @Test
+ void testIsAssignableWildcardWithEquivalentUpperBounds() {
+ final Type first = TypeUtils.parameterize(List.class, String.class);
+ final Type second = TypeUtils.parameterize(List.class, String.class);
+ final WildcardType wildcard =
TypeUtils.wildcardType().withUpperBounds(first, second).build();
+ assertFalse(TypeUtils.isAssignable(String.class, wildcard));
+
assertFalse(TypeUtils.isAssignable(TypeUtils.parameterize(ArrayList.class,
Integer.class), wildcard));
+
assertTrue(TypeUtils.isAssignable(TypeUtils.parameterize(ArrayList.class,
String.class), wildcard));
+ assertArrayEquals(new Type[] { first },
TypeUtils.getImplicitUpperBounds(wildcard));
+ }
+
@SuppressWarnings("boxing") // boxing is deliberate here
@Test
void testIsInstance() throws NoSuchFieldException {
@@ -1815,6 +1842,23 @@ void testNormalizeUpperBounds_LANG_820() {
assertArrayEquals(expectedArray,
TypeUtils.normalizeUpperBounds(typeArray));
}
+ @Test
+ void testNormalizeUpperBoundsEquivalentSupertypes() {
+ final Type first = TypeUtils.parameterize(Collection.class,
String.class);
+ final Type second = TypeUtils.parameterize(Collection.class,
String.class);
+ final Type subtype = TypeUtils.parameterize(List.class, String.class);
+ assertArrayEquals(new Type[] { subtype },
TypeUtils.normalizeUpperBounds(new Type[] { first, second, subtype }));
+ }
+
+ @ParameterizedTest
+ @MethodSource
+ void testNormalizeUpperBoundsEquivalentTypes(final Type first, final Type
second) {
+ assertNotSame(first, second);
+ assertEquals(first, second);
+ assertArrayEquals(new Type[] { first },
TypeUtils.normalizeUpperBounds(new Type[] { first, second }));
+ assertArrayEquals(new Type[] { first },
TypeUtils.normalizeUpperBounds(new Type[] { Object.class, first, second }));
+ }
+
@Test
void testParameterizedOwnerWithNonGenericInnerClassToString() throws
NoSuchMethodException {
final ParameterizedType owner =
TypeUtils.parameterize(ParameterizedOwner.class, String.class);