Search in sources :

Example 31 with JDefinedClass

use of com.sun.codemodel.JDefinedClass 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)

Example 32 with JDefinedClass

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

the class JavaDataTemplateGenerator method generatePathSpecMethodsForCollection.

private void generatePathSpecMethodsForCollection(JDefinedClass templateClass, DataSchema schema, JClass childClass, String wildcardMethodName) throws JClassAlreadyExistsException {
    if (hasNestedFields(schema)) {
        final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(templateClass);
        final JClass itemsFieldType = getCodeModel().ref(childClass.fullName() + ".Fields");
        final JMethod constantField = fieldsNestedClass.method(JMod.PUBLIC, itemsFieldType, wildcardMethodName);
        constantField.body()._return(JExpr._new(itemsFieldType).arg(JExpr.invoke("getPathComponents")).arg(_pathSpecClass.staticRef("WILDCARD")));
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JMethod(com.sun.codemodel.JMethod)

Example 33 with JDefinedClass

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

the class JavaDataTemplateGenerator method generatePathSpecMethodsForRecord.

private void generatePathSpecMethodsForRecord(List<RecordTemplateSpec.Field> fieldSpecs, JDefinedClass templateClass) throws JClassAlreadyExistsException {
    final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(templateClass);
    for (RecordTemplateSpec.Field field : fieldSpecs) {
        JClass fieldsRefType = _pathSpecClass;
        if (hasNestedFields(field.getSchemaField().getType())) {
            final JClass fieldType = generate(field.getType());
            fieldsRefType = getCodeModel().ref(fieldType.fullName() + ".Fields");
        }
        final JMethod constantField = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, escapeReserved(field.getSchemaField().getName()));
        constantField.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(field.getSchemaField().getName()));
        if (!field.getSchemaField().getDoc().isEmpty()) {
            constantField.javadoc().append(field.getSchemaField().getDoc());
        }
        setDeprecatedAnnotationAndJavadoc(constantField, field.getSchemaField());
    }
    final JVar staticFields = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, fieldsNestedClass, "_fields").init(JExpr._new(fieldsNestedClass));
    final JMethod staticFieldsAccessor = templateClass.method(JMod.PUBLIC | JMod.STATIC, fieldsNestedClass, "fields");
    staticFieldsAccessor.body()._return(staticFields);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JMethod(com.sun.codemodel.JMethod) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) JVar(com.sun.codemodel.JVar)

Example 34 with JDefinedClass

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

the class JavaDataTemplateGenerator method generateUnion.

protected void generateUnion(JDefinedClass unionClass, UnionTemplateSpec unionSpec) throws JClassAlreadyExistsException {
    extendUnionBaseClass(unionClass);
    final JVar schemaField = generateSchemaField(unionClass, unionSpec.getSchema());
    generateConstructorWithNoArg(unionClass, schemaField, _dataMapClass);
    generateConstructorWithObjectArg(unionClass, schemaField);
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        if (member.getClassTemplateSpec() != null) {
            generateUnionMemberAccessors(unionClass, member, generate(member.getClassTemplateSpec()), generate(member.getDataClass()), schemaField);
        }
    }
    unionSpec.getMembers().stream().map(UnionTemplateSpec.Member::getCustomInfo).distinct().forEach(customInfo -> generateCustomClassInitialization(unionClass, customInfo));
    if (_pathSpecMethods) {
        generatePathSpecMethodsForUnion(unionSpec, unionClass);
    }
    if (_copierMethods) {
        generateCopierMethods(unionClass);
    }
    if (unionSpec.getTyperefClass() != null) {
        final TyperefTemplateSpec typerefClassSpec = unionSpec.getTyperefClass();
        final JDefinedClass typerefInfoClass = unionClass._class(getJModValue(typerefClassSpec.getModifiers()), escapeReserved(typerefClassSpec.getClassName()));
        generateTyperef(typerefInfoClass, typerefClassSpec);
        final JFieldVar typerefInfoField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, TyperefInfo.class, DataTemplateUtil.TYPEREFINFO_FIELD_NAME);
        typerefInfoField.init(JExpr._new(typerefInfoClass));
        unionClass._implements(HasTyperefInfo.class);
        final JMethod typerefInfoMethod = unionClass.method(JMod.PUBLIC, TyperefInfo.class, "typerefInfo");
        typerefInfoMethod.body()._return(typerefInfoField);
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 35 with JDefinedClass

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

the class JavaDataTemplateGenerator method generate.

public JClass generate(ClassTemplateSpec classTemplateSpec) {
    final JClass result;
    if (classTemplateSpec == null) {
        result = null;
    } else {
        if (classTemplateSpec.getSchema() == null) {
            // this is for custom class, package override is not applicable.
            result = getCodeModel().directClass(classTemplateSpec.getFullName());
        } else if (PredefinedJavaClasses.containsKey(classTemplateSpec.getSchema())) {
            final Class<?> nativeJavaClass = PredefinedJavaClasses.get(classTemplateSpec.getSchema());
            result = getCodeModel().ref(nativeJavaClass);
        } else if (classTemplateSpec.getSchema().isPrimitive()) {
            result = generatePrimitive((PrimitiveTemplateSpec) classTemplateSpec);
        } else {
            try {
                final JDefinedClass definedClass = defineClass(classTemplateSpec);
                populateClassContent(classTemplateSpec, definedClass);
                result = definedClass;
            } catch (JClassAlreadyExistsException e) {
                throw new IllegalArgumentException(classTemplateSpec.getFullName());
            }
        }
    }
    return result;
}
Also used : JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) PrimitiveTemplateSpec(com.linkedin.pegasus.generator.spec.PrimitiveTemplateSpec) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass)

Aggregations

JDefinedClass (com.sun.codemodel.JDefinedClass)43 JClass (com.sun.codemodel.JClass)23 JMethod (com.sun.codemodel.JMethod)17 JVar (com.sun.codemodel.JVar)13 JCodeModel (com.sun.codemodel.JCodeModel)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 JType (com.sun.codemodel.JType)9 Test (org.junit.Test)9 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)8 Map (java.util.Map)8 JBlock (com.sun.codemodel.JBlock)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 JExpression (com.sun.codemodel.JExpression)5 JFieldVar (com.sun.codemodel.JFieldVar)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ArrayList (java.util.ArrayList)4 DataMap (com.linkedin.data.DataMap)3 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3