Search in sources :

Example 51 with JMethod

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

the class JavaDataTemplateGenerator method overrideCopierMethod.

private static void overrideCopierMethod(JDefinedClass templateClass, String methodName) {
    final JMethod copierMethod = templateClass.method(JMod.PUBLIC, templateClass, methodName);
    copierMethod.annotate(Override.class);
    copierMethod._throws(CloneNotSupportedException.class);
    copierMethod.body()._return(JExpr.cast(templateClass, JExpr._super().invoke(methodName)));
}
Also used : JMethod(com.sun.codemodel.JMethod)

Example 52 with JMethod

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

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

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

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

the class JavaDataTemplateGenerator method generateConstructorWithInitialCapacity.

private void generateConstructorWithInitialCapacity(JDefinedClass cls, JClass elementClass) {
    final JMethod argConstructor = cls.constructor(JMod.PUBLIC);
    final JVar initialCapacity = argConstructor.param(getCodeModel().INT, "initialCapacity");
    argConstructor.body().invoke(THIS).arg(JExpr._new(elementClass).arg(initialCapacity));
}
Also used : JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

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