How to check if java.lang.reflect.Type is an Enum

后端 未结 4 1087
醉话见心
醉话见心 2020-12-30 23:16

I want to check whether a java.lang.reflect.Type instance represents an Emum object or not.

I can check whether it\'s an instance of a specific class us

相关标签:
4条回答
  • 2020-12-31 00:01

    Why don't you use .equals method to compare this type of comparisons. == is mostly used for primitive types.

    type.equals(Enum.class)
    

    or maybe you will need compare your own classes.

    type.equals(MyClass.class)
    
    0 讨论(0)
  • 2020-12-31 00:02

    Class.isEnum() will do it for you.

    Refer to Oracle Doc

    0 讨论(0)
  • 2020-12-31 00:05
    if(type instanceof Class && (Class)type.getClass().isEnum()) {...}
    
    0 讨论(0)
  • 2020-12-31 00:16
    if(type instanceof Class && ((Class<?>)type).isEnum())
    
    0 讨论(0)
提交回复
热议问题