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
In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.
if (obj. getClass(). isEnum()) { ... } If Enum is your custom class, then just check that obj instanceof Enum .
Provides classes that are fundamental to the design of the Java programming language. java.lang.reflect. Provides classes and interfaces for obtaining reflective information about classes and objects.
if(type instanceof Class && ((Class<?>)type).isEnum())
Class.isEnum() will do it for you.
Refer to Oracle Doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With