How do I build a Java type object at runtime from a generic type definition and runtime type parameters?

前端 未结 6 1163
无人及你
无人及你 2020-12-05 12:16

Assuming a generic type declaration (Java)

class Foo {
    public T bar;
}

how can I, at runtime, instantiate a Type object that r

6条回答
  •  粉色の甜心
    2020-12-05 12:28

    What everyone else said :) . The class that you want to instantiate needs to be available at runtime. Many ways of doing this: put the class or class name in a variable local to your factory, have a protected method, create an "object factory" class if you need to do this in many different places, etc. This is the kind of job that bean frameworks do, so if you are using one, it might be possible to do it by configuring that.

提交回复
热议问题