Search in sources :

Example 1 with TyperefTemplateSpec

use of com.linkedin.pegasus.generator.spec.TyperefTemplateSpec in project rest.li by linkedin.

the class TemplateSpecGenerator method generateTyperef.

private TyperefTemplateSpec generateTyperef(TyperefDataSchema schema, TyperefDataSchema originalTyperefSchema) {
    final TyperefTemplateSpec typerefClass = new TyperefTemplateSpec(schema);
    typerefClass.setOriginalTyperefSchema(originalTyperefSchema);
    typerefClass.setNamespace(schema.getNamespace());
    typerefClass.setPackage(schema.getPackage());
    typerefClass.setClassName(schema.getName());
    typerefClass.setModifiers(ModifierSpec.PUBLIC);
    registerClassTemplateSpec(schema, typerefClass);
    return typerefClass;
}
Also used : TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)

Example 2 with TyperefTemplateSpec

use of com.linkedin.pegasus.generator.spec.TyperefTemplateSpec in project rest.li by linkedin.

the class JavaDataTemplateGenerator method defineClass.

private JDefinedClass defineClass(ClassTemplateSpec classTemplateSpec) throws JClassAlreadyExistsException {
    JDefinedClass result = _definedClasses.get(classTemplateSpec);
    if (result == null) {
        final int jmodValue = getJModValue(classTemplateSpec.getModifiers());
        final JClassContainer container;
        if (classTemplateSpec.getEnclosingClass() == null) {
            container = getPackage(classTemplateSpec.getPackage());
        } else {
            container = defineClass(classTemplateSpec.getEnclosingClass());
        }
        if (classTemplateSpec instanceof ArrayTemplateSpec || classTemplateSpec instanceof FixedTemplateSpec || classTemplateSpec instanceof MapTemplateSpec || classTemplateSpec instanceof RecordTemplateSpec || classTemplateSpec instanceof TyperefTemplateSpec || classTemplateSpec instanceof UnionTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()));
        } else if (classTemplateSpec instanceof EnumTemplateSpec) {
            result = container._class(jmodValue, escapeReserved(classTemplateSpec.getClassName()), ClassType.ENUM);
        } else {
            throw new RuntimeException();
        }
        _definedClasses.put(classTemplateSpec, result);
    }
    return result;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) FixedTemplateSpec(com.linkedin.pegasus.generator.spec.FixedTemplateSpec) MapTemplateSpec(com.linkedin.pegasus.generator.spec.MapTemplateSpec) EnumTemplateSpec(com.linkedin.pegasus.generator.spec.EnumTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) RecordTemplateSpec(com.linkedin.pegasus.generator.spec.RecordTemplateSpec) JClassContainer(com.sun.codemodel.JClassContainer) ArrayTemplateSpec(com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)

Example 3 with TyperefTemplateSpec

use of com.linkedin.pegasus.generator.spec.TyperefTemplateSpec 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 4 with TyperefTemplateSpec

use of com.linkedin.pegasus.generator.spec.TyperefTemplateSpec in project rest.li by linkedin.

the class TemplateSpecGenerator method generateUnion.

private ClassTemplateSpec generateUnion(UnionDataSchema schema, TyperefDataSchema typerefDataSchema) {
    assert typerefDataSchema.getRef() == schema;
    pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(typerefDataSchema.getFullName()));
    final UnionTemplateSpec unionClass = new UnionTemplateSpec(schema);
    unionClass.setNamespace(typerefDataSchema.getNamespace());
    unionClass.setPackage(typerefDataSchema.getPackage());
    unionClass.setClassName(typerefDataSchema.getName());
    unionClass.setModifiers(ModifierSpec.PUBLIC);
    registerClassTemplateSpec(typerefDataSchema, unionClass);
    final TyperefTemplateSpec typerefInfoClass = new TyperefTemplateSpec(typerefDataSchema);
    typerefInfoClass.setEnclosingClass(unionClass);
    typerefInfoClass.setClassName("UnionTyperefInfo");
    typerefInfoClass.setModifiers(ModifierSpec.PRIVATE, ModifierSpec.STATIC, ModifierSpec.FINAL);
    final UnionTemplateSpec result = generateUnion(schema, unionClass);
    result.setTyperefClass(typerefInfoClass);
    popCurrentLocation();
    return result;
}
Also used : UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) TyperefTemplateSpec(com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)

Aggregations

TyperefTemplateSpec (com.linkedin.pegasus.generator.spec.TyperefTemplateSpec)4 UnionTemplateSpec (com.linkedin.pegasus.generator.spec.UnionTemplateSpec)3 JDefinedClass (com.sun.codemodel.JDefinedClass)2 ArrayTemplateSpec (com.linkedin.pegasus.generator.spec.ArrayTemplateSpec)1 EnumTemplateSpec (com.linkedin.pegasus.generator.spec.EnumTemplateSpec)1 FixedTemplateSpec (com.linkedin.pegasus.generator.spec.FixedTemplateSpec)1 MapTemplateSpec (com.linkedin.pegasus.generator.spec.MapTemplateSpec)1 RecordTemplateSpec (com.linkedin.pegasus.generator.spec.RecordTemplateSpec)1 JClassContainer (com.sun.codemodel.JClassContainer)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JMethod (com.sun.codemodel.JMethod)1 JVar (com.sun.codemodel.JVar)1