Search in sources :

Example 31 with JMethod

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

the class PropertiesRule method addOverrideBuilder.

private void addOverrideBuilder(JDefinedClass thisJDefinedClass, JMethod parentBuilder, JVar parentParam) {
    if (thisJDefinedClass.getMethod(parentBuilder.name(), new JType[] { parentParam.type() }) == null) {
        JMethod builder = thisJDefinedClass.method(parentBuilder.mods().getValue(), thisJDefinedClass, parentBuilder.name());
        builder.annotate(Override.class);
        JVar param = builder.param(parentParam.type(), parentParam.name());
        JBlock body = builder.body();
        body.invoke(JExpr._super(), parentBuilder).arg(param);
        body._return(JExpr._this());
    }
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 32 with JMethod

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

the class PropertyRule method apply.

/**
     * Applies this schema rule to take the required code generation steps.
     * <p>
     * This rule adds a property to a given Java class according to the Java
     * Bean spec. A private field is added to the class, along with accompanying
     * accessor methods.
     * <p>
     * If this rule's schema mapper is configured to include builder methods
     * (see {@link GenerationConfig#isGenerateBuilders()} ),
     * then a builder method of the form <code>withFoo(Foo foo);</code> is also
     * added.
     *
     * @param nodeName
     *            the name of the property to be applied
     * @param node
     *            the node describing the characteristics of this property
     * @param jclass
     *            the Java class which should have this property added
     * @return the given jclass
     */
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
    String propertyName = ruleFactory.getNameHelper().getPropertyName(nodeName, node);
    JType propertyType = ruleFactory.getSchemaRule().apply(nodeName, node, jclass, schema);
    node = resolveRefs(node, schema);
    int accessModifier = ruleFactory.getGenerationConfig().isIncludeAccessors() ? JMod.PRIVATE : JMod.PUBLIC;
    JFieldVar field = jclass.field(accessModifier, propertyType, propertyName);
    propertyAnnotations(nodeName, node, schema, field);
    formatAnnotation(field, node);
    ruleFactory.getAnnotator().propertyField(field, jclass, nodeName, node);
    if (ruleFactory.getGenerationConfig().isIncludeAccessors()) {
        JMethod getter = addGetter(jclass, field, nodeName, node);
        ruleFactory.getAnnotator().propertyGetter(getter, nodeName);
        propertyAnnotations(nodeName, node, schema, getter);
        JMethod setter = addSetter(jclass, field, nodeName, node);
        ruleFactory.getAnnotator().propertySetter(setter, nodeName);
        propertyAnnotations(nodeName, node, schema, setter);
    }
    if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
        addBuilder(jclass, field);
    }
    if (node.has("pattern")) {
        ruleFactory.getPatternRule().apply(nodeName, node.get("pattern"), field, schema);
    }
    ruleFactory.getDefaultRule().apply(nodeName, node.get("default"), field, schema);
    ruleFactory.getMinimumMaximumRule().apply(nodeName, node, field, schema);
    ruleFactory.getMinItemsMaxItemsRule().apply(nodeName, node, field, schema);
    ruleFactory.getMinLengthMaxLengthRule().apply(nodeName, node, field, schema);
    if (isObject(node) || isArray(node)) {
        ruleFactory.getValidRule().apply(nodeName, node, field, schema);
    }
    return jclass;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType)

Example 33 with JMethod

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

the class PropertyRule method addBuilder.

private JMethod addBuilder(JDefinedClass c, JFieldVar field) {
    JMethod builder = c.method(JMod.PUBLIC, c, getBuilderName(field.name()));
    JVar param = builder.param(field.type(), field.name());
    JBlock body = builder.body();
    body.assign(JExpr._this().ref(field), param);
    body._return(JExpr._this());
    return builder;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 34 with JMethod

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

the class PropertyRule method addSetter.

private JMethod addSetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) {
    JMethod setter = c.method(JMod.PUBLIC, void.class, getSetterName(jsonPropertyName, node));
    JVar param = setter.param(field.type(), field.name());
    JBlock body = setter.body();
    body.assign(JExpr._this().ref(field), param);
    return setter;
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 35 with JMethod

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

the class SerializableHelper method processMethodCollectionForSerializableSupport.

private static void processMethodCollectionForSerializableSupport(Iterator<JMethod> methods, DataOutputStream dataOutputStream) throws IOException {
    TreeMap<String, JMethod> sortedMethods = new TreeMap<String, JMethod>();
    while (methods.hasNext()) {
        JMethod method = methods.next();
        //Collect non-private methods
        if ((method.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
            sortedMethods.put(method.name(), method);
        }
    }
    for (JMethod method : sortedMethods.values()) {
        dataOutputStream.writeUTF(method.name());
        dataOutputStream.writeInt(method.mods().getValue());
        if (method.type() != null) {
            dataOutputStream.writeUTF(method.type().fullName());
        }
        for (JVar param : method.params()) {
            dataOutputStream.writeUTF(param.type().fullName());
        }
    }
}
Also used : TreeMap(java.util.TreeMap) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JMethod (com.sun.codemodel.JMethod)63 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)18 JInvocation (com.sun.codemodel.JInvocation)15 JFieldVar (com.sun.codemodel.JFieldVar)11 JType (com.sun.codemodel.JType)10 JExpression (com.sun.codemodel.JExpression)9 Map (java.util.Map)7 JConditional (com.sun.codemodel.JConditional)6 HashMap (java.util.HashMap)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 ByteString (com.linkedin.data.ByteString)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2