use of com.sun.codemodel.JEnumConstant in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addEnumConstants.
protected void addEnumConstants(EnumDefinition enumDefinition, JDefinedClass _enum, Schema schema) {
JType type = enumDefinition.getBackingType();
String nodeName = enumDefinition.getNodeName();
JsonNode parentNode = enumDefinition.getEnumNode();
for (EnumValueDefinition enumValueDefinition : enumDefinition.values()) {
JEnumConstant constant = _enum.enumConstant(enumValueDefinition.getName());
String value = enumValueDefinition.getValue();
constant.arg(DefaultRule.getDefaultValue(type, value));
Annotator annotator = ruleFactory.getAnnotator();
annotator.enumConstant(_enum, constant, value);
String enumNodeName = nodeName + "#" + value;
if (enumValueDefinition.hasTitle()) {
JsonNode titleNode = enumValueDefinition.getTitleNode();
ruleFactory.getTitleRule().apply(enumNodeName, titleNode, parentNode, constant, schema);
}
if (enumValueDefinition.hasDescription()) {
JsonNode descriptionNode = enumValueDefinition.getDescriptionNode();
ruleFactory.getDescriptionRule().apply(enumNodeName, descriptionNode, parentNode, constant, schema);
}
}
}
use of com.sun.codemodel.JEnumConstant in project rest.li by linkedin.
the class JavaDataTemplateGenerator method generateEnum.
protected void generateEnum(JDefinedClass enumClass, EnumTemplateSpec enumSpec) {
enumClass.javadoc().append(enumSpec.getSchema().getDoc());
setDeprecatedAnnotationAndJavadoc(enumSpec.getSchema(), enumClass);
generateSchemaField(enumClass, enumSpec.getSchema(), enumSpec.getSourceFileFormat());
for (String value : enumSpec.getSchema().getSymbols()) {
if (isReserved(value)) {
throw new IllegalArgumentException("Enum contains Java reserved symbol: " + value + " schema: " + enumSpec.getSchema());
}
final JEnumConstant enumConstant = enumClass.enumConstant(value);
final String enumConstantDoc = enumSpec.getSchema().getSymbolDocs().get(value);
if (enumConstantDoc != null) {
enumConstant.javadoc().append(enumConstantDoc);
}
setDeprecatedAnnotationAndJavadoc(enumSpec.getSchema(), value, enumConstant);
}
enumClass.enumConstant(DataTemplateUtil.UNKNOWN_ENUM);
}
use of com.sun.codemodel.JEnumConstant in project jsonschema2pojo by joelittlejohn.
the class EnumRule method addEnumConstants.
private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type) {
Collection<String> existingConstantNames = new ArrayList<String>();
for (int i = 0; i < node.size(); i++) {
JsonNode value = node.path(i);
if (!value.isNull()) {
String constantName = getConstantName(value.asText(), customNames.path(i).asText());
constantName = makeUnique(constantName, existingConstantNames);
existingConstantNames.add(constantName);
JEnumConstant constant = _enum.enumConstant(constantName);
String typeName = type.unboxify().fullName();
if (typeName.equals("int")) {
// integer
constant.arg(JExpr.lit(value.intValue()));
} else if (typeName.equals("long")) {
// integer-as-long
constant.arg(JExpr.lit(value.longValue()));
} else if (typeName.equals("double")) {
// number
constant.arg(JExpr.lit(value.doubleValue()));
} else if (typeName.equals("boolean")) {
// boolean
constant.arg(JExpr.lit(value.booleanValue()));
} else {
constant.arg(JExpr.lit(value.asText()));
}
ruleFactory.getAnnotator().enumConstant(constant, value.asText());
}
}
}
Aggregations