Search in sources :

Example 1 with Annotator

use of org.jsonschema2pojo.Annotator in project jsonschema2pojo by joelittlejohn.

the class ObjectRule method createClass.

/**
 * Creates a new Java class that will be generated.
 *
 * @param nodeName
 *            the node name which may be used to dictate the new class name
 * @param node
 *            the node representing the schema that caused the need for a
 *            new class. This node may include a 'javaType' property which
 *            if present will override the fully qualified name of the newly
 *            generated class.
 * @param _package
 *            the package which may contain a new class after this method
 *            call
 * @return a reference to a newly created class
 * @throws ClassAlreadyExistsException
 *             if the given arguments cause an attempt to create a class
 *             that already exists, either on the classpath or in the
 *             current map of classes to be generated.
 */
private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _package) throws ClassAlreadyExistsException {
    JDefinedClass newType;
    Annotator annotator = ruleFactory.getAnnotator();
    try {
        if (node.has("existingJavaType")) {
            String fqn = substringBefore(node.get("existingJavaType").asText(), "<");
            if (isPrimitive(fqn, _package.owner())) {
                throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner()));
            }
            JClass existingClass = resolveType(_package, fqn + (node.get("existingJavaType").asText().contains("<") ? "<" + substringAfter(node.get("existingJavaType").asText(), "<") : ""));
            throw new ClassAlreadyExistsException(existingClass);
        }
        boolean usePolymorphicDeserialization = annotator.isPolymorphicDeserializationSupported(node);
        if (node.has("javaType")) {
            String fqn = node.path("javaType").asText();
            if (isPrimitive(fqn, _package.owner())) {
                throw new GenerationException("javaType cannot refer to a primitive type (" + fqn + "), did you mean to use existingJavaType?");
            }
            if (fqn.contains("<")) {
                throw new GenerationException("javaType does not support generic args (" + fqn + "), did you mean to use existingJavaType?");
            }
            int index = fqn.lastIndexOf(".") + 1;
            if (index == 0) {
                // Actually not a fully qualified name
                fqn = _package.name() + "." + fqn;
                index = fqn.lastIndexOf(".") + 1;
            }
            if (index >= 0 && index < fqn.length()) {
                fqn = fqn.substring(0, index) + ruleFactory.getGenerationConfig().getClassNamePrefix() + fqn.substring(index) + ruleFactory.getGenerationConfig().getClassNameSuffix();
            }
            if (usePolymorphicDeserialization) {
                newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS);
            } else {
                newType = _package.owner()._class(fqn);
            }
        } else {
            if (usePolymorphicDeserialization) {
                newType = _package._class(JMod.PUBLIC, ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, _package), ClassType.CLASS);
            } else {
                newType = _package._class(ruleFactory.getNameHelper().getUniqueClassName(nodeName, node, _package));
            }
        }
    } catch (JClassAlreadyExistsException e) {
        throw new ClassAlreadyExistsException(e.getExistingClass());
    }
    annotator.typeInfo(newType, node);
    annotator.propertyInclusion(newType, node);
    return newType;
}
Also used : JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) JDefinedClass(com.sun.codemodel.JDefinedClass) Annotator(org.jsonschema2pojo.Annotator) JClass(com.sun.codemodel.JClass) GenerationException(org.jsonschema2pojo.exception.GenerationException) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) ClassAlreadyExistsException(org.jsonschema2pojo.exception.ClassAlreadyExistsException)

Example 2 with Annotator

use of org.jsonschema2pojo.Annotator 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)

Aggregations

Annotator (org.jsonschema2pojo.Annotator)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JClass (com.sun.codemodel.JClass)1 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 JEnumConstant (com.sun.codemodel.JEnumConstant)1 JType (com.sun.codemodel.JType)1 ClassAlreadyExistsException (org.jsonschema2pojo.exception.ClassAlreadyExistsException)1 GenerationException (org.jsonschema2pojo.exception.GenerationException)1 EnumValueDefinition (org.jsonschema2pojo.model.EnumValueDefinition)1