Allow TypeDescriptor create with null generics

Restore the ability to create a TypeDescriptor for a collection or
map where the generics may be null.

Issue: SPR-11006
This commit is contained in:
Phillip Webb 2013-10-18 10:56:04 -07:00
parent cfb66252eb
commit 79048e18be
2 changed files with 20 additions and 4 deletions

View File

@ -498,12 +498,13 @@ public class TypeDescriptor implements Serializable {
*/ */
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) { public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) {
Assert.notNull(collectionType, "CollectionType must not be null"); Assert.notNull(collectionType, "CollectionType must not be null");
Assert.notNull(elementTypeDescriptor, "ElementTypeDesciptor must not be null");
if (!Collection.class.isAssignableFrom(collectionType)) { if (!Collection.class.isAssignableFrom(collectionType)) {
throw new IllegalArgumentException("collectionType must be a java.util.Collection"); throw new IllegalArgumentException("collectionType must be a java.util.Collection");
} }
ResolvableType element = (elementTypeDescriptor == null ? null
: elementTypeDescriptor.resolvableType);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType, return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType,
elementTypeDescriptor.resolvableType), null, null); element), null, null);
} }
/** /**
@ -520,8 +521,9 @@ public class TypeDescriptor implements Serializable {
if (!Map.class.isAssignableFrom(mapType)) { if (!Map.class.isAssignableFrom(mapType)) {
throw new IllegalArgumentException("mapType must be a java.util.Map"); throw new IllegalArgumentException("mapType must be a java.util.Map");
} }
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, ResolvableType key = (keyTypeDescriptor == null ? null : keyTypeDescriptor.resolvableType);
keyTypeDescriptor.resolvableType, valueTypeDescriptor.resolvableType), null, null); ResolvableType value = (valueTypeDescriptor == null ? null : valueTypeDescriptor.resolvableType);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
} }
/** /**

View File

@ -918,4 +918,18 @@ public class TypeDescriptorTests {
TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject(); TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject();
assertThat(readObject, equalTo(typeDescriptor)); assertThat(readObject, equalTo(typeDescriptor));
} }
@Test
public void createCollectionWithNullElement() throws Exception {
TypeDescriptor typeDescriptor = TypeDescriptor.collection(List.class, null);
assertThat(typeDescriptor.getElementTypeDescriptor(), nullValue());
}
@Test
public void createMapWithNullElements() throws Exception {
TypeDescriptor typeDescriptor = TypeDescriptor.map(LinkedHashMap.class, null, null);
assertThat(typeDescriptor.getMapKeyTypeDescriptor(), nullValue());
assertThat(typeDescriptor.getMapValueTypeDescriptor(), nullValue());
}
} }