Search in sources :

Example 1 with JEnumConstant

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);
        }
    }
}
Also used : JEnumConstant(com.sun.codemodel.JEnumConstant) Annotator(org.jsonschema2pojo.Annotator) EnumValueDefinition(org.jsonschema2pojo.model.EnumValueDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) JType(com.sun.codemodel.JType)

Example 2 with JEnumConstant

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);
}
Also used : JEnumConstant(com.sun.codemodel.JEnumConstant) ByteString(com.linkedin.data.ByteString)

Example 3 with JEnumConstant

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());
        }
    }
}
Also used : JEnumConstant(com.sun.codemodel.JEnumConstant) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JEnumConstant (com.sun.codemodel.JEnumConstant)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ByteString (com.linkedin.data.ByteString)1 JType (com.sun.codemodel.JType)1 ArrayList (java.util.ArrayList)1 Annotator (org.jsonschema2pojo.Annotator)1 EnumValueDefinition (org.jsonschema2pojo.model.EnumValueDefinition)1