I want to create universal method, for any enum object, that will check if enum has specified value name, but as Enum type object I am unnable to use method values();. Why?
Is there any way to get values from an Enum type object?
I need method like this to check if value from configuration is a valid string for myEnum.valueOf(String); because if given string will be wrong then it will throw an exception (and I do not want it).
I want my method to look like this:
public static Boolean enumContains(Enum en, String valueString){ return toStringList(en.values()).contains(valueString.toUpperCase()); } But there is no method Enum.values(). How to create this method correctly?
Enumin your example?Enumas type of argument to match anyEnumobject to this method.valuesdoes only work for theenumtype, e.g.MyEnum.values()but clearly not for a specific value of anenumlike in your method...getDeclaredField("$VALUES");orEnum.class.getDeclaredMethod("values");for the enum class