[英]“Cannot be cast to java.lang.Class”, namely: which type is not a class
This may seem a dumb question (as it could be very project specific) but in this case it's general enough: 这似乎是一个愚蠢的问题(因为它可能是非常特定于项目的),但在这种情况下,它已足够普遍:
I'm running a piece of code which gives the ClassCastException
, with the error message I wrote as title. 我正在运行一段代码,该代码给出了
ClassCastException
,并以我写为标题的错误消息出现。 Here the mentioned cast is from an object of type java.lang.reflect.Type
to Class<?>
. 这里提到的类型转换是从类型为
java.lang.reflect.Type
的对象到Class<?>
。
Apart from primitive types, which type in Java is not a class? 除了原始类型,Java中的哪种类型不是类?
Thanks for your answers 谢谢你的回答
A Class
is a Type
but a Type
isn't necessary a Class
, since Class
implements Type
. Class
是Type
但Type
不一定是Class
,因为Class
实现Type
。
Basically, your myType
could also be any of the implementing classes of the interfaces GenericArrayType
, ParameterizedType
, TypeVariable
or WildcardType
, since they all extend the Type
interface. 基本上,您的
myType
也可以是GenericArrayType
, ParameterizedType
, TypeVariable
或WildcardType
接口的任何实现类,因为它们都扩展了Type
接口。
Try to print myType.getClass()
to see the type you mean: you'll get Class<TheSaidType>
. 尝试打印
myType.getClass()
以查看您的意思是类型:您将获得Class<TheSaidType>
。
From the Javadoc for Type you can see it has a number of implementing types. 从Type的Javadoc中,您可以看到它具有许多实现类型。
All Known Subinterfaces:
GenericArrayType, ParameterizedType, TypeVariable<D>, WildcardType
All Known Implementing Classes:
Class
Th sub-interfaces are implemented using internal data structure classes. 子接口是使用内部数据结构类实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.