Search in sources :

Example 41 with JClass

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

the class JavaDataTemplateGenerator method generateArray.

protected void generateArray(JDefinedClass arrayClass, ArrayTemplateSpec arrayDataTemplateSpec) throws JClassAlreadyExistsException {
    final JClass itemJClass = generate(arrayDataTemplateSpec.getItemClass());
    final JClass dataJClass = generate(arrayDataTemplateSpec.getItemDataClass());
    if (CodeUtil.isDirectType(arrayDataTemplateSpec.getSchema().getItems())) {
        arrayClass._extends(_directArrayBaseClass.narrow(itemJClass));
    } else {
        extendWrappingArrayBaseClass(itemJClass, arrayClass);
    }
    /** see {@link #schemaForArrayItemsOrMapValues} */
    final DataSchema bareSchema = new ArrayDataSchema(schemaForArrayItemsOrMapValues(arrayDataTemplateSpec.getCustomInfo(), arrayDataTemplateSpec.getSchema().getItems()));
    final JVar schemaField = generateSchemaField(arrayClass, bareSchema);
    generateConstructorWithNoArg(arrayClass, _dataListClass);
    generateConstructorWithInitialCapacity(arrayClass, _dataListClass);
    generateConstructorWithCollection(arrayClass, itemJClass);
    generateConstructorWithArg(arrayClass, schemaField, _dataListClass, itemJClass, dataJClass);
    if (_pathSpecMethods) {
        generatePathSpecMethodsForCollection(arrayClass, arrayDataTemplateSpec.getSchema(), itemJClass, "items");
    }
    generateCustomClassInitialization(arrayClass, arrayDataTemplateSpec.getCustomInfo());
    if (_copierMethods) {
        generateCopierMethods(arrayClass);
    }
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) JClass(com.sun.codemodel.JClass) JVar(com.sun.codemodel.JVar)

Example 42 with JClass

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

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

Example 44 with JClass

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

the class JavaRequestBuilderGenerator method methodMetadataMapInit.

private JVar methodMetadataMapInit(JDefinedClass facadeClass, ActionSchemaArray resourceActions, ActionSchemaArray entityActions, JBlock staticInit) {
    // CreateMetadata (only for actions right now)
    final JClass MetadataMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, getCodeModel().ref(DynamicRecordMetadata.class));
    final JVar requestMetadataMap = staticInit.decl(MetadataMapClass, "requestMetadataMap").init(JExpr._new(MetadataMapClass));
    final JClass fieldDefListClass = getCodeModel().ref(ArrayList.class).narrow(getCodeModel().ref(FieldDef.class).narrow(getCodeModel().ref(Object.class).wildcard()));
    // get all actions into a single ActionSchemaArray
    final int resourceActionsSize = resourceActions == null ? 0 : resourceActions.size();
    final int entityActionsSize = entityActions == null ? 0 : entityActions.size();
    final ActionSchemaArray allActionSchema = new ActionSchemaArray(resourceActionsSize + entityActionsSize);
    allActionSchema.addAll(resourceActions == null ? new ActionSchemaArray() : resourceActions);
    allActionSchema.addAll(entityActions == null ? new ActionSchemaArray() : entityActions);
    for (ActionSchema actionSchema : allActionSchema) {
        final String varName = actionSchema.getName() + "Params";
        final JVar currMethodParams = staticInit.decl(fieldDefListClass, varName).init(JExpr._new(fieldDefListClass));
        final ParameterSchemaArray parameters = actionSchema.getParameters();
        for (ParameterSchema parameterSchema : parameters == null ? new ParameterSchemaArray() : parameters) {
            final JInvocation fieldDefParam = createFieldDef(parameterSchema.getName(), parameterSchema.getType(), facadeClass);
            staticInit.add(currMethodParams.invoke("add").arg(fieldDefParam));
        }
        final String methodName = actionSchema.getName();
        final JInvocation newSchema = createMetadata(methodName, currMethodParams);
        staticInit.add(requestMetadataMap.invoke("put").arg(methodName).arg(newSchema));
    }
    return requestMetadataMap;
}
Also used : HashMap(java.util.HashMap) JClass(com.sun.codemodel.JClass) ArrayList(java.util.ArrayList) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) JInvocation(com.sun.codemodel.JInvocation) ActionSchema(com.linkedin.restli.restspec.ActionSchema) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray) JVar(com.sun.codemodel.JVar)

Example 45 with JClass

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

the class JavaRequestBuilderGenerator method getKeyClass.

/**
   * Get the key class. In case of collection with a simple key, return the one specified by "type" in the "identifier". Otherwise, get both "type" and "params", and return ComplexKeyResource
   * parameterized by those two.
   *
   * @param collection
   * @param facadeClass
   *
   * @return JClass as described above
   */
private JClass getKeyClass(CollectionSchema collection, JDefinedClass facadeClass) {
    final JClass keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
    if (collection.getIdentifier().getParams() == null) {
        return keyClass;
    }
    final JClass paramsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
    return getCodeModel().ref(ComplexResourceKey.class).narrow(keyClass, paramsClass);
}
Also used : JClass(com.sun.codemodel.JClass) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Aggregations

JClass (com.sun.codemodel.JClass)53 JVar (com.sun.codemodel.JVar)25 JDefinedClass (com.sun.codemodel.JDefinedClass)22 JMethod (com.sun.codemodel.JMethod)20 JInvocation (com.sun.codemodel.JInvocation)15 JBlock (com.sun.codemodel.JBlock)13 Map (java.util.Map)9 JCodeModel (com.sun.codemodel.JCodeModel)8 JFieldVar (com.sun.codemodel.JFieldVar)8 JType (com.sun.codemodel.JType)8 HashMap (java.util.HashMap)8 JExpression (com.sun.codemodel.JExpression)7 JPackage (com.sun.codemodel.JPackage)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 Schema (org.jsonschema2pojo.Schema)4