How to get the actual type arguments to an indirectly implemented generic interface?

后端 未结 3 1462
时光说笑
时光说笑 2021-02-09 08:40

I have a parameterized interface that is implemented in many different ways. At run time I need to figure out, given an arbitrary object that implements that interface, what the

3条回答
  •  遥遥无期
    2021-02-09 08:49

    The short answer is NO. I agree that it is a pity... :( The reason is that Java drops type parameters at the compile stage. They do not exist in byte code. They are used by compiler only.

    To solve your problem you have to add yet another "regular" parameter of type Class and pass it to the constructor when you are creating an instance of Base:

    class Base implements Awesome> { 
        private E type;
        public Base(E type) {
            this.type = type;
        }
    }
    

提交回复
热议问题