diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b9930b0d2..26d7be48c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -122,6 +122,7 @@ The type attribute can be add,update,fix,remove. [StepSecurity] ci: Harden GitHub Actions #1067. Update Javadoc for the insert methods in ArrayUtils #1078. Deprecate ExceptionUtils.ExceptionUtils(). + TypeUtils.getRawType() throws a NullPointerException on Wildcard GenericArrayType. Add GitHub coverage.yml. Add EnumUtils.getEnumSystemProperty(...). 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 09e65b749..f959d7a99 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java @@ -802,7 +802,7 @@ public class TypeUtils { .getGenericComponentType(), assigningType); // create array type from raw component type and return its class - return Array.newInstance(rawComponentType, 0).getClass(); + return rawComponentType != null ? Array.newInstance(rawComponentType, 0).getClass() : null; } // (hand-waving) this is not the method you're looking for 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 67de3b19e..f5407d722 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -346,27 +346,33 @@ public class TypeUtilsTest extends AbstractLangTest { @Test public void testGetRawType() throws SecurityException, NoSuchFieldException { - final Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent") - .getGenericType(); - final Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent") - .getGenericType(); + final Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent").getGenericType(); + final Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent").getGenericType(); final Type foosFieldType = GenericTypeHolder.class.getDeclaredField("foos").getGenericType(); final Type genericParentT = GenericParent.class.getTypeParameters()[0]; assertEquals(GenericParent.class, TypeUtils.getRawType(stringParentFieldType, null)); - assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType, - null)); + assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType, null)); assertEquals(List.class, TypeUtils.getRawType(foosFieldType, null)); - assertEquals(String.class, TypeUtils.getRawType(genericParentT, - StringParameterizedChild.class)); - assertEquals(String.class, TypeUtils.getRawType(genericParentT, - stringParentFieldType)); - assertEquals(Foo.class, TypeUtils.getRawType(Iterable.class.getTypeParameters()[0], - foosFieldType)); - assertEquals(Foo.class, TypeUtils.getRawType(List.class.getTypeParameters()[0], - foosFieldType)); + assertEquals(String.class, TypeUtils.getRawType(genericParentT, StringParameterizedChild.class)); + assertEquals(String.class, TypeUtils.getRawType(genericParentT, stringParentFieldType)); + assertEquals(Foo.class, TypeUtils.getRawType(Iterable.class.getTypeParameters()[0], foosFieldType)); + assertEquals(Foo.class, TypeUtils.getRawType(List.class.getTypeParameters()[0], foosFieldType)); assertNull(TypeUtils.getRawType(genericParentT, GenericParent.class)); - assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class - .getDeclaredField("barParents").getGenericType(), null)); + assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class.getDeclaredField("barParents").getGenericType(), null)); + } + + /** + * Tests https://issues.apache.org/jira/browse/LANG-1697 + */ + @Test + public void testGetRawType_LANG_1697() throws NoSuchFieldException { + assertEquals(int[].class, TypeUtils.getRawType(TypeUtils.genericArrayType(Integer.TYPE), Integer.TYPE)); + // LANG-1697: + assertNull(TypeUtils.getRawType(TypeUtils.genericArrayType(TypeUtils.WILDCARD_ALL), null)); + // TODO: Is this correct? + assertNull(TypeUtils.getRawType(TypeUtils.genericArrayType(TypeUtils.WILDCARD_ALL), TypeUtils.WILDCARD_ALL)); + // TODO: Is this correct? + assertNull(TypeUtils.getRawType(TypeUtils.genericArrayType(TypeUtils.WILDCARD_ALL), Integer.TYPE)); } @Test