From b1ed6725807382608ed45d9803f80b129a79c5a8 Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Sun, 29 May 2016 20:30:36 +0200 Subject: [PATCH] LANG-1190: TypeUtils.isAssignable throws NullPointerException when fromType has type variables and toType generic superclass specifies type variable --- .../apache/commons/lang3/reflect/TypeUtils.java | 4 ++++ .../commons/lang3/reflect/TypeUtilsTest.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) 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 6eb286ab93c..30e9940ba22 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 @@ private static boolean isAssignable(final Type type, final ParameterizedType toP 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 9b6ca9b1d57..66477ea20ba 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 @@ public void testWrap() { Assert.assertEquals(String.class, TypeUtils.wrap(String.class).getType()); } + public static class ClassWithSuperClassWithGenericType extends ArrayList { + private static final long serialVersionUID = 1L; + + public static Iterable 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>> iterable; public static > G stub() {