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

前端 未结 6 1176
无人及你
无人及你 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:39

    Gson does actually provide a solution for this: https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener

    Of course what this does is little more than Bohemian's solution (you still need to pass the type parameter somehow), but done automatically for you.

提交回复
热议问题