Is it possible to get a class of a returned generic type?

I want to know the class of the general type of the return method, something like this:

<T> T getEntry() { System.out.println(T.class) } 

The problem is that this is not a general class, this is just a general method, so I cannot extract a generic type from a class. What I want to achieve is to know what specific type the caller requires without requiring a class, for example:

 <T> T getEntry(Class<?> clazz); 

Is it possible?

+4
source share
1 answer

Well, if Kaylar is right, I have to resort to:

 <T> T getEntry(String name, Class<T> clazz) 

I wanted to avoid an extra argument :(

+5
source

Source: https://habr.com/ru/post/1316593/


All Articles