I've got the class object for an enum (I have a Class<? extends Enum>
) and I need to get a list of the enumerated values represented by this enum. The values
static function has what I need, but I'm not sure how to get access to it from the class object.
views:
200answers:
3Huh... I've never noticed that method before. +1
ChssPly76
2009-10-26 19:56:45
It is all a bit twisted... (but, hey, that is reflection for you)
Tom Hawtin - tackline
2009-10-26 19:58:04
+3
A:
using reflection is simple as calling Class#getEnumConstants():
List<Enum> enum2list(Class<? extends Enum> cls) {
return Arrays.asList(cls.getEnumConstants());
}
dfa
2009-10-26 19:58:04
+1
A:
If you know the name of the value you need:
Class<? extends Enum> klass = ...
Enum<?> x = Enum.value(klass, "NAME");
If you don't, you can get an array of them by (as Tom got to first):
klass.getEnumConstants();
Yishai
2009-10-26 20:04:39