Search in sources :

Example 16 with JInvocation

use of com.sun.codemodel.JInvocation 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 17 with JInvocation

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

the class EnumRule method addQuickLookupMap.

private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {
    JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum);
    JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS");
    JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum);
    lookupMap.init(JExpr._new(lookupImplType));
    JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
    JInvocation put = forEach.body().invoke(lookupMap, "put");
    put.arg(forEach.var().ref("value"));
    put.arg(forEach.var());
    return lookupMap;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) HashMap(java.util.HashMap) JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) JForEach(com.sun.codemodel.JForEach) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with JInvocation

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

the class EnumRule method addFactoryMethod.

private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
    JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
    JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
    JVar valueParam = fromValue.param(backingType, "value");
    JBlock body = fromValue.body();
    JVar constant = body.decl(_enum, "constant");
    constant.init(quickLookupMap.invoke("get").arg(valueParam));
    JConditional _if = body._if(constant.eq(JExpr._null()));
    JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
    JExpression expr = valueParam;
    // if string no need to add ""
    if (!isString(backingType)) {
        expr = expr.plus(JExpr.lit(""));
    }
    illegalArgumentException.arg(expr);
    _if._then()._throw(illegalArgumentException);
    _if._else()._return(constant);
    ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 19 with JInvocation

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

the class AdditionalPropertiesRule method addSetter.

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

Example 20 with JInvocation

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

the class DefaultRule method getDefaultValue.

private JExpression getDefaultValue(JType fieldType, JsonNode node) {
    fieldType = fieldType.unboxify();
    if (fieldType.fullName().equals(String.class.getName())) {
        return JExpr.lit(node.asText());
    } else if (fieldType.fullName().equals(int.class.getName())) {
        return JExpr.lit(Integer.parseInt(node.asText()));
    } else if (fieldType.fullName().equals(BigInteger.class.getName())) {
        return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
    } else if (fieldType.fullName().equals(double.class.getName())) {
        return JExpr.lit(Double.parseDouble(node.asText()));
    } else if (fieldType.fullName().equals(BigDecimal.class.getName())) {
        return JExpr._new(fieldType).arg(JExpr.lit(node.asText()));
    } else if (fieldType.fullName().equals(boolean.class.getName())) {
        return JExpr.lit(Boolean.parseBoolean(node.asText()));
    } else if (fieldType.fullName().equals(getDateTimeType().getName())) {
        long millisecs = parseDateToMillisecs(node.asText());
        JInvocation newDateTime = JExpr._new(fieldType);
        newDateTime.arg(JExpr.lit(millisecs));
        return newDateTime;
    } else if (fieldType.fullName().equals(LocalDate.class.getName()) || fieldType.fullName().equals(LocalTime.class.getName())) {
        JInvocation stringParseableTypeInstance = JExpr._new(fieldType);
        stringParseableTypeInstance.arg(JExpr.lit(node.asText()));
        return stringParseableTypeInstance;
    } else if (fieldType.fullName().equals(long.class.getName())) {
        return JExpr.lit(Long.parseLong(node.asText()));
    } else if (fieldType.fullName().equals(float.class.getName())) {
        return JExpr.lit(Float.parseFloat(node.asText()));
    } else if (fieldType.fullName().equals(URI.class.getName())) {
        JInvocation invokeCreate = fieldType.owner().ref(URI.class).staticInvoke("create");
        return invokeCreate.arg(JExpr.lit(node.asText()));
    } else if (fieldType instanceof JDefinedClass && ((JDefinedClass) fieldType).getClassType().equals(ClassType.ENUM)) {
        return getDefaultEnum(fieldType, node);
    } else {
        return JExpr._null();
    }
}
Also used : LocalTime(org.joda.time.LocalTime) JDefinedClass(com.sun.codemodel.JDefinedClass) BigInteger(java.math.BigInteger) JInvocation(com.sun.codemodel.JInvocation) LocalDate(org.joda.time.LocalDate) URI(java.net.URI) BigDecimal(java.math.BigDecimal)

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