vendredi 5 février 2016

Java reflection and generics - parameterized type info missing "extends"

I would like to reconstitute the signature of a method using reflection in Java 8. I've run into some issues with generic methods. In particular, I find nothing to help recover information about cases of "extends" or "super" in the template type declaration.

Here's a specific test case, based on known source code:

Signature of the method from source code:
public static <T extends AssertDelegateTarget> T assertThat(T assertion)

First of all, is there an elegant way to reconstitute the <T extends AssertDelegateTarget> part without having to parse Strings?

Here's the main info I was able to get by reflection:

  • method.toString():

    public static org.assertj.core.api.AssertDelegateTarget org.assertj.core.api.Assertions.assertThat(org.assertj.core.api.AssertDelegateTarget)

  • method.toGenericString():

    public static <T> T org.assertj.core.api.Assertions.assertThat(T)

    What happened to "extends AssertDelegateTarget"?

  • method.getReturnType.toString():

    interface org.assertj.core.api.AssertDelegateTarget

  • method.getGenericParameterTypes()[0].getTypeName():

    T

Based on the above reflection info, I could assume a relationship between T and AssertDelegateTarget, but how do I know whether it's "extends" or "super"?





Aucun commentaire:

Enregistrer un commentaire