Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeDescriptor.forElementType() does not work for TypeDescriptor obtained via TypeDescriptor.valueOf() [SPR-7390] #12048

Closed
spring-projects-issues opened this issue Jul 22, 2010 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 22, 2010

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:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I'm afraid this is not the intent behind forElementType at the moment: It is rather made to return a TypeDescriptor that exposes the element type as its primary type, preserving metadata such as annotations as declared on the field/method. So the behavior that you're seeing is exactly what you are supposed to get.

Constructing a TypeDescriptor for a collection plus element type is nevertheless a valid use case, in particular for testing. We might be able to add a corresponding factory method quite easily.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 12, 2012

Jon Kranes commented

This has been resolved by #12253

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants