Search in sources :

Example 21 with JMethod

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

the class JavaDataTemplateGenerator method generatePathSpecMethodsForUnion.

private void generatePathSpecMethodsForUnion(UnionTemplateSpec unionSpec, JDefinedClass unionClass) throws JClassAlreadyExistsException {
    final JDefinedClass fieldsNestedClass = generatePathSpecNestedClass(unionClass);
    for (UnionTemplateSpec.Member member : unionSpec.getMembers()) {
        JClass fieldsRefType = _pathSpecClass;
        if (hasNestedFields(member.getSchema())) {
            final JClass unionMemberClass = generate(member.getClassTemplateSpec());
            fieldsRefType = getCodeModel().ref(unionMemberClass.fullName() + ".Fields");
        }
        final JMethod accessorMethod = fieldsNestedClass.method(JMod.PUBLIC, fieldsRefType, CodeUtil.getUnionMemberName(member.getSchema()));
        accessorMethod.body()._return(JExpr._new(fieldsRefType).arg(JExpr.invoke("getPathComponents")).arg(member.getSchema().getUnionMemberKey()));
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) UnionTemplateSpec(com.linkedin.pegasus.generator.spec.UnionTemplateSpec) JMethod(com.sun.codemodel.JMethod)

Example 22 with JMethod

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

the class JavaDataTemplateGenerator method generateRecordFieldAccessors.

private void generateRecordFieldAccessors(JDefinedClass templateClass, RecordTemplateSpec.Field field, JClass type, JVar schemaFieldVar) {
    final RecordDataSchema.Field schemaField = field.getSchemaField();
    final DataSchema fieldSchema = schemaField.getType();
    final boolean isDirect = CodeUtil.isDirectType(fieldSchema);
    final String wrappedOrDirect;
    if (isDirect) {
        wrappedOrDirect = (field.getCustomInfo() == null ? "Direct" : "CustomType");
    } else {
        wrappedOrDirect = "Wrapped";
    }
    final String capitalizedName = CodeUtil.capitalize(schemaField.getName());
    final String fieldFieldName = "FIELD_" + capitalizedName;
    final JFieldVar fieldField = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, RecordDataSchema.Field.class, fieldFieldName);
    fieldField.init(schemaFieldVar.invoke("getField").arg(schemaField.getName()));
    // Generate has method.
    final JMethod has = templateClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "has" + capitalizedName);
    addAccessorDoc(templateClass, has, schemaField, "Existence checker");
    setDeprecatedAnnotationAndJavadoc(has, schemaField);
    final JBlock hasBody = has.body();
    JExpression res = JExpr.invoke("contains").arg(fieldField);
    hasBody._return(res);
    if (_recordFieldRemove) {
        // Generate remove method.
        final String removeName = "remove" + capitalizedName;
        final JMethod remove = templateClass.method(JMod.PUBLIC, getCodeModel().VOID, removeName);
        addAccessorDoc(templateClass, remove, schemaField, "Remover");
        setDeprecatedAnnotationAndJavadoc(remove, schemaField);
        final JBlock removeBody = remove.body();
        removeBody.invoke("remove").arg(fieldField);
    }
    final String getterName = JavaCodeUtil.getGetterName(getCodeModel(), type, capitalizedName);
    if (_recordFieldAccessorWithMode) {
        // Getter method with mode.
        final JMethod getterWithMode = templateClass.method(JMod.PUBLIC, type, getterName);
        addAccessorDoc(templateClass, getterWithMode, schemaField, "Getter");
        setDeprecatedAnnotationAndJavadoc(getterWithMode, schemaField);
        JVar modeParam = getterWithMode.param(_getModeClass, "mode");
        final JBlock getterWithModeBody = getterWithMode.body();
        res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(modeParam);
        getterWithModeBody._return(res);
    }
    // Getter method without mode.
    final JMethod getterWithoutMode = templateClass.method(JMod.PUBLIC, type, getterName);
    addAccessorDoc(templateClass, getterWithoutMode, schemaField, "Getter");
    setDeprecatedAnnotationAndJavadoc(getterWithoutMode, schemaField);
    JCommentPart returnComment = getterWithoutMode.javadoc().addReturn();
    if (schemaField.getOptional()) {
        getterWithoutMode.annotate(Nullable.class);
        returnComment.add("Optional field. Always check for null.");
    } else {
        getterWithoutMode.annotate(Nonnull.class);
        returnComment.add("Required field. Could be null for partial record.");
    }
    final JBlock getterWithoutModeBody = getterWithoutMode.body();
    res = JExpr.invoke("obtain" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type)).arg(_strictGetMode);
    getterWithoutModeBody._return(res);
    // Determine dataClass
    final JClass dataClass = generate(field.getDataClass());
    final String setterName = "set" + capitalizedName;
    if (_recordFieldAccessorWithMode) {
        // Setter method with mode
        final JMethod setterWithMode = templateClass.method(JMod.PUBLIC, templateClass, setterName);
        addAccessorDoc(templateClass, setterWithMode, schemaField, "Setter");
        setDeprecatedAnnotationAndJavadoc(setterWithMode, schemaField);
        JVar param = setterWithMode.param(type, "value");
        JVar modeParam = setterWithMode.param(_setModeClass, "mode");
        JInvocation inv = setterWithMode.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
        dataClassArg(inv, dataClass).arg(param).arg(modeParam);
        setterWithMode.body()._return(JExpr._this());
    }
    // Setter method without mode
    final JMethod setter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
    addAccessorDoc(templateClass, setter, schemaField, "Setter");
    setDeprecatedAnnotationAndJavadoc(setter, schemaField);
    JVar param = setter.param(type, "value");
    param.annotate(Nonnull.class);
    JCommentPart paramDoc = setter.javadoc().addParam(param);
    paramDoc.add("Must not be null. For more control, use setters with mode instead.");
    JInvocation inv = setter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
    dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
    setter.body()._return(JExpr._this());
    // Setter method without mode for unboxified type
    if (!type.unboxify().equals(type)) {
        final JMethod unboxifySetter = templateClass.method(JMod.PUBLIC, templateClass, setterName);
        addAccessorDoc(templateClass, unboxifySetter, schemaField, "Setter");
        setDeprecatedAnnotationAndJavadoc(unboxifySetter, schemaField);
        param = unboxifySetter.param(type.unboxify(), "value");
        inv = unboxifySetter.body().invoke("put" + wrappedOrDirect).arg(fieldField).arg(JExpr.dotclass(type));
        dataClassArg(inv, dataClass).arg(param).arg(_disallowNullSetMode);
        unboxifySetter.body()._return(JExpr._this());
    }
}
Also used : JClass(com.sun.codemodel.JClass) JCommentPart(com.sun.codemodel.JCommentPart) JInvocation(com.sun.codemodel.JInvocation) ByteString(com.linkedin.data.ByteString) JExpression(com.sun.codemodel.JExpression) 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) JFieldVar(com.sun.codemodel.JFieldVar) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 23 with JMethod

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

the class JavaDataTemplateGenerator method generateConstructorWithMap.

private void generateConstructorWithMap(JDefinedClass cls, JClass valueClass) {
    final JMethod argConstructor = cls.constructor(JMod.PUBLIC);
    final JVar m = argConstructor.param(_mapClass.narrow(_stringClass, valueClass), "m");
    argConstructor.body().invoke(THIS).arg(JExpr.invoke("newDataMapOfSize").arg(m.invoke("size")));
    argConstructor.body().invoke("putAll").arg(m);
}
Also used : JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 24 with JMethod

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

the class JavaDataTemplateGenerator method generateConstructorWithCollection.

private void generateConstructorWithCollection(JDefinedClass cls, JClass elementClass) {
    final JMethod argConstructor = cls.constructor(JMod.PUBLIC);
    final JVar c = argConstructor.param(_collectionClass.narrow(elementClass), "c");
    argConstructor.body().invoke(THIS).arg(JExpr._new(_dataListClass).arg(c.invoke("size")));
    argConstructor.body().invoke("addAll").arg(c);
}
Also used : JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 25 with JMethod

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

the class JavaDataTemplateGenerator method generateUnionMemberAccessors.

private void generateUnionMemberAccessors(JDefinedClass unionClass, UnionTemplateSpec.Member member, JClass memberClass, JClass dataClass, JVar schemaField) {
    final DataSchema memberType = member.getSchema();
    final boolean isDirect = CodeUtil.isDirectType(memberType);
    final String wrappedOrDirect;
    if (isDirect) {
        wrappedOrDirect = (member.getCustomInfo() == null ? "Direct" : "CustomType");
    } else {
        wrappedOrDirect = "Wrapped";
    }
    final String memberKey = memberType.getUnionMemberKey();
    final String capitalizedName = CodeUtil.getUnionMemberName(memberType);
    final String memberFieldName = "MEMBER_" + capitalizedName;
    final JFieldVar memberField = unionClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, DataSchema.class, memberFieldName);
    memberField.init(schemaField.invoke("getType").arg(memberKey));
    final String setterName = "set" + capitalizedName;
    // Generate builder.
    final JMethod createMethod = unionClass.method(JMod.PUBLIC | JMod.STATIC, unionClass, "create");
    JVar param = createMethod.param(memberClass, "value");
    final JVar newUnionVar = createMethod.body().decl(unionClass, "newUnion", JExpr._new(unionClass));
    createMethod.body().invoke(newUnionVar, setterName).arg(param);
    createMethod.body()._return(newUnionVar);
    // Is method.
    final JMethod is = unionClass.method(JMod.PUBLIC, getCodeModel().BOOLEAN, "is" + capitalizedName);
    final JBlock isBody = is.body();
    JExpression res = JExpr.invoke("memberIs").arg(memberKey);
    isBody._return(res);
    // Getter method.
    final String getterName = "get" + capitalizedName;
    final JMethod getter = unionClass.method(JMod.PUBLIC, memberClass, getterName);
    final JBlock getterBody = getter.body();
    res = JExpr.invoke("obtain" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass)).arg(memberKey);
    getterBody._return(res);
    // Setter method.
    final JMethod setter = unionClass.method(JMod.PUBLIC, Void.TYPE, setterName);
    param = setter.param(memberClass, "value");
    final JInvocation inv = setter.body().invoke("select" + wrappedOrDirect).arg(memberField).arg(JExpr.dotclass(memberClass));
    dataClassArg(inv, dataClass).arg(memberKey).arg(param);
}
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) JFieldVar(com.sun.codemodel.JFieldVar) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) ByteString(com.linkedin.data.ByteString) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Aggregations

JMethod (com.sun.codemodel.JMethod)62 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)17 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