开发者

Getting class of return generic type possible?

开发者 https://www.devze.com 2023-01-08 17:06 出处:网络
I want to know the class of a generic return type of a method, something like th开发者_如何学编程is:

I want to know the class of a generic return type of a method, something like th开发者_如何学编程is:

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

The problem is, this is not a generic class, this is just a generic method, so I can't extract the generic type from the class. What I want to achieve is knowing what concrete type the caller wants without requiring the class, like:

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

Is that possible?


Well if Kylar is right I've to resort to:

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

I wanted to avoid the extra argument :(

0

精彩评论

暂无评论...
验证码 换一张
取 消