vendredi 17 mars 2023

Getting the classtype of a generic in an interface [duplicate]

I am trying to develop an http-library, mainly for getting a clue about how they work.

I am currently trying to automatically implement interfaces in which the methods look like this:

public Response<ExampleObject> getInformationAsExampleObject();

The interface is implemented and an InvocationHandler is created for each method.

Here is the problem: I haven't found a straightforward way to get the Class<?> for the T of the generic. In the example above it would be ExampleObject.class

It would be pretty simple if I was given an initialized object, but instead I am dealing with an unimplemented interface.

I have resorted to this:

Class.forName(Arrays.stream(((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()).toList().get(0).getTypeName());

and it feels like there has to be a more straightforward way to achieve the same result. The question is how.





Aucun commentaire:

Enregistrer un commentaire