Is there a way to find the type of a template (generic) parameter in Java?

前端 未结 1 1974
灰色年华
灰色年华 2020-12-10 14:41

I\'m trying to do something like this in Java:

public static  T foo() {
  return (T) bar(T);
}

public static Object bar(Class klaz) {
  re         


        
相关标签:
1条回答
  • 2020-12-10 15:02

    There's no way to get the type. Java's generics use type erasure so even java itself does not know what T is at runtime.

    As for workarounds: you could define foo to take a parameter of type Class<T> and then use that.

    0 讨论(0)
提交回复
热议问题