提交 79048e18 编写于 作者: P Phillip Webb

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
上级 cfb66252
......@@ -498,12 +498,13 @@ public class TypeDescriptor implements Serializable {
*/
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) {
Assert.notNull(collectionType, "CollectionType must not be null");
Assert.notNull(elementTypeDescriptor, "ElementTypeDesciptor must not be null");
if (!Collection.class.isAssignableFrom(collectionType)) {
throw new IllegalArgumentException("collectionType must be a java.util.Collection");
}
ResolvableType element = (elementTypeDescriptor == null ? null
: elementTypeDescriptor.resolvableType);
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)) {
throw new IllegalArgumentException("mapType must be a java.util.Map");
}
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType,
keyTypeDescriptor.resolvableType, valueTypeDescriptor.resolvableType), null, null);
ResolvableType key = (keyTypeDescriptor == null ? null : keyTypeDescriptor.resolvableType);
ResolvableType value = (valueTypeDescriptor == null ? null : valueTypeDescriptor.resolvableType);
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
}
/**
......
......@@ -918,4 +918,18 @@ public class TypeDescriptorTests {
TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject();
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());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册