final boolean useValuesAsName, final Enumeration<Facet> enumeration) {
AnnotationBuilder[] annotationBuilders =
state.getSGStateInfo().getSourceGenerator().getAnnotationBuilders();
JEnum jEnum = (JEnum) state.getJClass();
// add value field
JField jField = new JField(new JClass("java.lang.String"), "value");
JModifiers modifiers = new JModifiers();
modifiers.setFinal(true);
modifiers.makePrivate();
jField.setModifiers(modifiers);
jEnum.addField(jField);
JMethod valueMethod = new JMethod("value", new JClass("java.lang.String"),
"the value of this constant");
valueMethod.setSourceCode("return this.value;");
jEnum.addMethod(valueMethod, false);
JMethod fromValueMethod = new JMethod("fromValue", jEnum, "the constant for this value");
fromValueMethod.addParameter(new JParameter(new JClass("java.lang.String"), "value"));
JSourceCode sourceCode = new JSourceCode();
sourceCode.add("for (" + jEnum.getLocalName() + " c: "
+ jEnum.getLocalName() + ".values()) {");
sourceCode.indent();
sourceCode.add("if (c.value.equals(value)) {");
sourceCode.indent();
sourceCode.add("return c;");
sourceCode.unindent();
sourceCode.add("}");
sourceCode.unindent();
sourceCode.add("}");
sourceCode.add("throw new IllegalArgumentException(value);");
fromValueMethod.setSourceCode(sourceCode);
modifiers = new JModifiers();
modifiers.setStatic(true);
fromValueMethod.setModifiers(modifiers);
jEnum.addMethod(fromValueMethod, false);
JMethod setValueMethod = new JMethod("setValue");
setValueMethod.addParameter(new JParameter(new JClass("java.lang.String"), "value"));
jEnum.addMethod(setValueMethod, false);
JMethod toStringMethod = new JMethod("toString",
new JClass("java.lang.String"), "the value of this constant");
toStringMethod.setSourceCode("return this.value;");
jEnum.addMethod(toStringMethod, false);
JConstructor constructor = jEnum.createConstructor();
constructor.addParameter(new JParameter(new JClass("java.lang.String"), "value"));
constructor.setSourceCode("this.value = value;");
modifiers = new JModifiers();
modifiers.makePrivate();
constructor.setModifiers(modifiers);
jEnum.addConstructor(constructor);
int enumCount = 0;
while (enumeration.hasMoreElements()) {
Facet facet = (Facet) enumeration.nextElement();
JEnumConstant enumConstant;
if (useValuesAsName) {
enumConstant = new JEnumConstant(
translateEnumValueToIdentifier(component.getEnumBinding(), facet),
new String[]{"\"" + facet.getValue() + "\""});
} else {
enumConstant = new JEnumConstant(
"VALUE_" + enumCount, new String[]{"\""
+ facet.getValue() + "\""});
}
// custom annotations
for (int i = 0; i < annotationBuilders.length; i++) {
AnnotationBuilder annotationBuilder = annotationBuilders[i];
annotationBuilder.addEnumConstantAnnotations(facet, enumConstant);
}
jEnum.addConstant(enumConstant);
enumCount++;
}
// custom annotations
for (int i = 0; i < annotationBuilders.length; i++) {