Skip to content

ResolvableType.resolveType() functionality is useful but package-private #36103

@sparecycles

Description

@sparecycles

Hi Spring team!

I've come across a snag/deficiency with ResolvableType and resolving type variables and I do have a workaround, but I'm sharing anyway:


Suppose I have a class with a generic (in a library)

class Thing<T> { }

and I expect this to be subclassed with a generic

 static class Thingy<T> extends Thing<T> {};

and then given a concrete type in usage

interface MyService {
  Thingy<List<String>> method();
}

The library wants to find the generic in terms of Thing on the method's return value, so I tried

thingGeneric = ( returnType = ResolvableType.forType(method.getGenericReturnType()) ).as(Thing.class).getGeneric(0);

which does represent List<String> (according to the .toString() on the result), but the "raw class" from that expression is null and the getType() result is useless TypeVariable without context. Meanwhile returnType.resolveGeneric(0) correctly returns List.class but that loses the <String> generic.

In the debugger, thingGeneric.resolveType().getType() produces the List<String> Type, but resolveType() is package protected with a note about how the intermediate value wouldn't be Serializable (personally I couldn't care less about that, and there are other methods that have notes about non-serializable resolvable type methods that are not package protected).

My workaround is (expecting that client usage looks like the above),... to take both the returnType.getGeneric(0) (first generic of the method type) and returnType.as(Thing.class).getGeneric(0) (the T passed to Thing<T>) and sanity check that they are both .isAssignableFrom() each other, and then use returnType.getGeneric(0).getType() which does resolve.

I'm wondering if it's possible to either make resolveType() public or add something like this for this case?

   public Type getResolvedGenericType(int index) { return getGeneric(i).resolveType().getType(); }

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions