-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Jon Kranes opened SPR-7390 and commented
Based on JavaDoc for TypeDescriptor.forElementType(), I expect the expression:
TypeDescriptor.valueOf(java.util.List.class).forElementType(String.class)
should produce a TypeDescriptor for parameterized List of type String, i.e. java.util.List<String>. However, the implementation for a TypeDescriptor that was not originally constructed from a Field or MethodParameter simply does:
return TypeDescriptor.valueOf(elementType);
The expected behavior is very useful as there appears to be no other way to programmatically construct a TypeDescriptor for a parameterized collection, without having reference to a Field or MethodParameter object. The workaround is to create or refer to a field or method in some arbitrary class in order to use the Field or MethodParameter constructors, but this is awkward and would be unnecessary if the forElementType worked as expected.
Simple unit test:
@Test
public void testForElementType() {
TypeDescriptor td = TypeDescriptor.valueOf(java.util.List.class).forElementType(String.class);
// Fails. getType() returns java.util.String
assertEquals(java.util.List.class, td.getType());
//Fails. getElementType() returns null
assertEquals(String.class, td.getElementType());
}
Affects: 3.0.3
Issue Links:
- Construct org.springframework.core.convert.TypeDescriptor programatically for collections [SPR-7597] #12253 Construct org.springframework.core.convert.TypeDescriptor programatically for collections