java - Get Enum Instance from Class<? extends Enum> using String value? -
i'm finding difficult put exact question words, i'll give example.
i have 2 enum types:
enum shape {     cat, dog; }  enum color {     blue, red; } i have method:
public object getinstance(string value, class<?> type); i use method like:
// somevalue "red", , someenumclass color.class color c = getinstance(somevalue, someenumclass); i've been having trouble determining how implement getinstance(). once know exact enum class want instantiate, it's easy:
color.valueof("red"); but how can above line accomplished unknown class? (it is, however, known someenumclass subclass of enum.)
thanks!
 public static <t extends enum<t>> t getinstance(final string value, final class<t> enumclass) {      return enum.valueof(enumclass, value);  } and method used as:
final shape shape = getinstance("cat", shape.class); then again, can use
final shape shape = shape.valueof("cat"); which shortcut
enum.valueof(shape.class, "cat"); 
Comments
Post a Comment