Saturday, February 05, 2005

More fun with enum

The Enum<E extends Enum<E>> class is very special: only the compiler is able to create instances of classes which extends Enum. This prevents the creation of dynamic enumerations. But there is hope:

final MockControl control = MockClassControl.createControl(
        new Class[]{String.class, int.class},
        new Object{"Foo", 1});
final Enum mock = (Enum) control.getMock();

System.out.println(" = " +;
System.out.println("mock.ordinal() = " + mock.ordinal());

The magic of EasyMock and its class extension (using cglib under the hood) creates a Java proxy for Enum. Bytecode engineering to the rescue!

But do note that some methods associated with enumerations (such as values()) do not exist in the base class, Enum. They are synthesized by the compiler specific to each enumeration class.

Post a Comment