简体   繁体   English

“无法转换为java.lang.Class”,即:哪种类型不是类

[英]“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 . ClassTypeType不一定是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也可以是GenericArrayTypeParameterizedTypeTypeVariableWildcardType接口的任何实现类,因为它们都扩展了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.

相关问题 Java:ClassCastException-无法将java.lang.Class强制转换为 - Java: ClassCastException - java.lang.Class cannot be cast to 如何在没有警告的情况下转换java.lang.Class <?>类型的对象? - How to cast objects of type java.lang.Class<?> without warnings? class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized - class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized 引起:java.lang.ClassCastException:使用Generic Type时,libcore.reflect.ParameterizedTypeImpl无法强制转换为java.lang.Class - Caused by: java.lang.ClassCastException: libcore.reflect.ParameterizedTypeImpl cannot be cast to java.lang.Class when use Generic Type 无法调用需要java.lang.Class参数的方法? - Cannot call method which requires java.lang.Class arguments? java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType GroovyCastException:无法将具有类“java.lang.Class”的对象“class com.jcraft.jsch.ChannelExec”转换为类 - GroovyCastException: Cannot cast object 'class com.jcraft.jsch.ChannelExec' with class 'java.lang.Class' to class Spring,CXF,java.lang.Class无法转换为[Ljava.lang.Class; - Spring, CXF, java.lang.Class cannot be cast to [Ljava.lang.Class; How to fix java.lang.ClassCastException: java.lang.Class cannot be cast to java.util.Map - How to fix java.lang.ClassCastException: java.lang.Class cannot be cast to java.util.Map 调用EJB Bean方法时,不能将java.lang.Class强制转换为java.lang.String - java.lang.Class cannot be cast to java.lang.String when invoking a EJB Bean Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM