LANG-1190: TypeUtils.isAssignable throws NullPointerException when fromType has type variables and toType generic superclass specifies type variable (closes #158)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 224e034..712fd7e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -46,6 +46,7 @@
<body>
<release version="3.5" date="tba" description="tba">
+ <action issue="LANG-1190" type="fix" dev="pschumacher" due-to="pschumacher">TypeUtils.isAssignable throws NullPointerException when fromType has type variables and toType generic superclass specifies type variable</action>
<action issue="LANG-1226" type="fix" dev="pschumacher" due-to="pschumacher">StringUtils#normalizeSpace does not trim the string anymore</action>
<action issue="LANG-1251" type="fix" dev="pschumacher" due-to="Takuya Ueshin">SerializationUtils.ClassLoaderAwareObjectInputStream should use static initializer to initialize primitiveTypes map</action>
<action issue="LANG-1253" type="add" dev="ggregory" due-to="adilek">[GitHub issue #170] Add RandomUtils#nextBoolean() method</action>
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 6eb286a..30e9940 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java
@@ -464,6 +464,10 @@
final Type toTypeArg = unrollVariableAssignments(var, toTypeVarAssigns);
final Type fromTypeArg = unrollVariableAssignments(var, fromTypeVarAssigns);
+ if (toTypeArg == null && fromTypeArg instanceof Class) {
+ continue;
+ }
+
// parameters must either be absent from the subject type, within
// the bounds of the wildcard type, or be an exact match to the
// parameters of the target type.
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 9b6ca9b..66477ea 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java
@@ -25,6 +25,7 @@
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.net.URI;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@@ -755,6 +756,22 @@
Assert.assertEquals(String.class, TypeUtils.wrap(String.class).getType());
}
+ public static class ClassWithSuperClassWithGenericType extends ArrayList<Object> {
+ private static final long serialVersionUID = 1L;
+
+ public static <U> Iterable<U> methodWithGenericReturnType() {
+ return null;
+ }
+ }
+
+ @Test
+ public void testLANG1190() throws Exception {
+ Type fromType = ClassWithSuperClassWithGenericType.class.getDeclaredMethod("methodWithGenericReturnType").getGenericReturnType();
+ Type failingToType = TypeUtils.wildcardType().withLowerBounds(ClassWithSuperClassWithGenericType.class).build();
+
+ Assert.assertTrue(TypeUtils.isAssignable(fromType, failingToType));
+ }
+
public Iterable<? extends Map<Integer, ? extends Collection<?>>> iterable;
public static <G extends Comparable<G>> G stub() {