Quick quetion about Java Generic and Reflection

gbb21 2011-04-08 12:32:07
For a generic class, for example, List<T>, how can I get the "Class" object ?

For raw type List, I can get Class<List> by calling List.class; however, I cannot call List<T>.class to get Class<List<T>> object.

The motivation that I want to get Class<List<T>> is for the follow function call.

List<T> list = create(Class<List<T>> c);


thanks for your help, in advance .
...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
SENSSSZ 2011-04-09
  • 打赏
  • 举报
回复
你该看看泛型擦除,运行时是无法得到类型参数的信息的。而且,List类只有一个,不是说有多少种类型参数就会有多少个List类,所以也没有List<T>.class这种东西,只能是List.class。
大壹哥 2011-04-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gbb21 的回复:]
没有人唉~
[/Quote]
你应该说nobody!
gbb21 2011-04-09
  • 打赏
  • 举报
回复
没有人唉~
爱吃面条 2011-04-08
  • 打赏
  • 举报
回复
create(Class<List<T>> c);
这个方法接收一个普通的Class对象就可以了,返回一个List可以直接赋值给List<T> list
ldq67123 2011-04-08
  • 打赏
  • 举报
回复
if the list is not empty and the Class<T> is a POJO,your can test the type of any element in the list
if not ,or the Class<T> is just a abstract class,your have to test every element to get its type
does anyone have a better solution?
gbb21 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 slabcde 的回复:]

create(Class<List<T>> c);
这个方法接收一个普通的Class对象就可以了,返回一个List可以直接赋值给List<T> list
[/Quote]

If I call "create" function with List.class as argument, compiler will give me warning .....(saying something about raw type ....)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧