62,615
社区成员
发帖
与我相关
我的任务
分享
OptionManager manager = new OptionManager();
manager 这个对象如果加泛型,则Type superClass = getClass().getGenericSuperclass();
这句代码获取的是ParameterizedType这个接口的实现实例,而这个接口不是class的子类,所以if里是false,可以获取trueType--即泛型,也就是实际的代码中的参数。但是如果不加泛型,则if里会是true,这时候会抛出异常,因为这时候的superClass就是GenericDAO的对象,也就是加了泛型后的ParameterizedType这个接口的rawType这个属性。上次没看明白楼主的意思,这样子解释楼主应该可以理解了吧。
protected Tmp() {
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof Class<?>) { // sanity check, should never happen
throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
}
}
public static void main(String[] args) {
Tmp t = new Tmp<Object>();
}
jdk1.6实测,if里返回的是true
Exception in thread "main" java.lang.IllegalArgumentException: Internal error: TypeReference constructed without actual type information