Search in sources :

Example 1 with JInvocation

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

the class ObjectRule method addToString.

private void addToString(JDefinedClass jclass) {
    JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
    Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
    JBlock body = toString.body();
    JInvocation reflectionToString = jclass.owner().ref(toStringBuilder).staticInvoke("reflectionToString");
    reflectionToString.arg(JExpr._this());
    body._return(reflectionToString);
    toString.annotate(Override.class);
}
Also used : JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod)

Example 2 with JInvocation

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

the class ObjectRule method addConstructors.

private void addConstructors(JDefinedClass jclass, JsonNode node, Schema schema, boolean onlyRequired) {
    LinkedHashSet<String> classProperties = getConstructorProperties(node, onlyRequired);
    LinkedHashSet<String> combinedSuperProperties = getSuperTypeConstructorPropertiesRecursive(node, schema, onlyRequired);
    // no properties to put in the constructor => default constructor is good enough.
    if (classProperties.isEmpty() && combinedSuperProperties.isEmpty()) {
        return;
    }
    // add a no-args constructor for serialization purposes
    JMethod noargsConstructor = jclass.constructor(JMod.PUBLIC);
    noargsConstructor.javadoc().add("No args constructor for use in serialization");
    // add the public constructor with property parameters
    JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
    JBlock constructorBody = fieldsConstructor.body();
    JInvocation superInvocation = constructorBody.invoke("super");
    Map<String, JFieldVar> fields = jclass.fields();
    Map<String, JVar> classFieldParams = new HashMap<String, JVar>();
    for (String property : classProperties) {
        JFieldVar field = fields.get(property);
        if (field == null) {
            throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
        }
        fieldsConstructor.javadoc().addParam(property);
        JVar param = fieldsConstructor.param(field.type(), field.name());
        constructorBody.assign(JExpr._this().ref(field), param);
        classFieldParams.put(property, param);
    }
    List<JVar> superConstructorParams = new ArrayList<JVar>();
    for (String property : combinedSuperProperties) {
        JFieldVar field = searchSuperClassesForField(property, jclass);
        if (field == null) {
            throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
        }
        JVar param = classFieldParams.get(property);
        if (param == null) {
            param = fieldsConstructor.param(field.type(), field.name());
        }
        fieldsConstructor.javadoc().addParam(property);
        superConstructorParams.add(param);
    }
    for (JVar param : superConstructorParams) {
        superInvocation.arg(param);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JInvocation(com.sun.codemodel.JInvocation) JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 3 with JInvocation

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

the class AdditionalPropertiesRule method addBuilder.

private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
    JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");
    JVar nameParam = builder.param(String.class, "name");
    JVar valueParam = builder.param(propertyType, "value");
    JBlock body = builder.body();
    JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
    mapInvocation.arg(nameParam);
    mapInvocation.arg(valueParam);
    body._return(JExpr._this());
}
Also used : JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 4 with JInvocation

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

the class DefaultRule method getDefaultEnum.

private JExpression getDefaultEnum(JType fieldType, JsonNode node) {
    JInvocation invokeFromValue = ((JClass) fieldType).staticInvoke("fromValue");
    invokeFromValue.arg(node.asText());
    return invokeFromValue;
}
Also used : JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation)

Example 5 with JInvocation

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

the class DefaultRule method getDefaultSet.

/**
     * Creates a default value for a set property by:
     * <ol>
     * <li>Creating a new {@link LinkedHashSet} with the correct generic type
     * <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
     * correct default values
     * </ol>
     *
     * @param fieldType
     *            the java type that applies for this field ({@link Set} with
     *            some generic type argument)
     * @param node
     *            the node containing default values for this set
     * @return an expression that creates a default value that can be assigned
     *         to this field
     */
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
    JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
    JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
    setImplClass = setImplClass.narrow(setGenericType);
    JInvocation newSetImpl = JExpr._new(setImplClass);
    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
        }
        newSetImpl.arg(invokeAsList);
    } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
        return JExpr._null();
    }
    return newSetImpl;
}
Also used : JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Arrays(java.util.Arrays)

Aggregations

JInvocation (com.sun.codemodel.JInvocation)28 JVar (com.sun.codemodel.JVar)17 JClass (com.sun.codemodel.JClass)16 JMethod (com.sun.codemodel.JMethod)15 JBlock (com.sun.codemodel.JBlock)14 JFieldVar (com.sun.codemodel.JFieldVar)9 JExpression (com.sun.codemodel.JExpression)7 HashMap (java.util.HashMap)7 JDefinedClass (com.sun.codemodel.JDefinedClass)5 ArrayList (java.util.ArrayList)5 ByteString (com.linkedin.data.ByteString)3 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)3 JType (com.sun.codemodel.JType)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2