Search in sources :

Example 56 with JType

use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.

the class TypeRuleTest method applyGeneratesNullAsObject.

@Test
public void applyGeneratesNullAsObject() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "null");
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is(Object.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 57 with JType

use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.

the class DynamicPropertiesRule method addPublicWithMethod.

private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
    JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
    // if we have additional properties, then put value.
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
        notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam).arg(cast(additionalPropertiesType, valueParam)));
    } else // else throw exception.
    {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    body._return(_this());
    return method;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 58 with JType

use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.

the class DynamicPropertiesRule method addInternalGetMethodJava6.

private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
    JBlock body = method.body();
    JConditional propertyConditional = null;
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext(); ) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();
            JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
            if (propertyConditional == null) {
                propertyConditional = body._if(condition);
            } else {
                propertyConditional = propertyConditional._elseif(condition);
            }
            JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
            propertyConditional._then()._return(invoke(propertyGetter));
        }
    }
    JClass extendsType = jclass._extends();
    JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
    } else {
        lastBlock._return(notFoundParam);
    }
    return method;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JsonNode(com.fasterxml.jackson.databind.JsonNode) JExpression(com.sun.codemodel.JExpression) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod) Map(java.util.Map) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 59 with JType

use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.

the class EnumRule method apply.

/**
     * Applies this schema rule to take the required code generation steps.
     * <p>
     * A Java {@link Enum} is created, with constants for each of the enum
     * values present in the schema. The enum name is derived from the nodeName,
     * and the enum type itself is created as an inner class of the owning type.
     * In the rare case that no owning type exists (the enum is the root of the
     * schema), then the enum becomes a public class in its own right.
     * <p>
     * The actual JSON value for each enum constant is held in a property called
     * "value" in the generated type. A static factory method
     * <code>fromValue(String)</code> is added to the generated enum, and the
     * methods are annotated to allow Jackson to marshal/unmarshal values
     * correctly.
     *
     * @param nodeName
     *            the name of the property which is an "enum"
     * @param node
     *            the enum node
     * @param container
     *            the class container (class or package) to which this enum
     *            should be added
     * @return the newly generated Java type that was created to represent the
     *         given enum
     */
@Override
public JType apply(String nodeName, JsonNode node, JClassContainer container, Schema schema) {
    JDefinedClass _enum;
    try {
        _enum = createEnum(node, nodeName, container);
    } catch (ClassAlreadyExistsException e) {
        return e.getExistingClass();
    }
    schema.setJavaTypeIfEmpty(_enum);
    if (node.has("javaInterfaces")) {
        addInterfaces(_enum, node.get("javaInterfaces"));
    }
    // copy our node; remove the javaType as it will throw off the TypeRule for our case
    ObjectNode typeNode = (ObjectNode) node.deepCopy();
    typeNode.remove("javaType");
    // If type is specified on the enum, get a type rule for it.  Otherwise, we're a string.
    // (This is different from the default of Object, which is why we don't do this for every case.)
    JType backingType = node.has("type") ? ruleFactory.getTypeRule().apply(nodeName, typeNode, container, schema) : container.owner().ref(String.class);
    JFieldVar valueField = addValueField(_enum, backingType);
    // override toString only if we have a sensible string to return
    if (isString(backingType)) {
        addToString(_enum, valueField);
    }
    addValueMethod(_enum, valueField);
    addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
    addFactoryMethod(_enum, backingType);
    return _enum;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) ClassAlreadyExistsException(org.jsonschema2pojo.exception.ClassAlreadyExistsException) JType(com.sun.codemodel.JType)

Example 60 with JType

use of com.sun.codemodel.JType in project jsonschema2pojo by joelittlejohn.

the class AdditionalPropertiesRule method apply.

/**
     * Applies this schema rule to take the required code generation steps.
     * <p>
     * If additionalProperties is specified and set to the boolean value
     * <code>false</code>, this rule does not make any change to the generated
     * Java type (the type does not allow additional properties).
     * <p>
     * If the additionalProperties node is <code>null</code> (not specified in
     * the schema) or empty, then a new bean property named
     * "additionalProperties", of type {@link Map}{@literal <String,Object>} is
     * added to the generated type (with appropriate accessors). The accessors
     * are annotated to allow unrecognised (additional) properties found in JSON
     * data to be marshalled/unmarshalled from/to this map.
     * <p>
     * If the additionalProperties node is present and specifies a schema, then
     * an "additionalProperties" map is added to the generated type. This time
     * the map values will be restricted and must be instances of a newly
     * generated Java type that will be created based on the
     * additionalProperties schema provided. If the schema does not specify the
     * javaType property, the name of the newly generated type will be derived
     * from the nodeName and the suffix 'Property'.
     *
     * @param nodeName
     *            the name of the schema node for which the additionalProperties
     *            node applies
     * @param node
     *            the additionalProperties node itself, found in the schema (may
     *            be null if not specified in the schema)
     * @param jclass
     *            the Java type that is being generated to represent this schema
     * @return the given Java type jclass
     */
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
    if (node != null && node.isBoolean() && node.asBoolean() == false) {
        // no additional properties allowed
        return jclass;
    }
    if (!this.ruleFactory.getGenerationConfig().isIncludeAdditionalProperties()) {
        // no additional properties allowed
        return jclass;
    }
    if (!ruleFactory.getAnnotator().isAdditionalPropertiesSupported()) {
        // schema allows additional properties, but serializer library can't support them
        return jclass;
    }
    JType propertyType;
    if (node != null && node.size() != 0) {
        propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, jclass, schema);
    } else {
        propertyType = jclass.owner().ref(Object.class);
    }
    JFieldVar field = addAdditionalPropertiesField(jclass, propertyType);
    addGetter(jclass, field);
    addSetter(jclass, propertyType, field);
    if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
        ruleFactory.getValidRule().apply(nodeName, node, field, schema);
    }
    if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
        addBuilder(jclass, propertyType, field);
    }
    return jclass;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JType(com.sun.codemodel.JType)

Aggregations

JType (com.sun.codemodel.JType)65 Test (org.junit.Test)42 JCodeModel (com.sun.codemodel.JCodeModel)41 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)39 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)38 JPackage (com.sun.codemodel.JPackage)37 JVar (com.sun.codemodel.JVar)13 JBlock (com.sun.codemodel.JBlock)10 JMethod (com.sun.codemodel.JMethod)10 JClass (com.sun.codemodel.JClass)9 JDefinedClass (com.sun.codemodel.JDefinedClass)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)5 TextNode (com.fasterxml.jackson.databind.node.TextNode)4 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)3 JExpression (com.sun.codemodel.JExpression)3 JFieldVar (com.sun.codemodel.JFieldVar)3 JInvocation (com.sun.codemodel.JInvocation)3 JConditional (com.sun.codemodel.JConditional)2 JSwitch (com.sun.codemodel.JSwitch)2