Search in sources :

Example 16 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 17 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 18 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 19 with JMethod

use of com.sun.codemodel.JMethod in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generateConstructorWithNoArg.

private static void generateConstructorWithNoArg(JDefinedClass cls, JClass newClass) {
    final JMethod noArgConstructor = cls.constructor(JMod.PUBLIC);
    noArgConstructor.body().invoke(THIS).arg(JExpr._new(newClass));
}
Also used : JMethod(com.sun.codemodel.JMethod)

Example 20 with JMethod

use of com.sun.codemodel.JMethod in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generateTyperef.

protected void generateTyperef(JDefinedClass typerefClass, TyperefTemplateSpec typerefSpec) {
    typerefClass.javadoc().append(typerefSpec.getSchema().getDoc());
    setDeprecatedAnnotationAndJavadoc(typerefSpec.getSchema(), typerefClass);
    typerefClass._extends(TyperefInfo.class);
    final JVar schemaField = generateSchemaField(typerefClass, typerefSpec.getSchema());
    final JMethod constructor = typerefClass.constructor(JMod.PUBLIC);
    constructor.body().invoke(SUPER).arg(schemaField);
}
Also used : JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JMethod (com.sun.codemodel.JMethod)62 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)17 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