简体   繁体   中英

“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. Here the mentioned cast is from an object of type java.lang.reflect.Type to Class<?> .

Apart from primitive types, which type in Java is not a class?

Thanks for your answers

A Class is a Type but a Type isn't necessary a Class , since Class implements 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.

Try to print myType.getClass() to see the type you mean: you'll get Class<TheSaidType> .

From the Javadoc for Type you can see it has a number of implementing types.

All Known Subinterfaces:
    GenericArrayType, ParameterizedType, TypeVariable<D>, WildcardType

All Known Implementing Classes:
    Class 

Th sub-interfaces are implemented using internal data structure classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM