Skip to content

Instantly share code, notes, and snippets.

@DominikMostek
Last active December 17, 2015 21:39
Show Gist options
  • Save DominikMostek/5676134 to your computer and use it in GitHub Desktop.
Save DominikMostek/5676134 to your computer and use it in GitHub Desktop.
Shows how to produce enum elements at runtime
package cz.dmostek.test;
import java.lang.reflect.Constructor;
import sun.reflect.ReflectionFactory;
public class EnumTest {
public enum Foo {
BAR
}
public static void main(String[] args) throws Exception {
Constructor cstr = Foo.class.getDeclaredConstructor(String.class, int.class);
ReflectionFactory reflection = ReflectionFactory.getReflectionFactory();
Foo e = (Foo) reflection.newConstructorAccessor(cstr).newInstance(new Object[] {"BLA", 3});
System.out.println(e.toString()); // BLA
System.out.println(e instanceof Foo); // true
System.out.println(e.ordinal()); // 3
// Foo.values() returns only [BAR]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment