Search in sources :

Example 41 with JMethod

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

the class ObjectRule method addEquals.

private void addEquals(JDefinedClass jclass) {
    Map<String, JFieldVar> fields = jclass.fields();
    JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
    JVar otherObject = equals.param(Object.class, "other");
    Class<?> equalsBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.EqualsBuilder.class : org.apache.commons.lang.builder.EqualsBuilder.class;
    JBlock body = equals.body();
    body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
    body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
    JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
    JClass equalsBuilderClass = jclass.owner().ref(equalsBuilder);
    JInvocation equalsBuilderInvocation = JExpr._new(equalsBuilderClass);
    if (!jclass._extends().fullName().equals(Object.class.getName())) {
        equalsBuilderInvocation = equalsBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject));
    }
    for (JFieldVar fieldVar : fields.values()) {
        if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        equalsBuilderInvocation = equalsBuilderInvocation.invoke("append").arg(fieldVar).arg(rhsVar.ref(fieldVar.name()));
    }
    JInvocation reflectionEquals = jclass.owner().ref(equalsBuilder).staticInvoke("reflectionEquals");
    reflectionEquals.arg(JExpr._this());
    reflectionEquals.arg(otherObject);
    body._return(equalsBuilderInvocation.invoke("isEquals"));
    equals.annotate(Override.class);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 42 with JMethod

use of com.sun.codemodel.JMethod 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 43 with JMethod

use of com.sun.codemodel.JMethod 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 44 with JMethod

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

the class DynamicPropertiesRule method addWithMethods.

private void addWithMethods(JDefinedClass jclass) {
    JMethod internalSetMethod = getInternalSetMethod(jclass);
    addPublicWithMethod(jclass, internalSetMethod);
}
Also used : JMethod(com.sun.codemodel.JMethod)

Example 45 with JMethod

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

the class EnumRule method addValueMethod.

private void addValueMethod(JDefinedClass _enum, JFieldVar valueField) {
    JMethod fromValue = _enum.method(JMod.PUBLIC, valueField.type(), "value");
    JBlock body = fromValue.body();
    body._return(JExpr._this().ref(valueField));
    ruleFactory.getAnnotator().enumValueMethod(fromValue);
}
Also used : JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod)

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