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

20,813

Solution 1

if(type instanceof Class && ((Class<?>)type).isEnum())

Solution 2

Class.isEnum() will do it for you.

Refer to Oracle Doc

Solution 3

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)
Share:
20,813
Edd
Author by

Edd

I'm a java developer and I ain't afraid to show it

Updated on January 24, 2020

Comments

  • Edd
    Edd over 4 years

    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 using == comparisons e.g:

    type == String.class // works
    

    but this doesn't seem to work for the Enum class:

    type == Enum.class // doesn't work
    

    ... this makes sense as the instance would be of a specific enum but I would like to check whether the type is for any enum or not.

    Could someone explain the obvious to me of how to tell whether the Type is an enum or not please