Last active
December 17, 2015 21:39
-
-
Save DominikMostek/5676134 to your computer and use it in GitHub Desktop.
Shows how to produce enum elements at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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