Search code examples
javajcodemodel

Define enum constant body in JCodeModel


I'd like to create the following Java source code using JCodeModel

public enum MyEnum {
    FIRST_CONSTANT {

        @Override
        public String toString() {
            return "first";
        }
    },
    SECOND_CONSTANT {

        @Override
        public String toString() {
            return "second";
        }
    };

    public abstract String toString();
}

But JEnumConstant does not expose methods to define the enum constant's body.

Is there a way to achieve this? Moreover I noticed that JCodeModel does not support certain JDK 7 oder JDK 8 features like try with resources. Is there an alternative source code generation utility?


Solution

  • Unfortunately, you are correct. the Latest version of JCodeModel (2.6) does not have the capabilities to generate enum constant methods.