开发者

how to know bounded type parameter on a class

开发者 https://www.devze.com 2023-03-07 01:24 出处:网络
I declared a class like this: public class TestManagerImpl<T extends IParams> implements TestManager<T> {

I declared a class like this:

public class TestManagerImpl<T extends IParams> implements TestManager<T> {

  public TestManagerImpl() {}
  ...

}

I wanted to know, if it was possible to extract which type from IParams is T when an object is instancied for this class ? When i try to test on a getClass(), it doesn't give me a Type that implements ParameterizedType.

I'v read that I can't, due to type erasure. Angelika Langer's Java Generics FAQ. But i'm not sure t开发者_运维技巧hat it's related to my problem, isn't it ?

Thank you


You can pass Class object to constructor. But I don't know if it is good practice.


Type erasure is precisely the problem here. You cannot discover at run-time what T is because that information is not preserved by the compiler. The only way to find out is if you actually have an object of type T passed into one of your methods (@Nikita suggests one such mechanism, but of course it relies on the cooperation of whoever is using your class).

0

精彩评论

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