tags:

views:

229

answers:

3

Hi All

I came across thas problem that I without knowing the actual enum type i need to iterate its possible values.

if (value instanceOf Enum){
   Enum enumValue = (Enum)value;
}

Any ideas how to extract from enumValue its possible values ?

Thanks

+7  A: 
Object[] possibleValues = enumValue.getDeclaringClass().getEnumConstants();
ColinD
+6  A: 

Enums are just lyke Classes in that they are typed. Your current code just checks if it is an Enum without specifying what type of Enum it is a part of.

Because you haven't specified the type of the enum, you will have to use reflection to find out what the list of enum values is.

You can do it like so:

enumValue.getDeclaringClass().getEnumConstants() 

This will return an array of Enum objects, with each being one of the available options.

RodeoClown
A: 

... or MyEnum.values() ? Or am I missing something?

deleted
Yes, the actual class of the enum is not available here to make a static method call on, just an instance of some subtype of Enum.
ColinD