[code](Class<T> ) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];[/code]
the code above causes a cast exception:
[code]java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType[/code]
and it is because:
[color=red]Reflection on generic parameters can happen only if you define parameters types at compile time rather than at run time.[/color]
.getGenericSuperclass()).getActualTypeArguments()[0];[/code]
the code above causes a cast exception:
[code]java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType[/code]
and it is because:
[color=red]Reflection on generic parameters can happen only if you define parameters types at compile time rather than at run time.[/color]
本文探讨了Java中使用反射获取泛型参数类型时遇到的问题:尝试将Class对象转换为ParameterizedType导致的类型转换异常。解释了该问题的根本原因在于泛型参数类型的定义时机,并提供了背景信息。
874

被折叠的 条评论
为什么被折叠?



