In Java, with Sun's JDK 1.6, with an enum such as this:
public enum MyEnum {
FIRST_MEMBER { public void foo() { } },
SECOND_MEMBER { public void foo() { } },
THIRD_MEMBER { public void foo() { } };
}
The compiled files are:
MyEnum$1.class MyEnum$2.class MyEnum$3.class MyEnum.class
This also means that a stack trace showing foo()
, or the method call printed in JVisualVM, etc., will have something like this at the top:
mypackage.MyEnum$1.run()
The $1
in the class name is due to the members of the enum being compiled to anonymous inner classes. I am wondering if it is safe to assume that the numbers used in these class names map to the order in which the enum members are defined? If it is not, is there a standard, guaranteed way to find the enum member from the number used as the anonymous class name?
EDIT
In regards to the design of the enum, this was used for illustration purposes only. The real enum implements an interface, and each member provides a different implementation of the methods. Please don't pay too much attention to what admittedly looks a bit strange.
EDIT #2
To clarify, I am not trying to do anything with this information programmatically (such as weird reflection nonsense). Rather, I am looking at stack traces and profiling information, and trying to map a method call on an enum member (shown as a call on an anonymous class) against the actual enum member in the source code.