【发布时间】:2011-05-07 14:41:36
【问题描述】:
可能的重复:
Get generic type of class at runtime
How to get the generic type at runtime?
你好,
如何在运行时获取 java 中泛型类型的类型?
在 C# 中,我可以使用 typeof 运算符。使用 typeof 运算符,我得到一个类型对象,其中包含有关此泛型类型在运行时的类型的更多信息。
Java中有等价的运算符吗?
提前致谢。
亲切的问候,帕特里克
//编辑:
我想做什么:
public ArrayList<T> SelectObjects() {
// I need to get here the type name for the oql script!
//this.SelectObjects( "select p from " + Class.forName(T));
}
public ArrayList<T> SelectObjects(String oql) {
try {
Iterator<T> iterator =
this.em.createQuery(oql).getResultList().iterator();
ArrayList<T> objects =
new ArrayList<T>();
while(iterator.hasNext())
{
objects.add(iterator.next());
}
return objects;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
我动态设置了一个 oql 脚本。如何从 T 中获取类型?
谢谢!
【问题讨论】: