Reputation: 717
I have a class that looks like this:
public class Catalog {
public enum Table1 implements IExcelEnum{
Name, Date, Id
}
public enum Table2 IExcelEnum {
...
}
}
I can get to the enum Table1, such as:
System.out.println(Catalog.Table2.Name.value());
But what I want to do is to be able to feed a String or an identifier and get the enum back out so that I can reference it when creating my data object. so for example:
Data dataCol1 = new Data(Catalog.getEnum("Table1"), ArrayList<String> values);
I have an interface called IExcelEnum that does not have any fields, its just so I can generically type my enums so my data structure is willing to accept any of them.
What is the cleanest way to do this?
Upvotes: 0
Views: 478
Reputation: 13900
You probably need to use reflection like this:
public class Catalog
{
public interface I
{
}
public enum T1 implements I
{
A, B, C;
}
public enum T2 implements I
{
D, E, F;
}
public static void main (String [] args) throws Exception
{
String name = "T1";
Class <? extends I> c =
(Class <? extends I>)Class.forName (
Catalog.class.getName () + "$" + name);
I [] values = (I [])c.getMethod ("values").invoke (null);
for (I i: values)
System.out.println (i);
}
}
Upvotes: 2