[LANG-1697] TypeUtils.getRawType() throws a NullPointerException on

Wildcard GenericArrayType
This commit is contained in:
Gary Gregory 2023-07-11 17:21:11 -04:00
parent 844ac9001b
commit 69c4dddbde
3 changed files with 24 additions and 17 deletions

View File

@ -122,6 +122,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="step-security-bot, Gary Gregory">[StepSecurity] ci: Harden GitHub Actions #1067.</action> <action type="fix" dev="ggregory" due-to="step-security-bot, Gary Gregory">[StepSecurity] ci: Harden GitHub Actions #1067.</action>
<action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou">Update Javadoc for the insert methods in ArrayUtils #1078.</action> <action type="fix" dev="ggregory" due-to="Dimitrios Efthymiou">Update Javadoc for the insert methods in ArrayUtils #1078.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate ExceptionUtils.ExceptionUtils().</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate ExceptionUtils.ExceptionUtils().</action>
<action issue="LANG-1697" type="fix" dev="ggregory" due-to="Jan Arne Sparka, Gary Gregory">TypeUtils.getRawType() throws a NullPointerException on Wildcard GenericArrayType.</action>
<!-- ADD --> <!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GitHub coverage.yml.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GitHub coverage.yml.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>

View File

@ -802,7 +802,7 @@ public class TypeUtils {
.getGenericComponentType(), assigningType); .getGenericComponentType(), assigningType);
// create array type from raw component type and return its class // 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 // (hand-waving) this is not the method you're looking for

View File

@ -346,27 +346,33 @@ public class TypeUtilsTest<B> extends AbstractLangTest {
@Test @Test
public void testGetRawType() throws SecurityException, NoSuchFieldException { public void testGetRawType() throws SecurityException, NoSuchFieldException {
final Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent") final Type stringParentFieldType = GenericTypeHolder.class.getDeclaredField("stringParent").getGenericType();
.getGenericType(); final Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent").getGenericType();
final Type integerParentFieldType = GenericTypeHolder.class.getDeclaredField("integerParent")
.getGenericType();
final Type foosFieldType = GenericTypeHolder.class.getDeclaredField("foos").getGenericType(); final Type foosFieldType = GenericTypeHolder.class.getDeclaredField("foos").getGenericType();
final Type genericParentT = GenericParent.class.getTypeParameters()[0]; final Type genericParentT = GenericParent.class.getTypeParameters()[0];
assertEquals(GenericParent.class, TypeUtils.getRawType(stringParentFieldType, null)); assertEquals(GenericParent.class, TypeUtils.getRawType(stringParentFieldType, null));
assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType, assertEquals(GenericParent.class, TypeUtils.getRawType(integerParentFieldType, null));
null));
assertEquals(List.class, TypeUtils.getRawType(foosFieldType, null)); assertEquals(List.class, TypeUtils.getRawType(foosFieldType, null));
assertEquals(String.class, TypeUtils.getRawType(genericParentT, assertEquals(String.class, TypeUtils.getRawType(genericParentT, StringParameterizedChild.class));
StringParameterizedChild.class)); assertEquals(String.class, TypeUtils.getRawType(genericParentT, stringParentFieldType));
assertEquals(String.class, TypeUtils.getRawType(genericParentT, assertEquals(Foo.class, TypeUtils.getRawType(Iterable.class.getTypeParameters()[0], foosFieldType));
stringParentFieldType)); assertEquals(Foo.class, TypeUtils.getRawType(List.class.getTypeParameters()[0], foosFieldType));
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)); assertNull(TypeUtils.getRawType(genericParentT, GenericParent.class));
assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class assertEquals(GenericParent[].class, TypeUtils.getRawType(GenericTypeHolder.class.getDeclaredField("barParents").getGenericType(), null));
.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 @Test