public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn’t used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful — a case-insensitive version of
Enum.valueOf(String). - It calls a static method on an instance so you can iterate.
EnumSetdoes all the reflective stuff for you.