How to do `MyClass.class` in Java?

前端 未结 6 1285
有刺的猬
有刺的猬 2020-12-15 21:46

How can call public T doit(Class clazz); using MyClass.class as clazz where I can not instantiate or extend MyClas

6条回答
  •  情书的邮戳
    2020-12-15 22:27

    See http://jackson.codehaus.org/1.7.0/javadoc/org/codehaus/jackson/type/TypeReference.html and the references that it references for a comprehensive discussion of the issues around generics.

    the bottom line is that, if you really want to work with generic types in this way, you have to stop using Class and start using Type and its subclasses.

    Contrary to your comment on another answer, you can write List> obj = (List>) doit(List.class);, you just can't avoid a warning when you write it.

提交回复
热议问题