我有一个对象,我需要获取 Type对于那个对象。我需要从已删除的对象中获取 rawType。
例如我有一个类
class Blah{}
class Dummy extends Blah{}
class A<T extends Blah>
{
}
i create a instance for A like this new A<Dummy>().
我需要获取所创建对象的原始类型,我看到 Apache 有 TypeUtils getRawClass。但它接受 java 类型。我有实际的实例,但如何从中获取类型。执行 object.getClass() 似乎没有帮助,并且原始类型没有恢复。希望得到一些意见。
编辑: 感谢您的回复,就像下面的答案一样,我找不到任何方法来获取实际类型。保留类型的唯一建议是使用父类(super class)型标记。 http://gafter.blogspot.com/2006/12/super-type-tokens.html
最佳答案
I have the actual instance but how do i get the Type from that
由于泛型类型删除,这是不可能的。
在运行时,实例没有其泛型类型的记录。
查看此other thread进行更多讨论。
关于java - 从类型删除的 Java 对象获取原始类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25050544/