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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -