fork(4) download
  1. import java.lang.invoke.MethodHandle;
  2. import java.lang.invoke.MethodHandles;
  3. import java.lang.reflect.Constructor;
  4. import java.util.EnumSet;
  5.  
  6. class EnumHack {
  7. public static void main(String[] args) throws Throwable {
  8. System.out.println(Runtime.version());
  9. Constructor<Example> c
  10. = Example.class.getDeclaredConstructor(String.class, int.class);
  11. c.setAccessible(true);
  12. MethodHandle h = MethodHandles.lookup().unreflectConstructor(c);
  13. Example baz = (Example) h.invokeExact("BAZ", 42);
  14. System.out.println("created Example " + baz + "(" + baz.ordinal() + ')');
  15. EnumSet<Example> set = EnumSet.allOf(Example.class);
  16. System.out.println(set.contains(baz));
  17. set.add(baz);
  18. System.out.println(set);
  19. }
  20.  
  21. enum Example {
  22. FOO, BAR
  23. }
  24. }
Runtime error #stdin #stdout #stderr 0.15s 49472KB
stdin
Standard input is empty
stdout
12.0.1+12
created Example BAZ(42)
false
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 42 out of bounds for length 2
	at java.base/java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:105)
	at java.base/java.util.RegularEnumSet$EnumSetIterator.next(RegularEnumSet.java:78)
	at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:472)
	at java.base/java.lang.String.valueOf(String.java:3042)
	at java.base/java.io.PrintStream.println(PrintStream.java:897)
	at EnumHack.main(Main.java:18)