I am trying to generate a enum which contains additional methods, so from
enum Gender { MALE = 1, FEMALE = 2 }
I get
enum Gender {
MALE,
FEMALE;
int getValue() {
switch (this) {
case MALE: return 1;
case FEMALE: return 2;
default: throw new IllegalArgumentException();
}
}
}
This is my inferrer code:
def dispatch void infer(EnumDef enumDef, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
val enumName = enumDef.fullyQualifiedName.toString
acceptor.accept(enumDef.toEnumerationType(enumName) [
]).initializeLater [
documentation = enumDef.documentation
val intTypeRef = enumDef.newTypeRef(typeof(int))
for (const1 : enumDef.enumConsts) {
members += const1.toEnumerationLiteral(const1.name)
}
members += enumDef.toMethod("getValue", intTypeRef) [
body = [append('''
switch (this) {
«FOR const1 : enumDef.enumConsts»
case «const1.name»: return «const1.id»;
«ENDFOR»
default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
}
''')]
]
]
}
The problem is that the generated code lacks ";" after FEMALE and doesn't work Is this a bug or is there a way to do this correctly?
Ideally I'd also like to add a "int value" field to the enum, but there doesn't seem to be a way to set constructor arguments from "toEnumerationLiteral".