How to get parametrized Class instance

前端 未结 4 1924
别那么骄傲
别那么骄傲 2020-12-20 14:30

Since generics were introduced, Class is parametrized, so that List.class produces Class. This is clear.

What I am not able to figure out is how to get a in

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 15:27

    The only thing you can do is instantiate List directly and call its getClass():

    instantiate(new List() { ... }.getClass());
    

    For types with multiple abstract methods like List, this is quite awkward. But unfortunately, calling subclass constructors (like new ArrayList) or factory methods (Collections.emptyList()) don't work.

提交回复
热议问题