Search in sources :

Example 16 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendMutableGetters.

/**
 * Get mutable values. This returns message builders for messages, collection
 * builders for collections, and the normal immutable value for everything
 * else.
 *
 * @param message The message to get mutable getters for.
 * @param field The field to generate getter for.
 */
private void appendMutableGetters(JMessage message, JField field) throws GeneratorException {
    if (field.field().getDescriptor() instanceof PPrimitive || field.type() == PType.ENUM) {
        // The other fields will have ordinary non-mutable getters.
        appendGetter(field);
        return;
    }
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Gets the builder for the contained " + field.name() + ".");
    }
    comment.newline().return_("The field builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    if (JAnnotation.isDeprecated(field)) {
        writer.appendln(JAnnotation.DEPRECATED);
    }
    writer.appendln(JAnnotation.NON_NULL);
    switch(field.type()) {
        case MESSAGE:
            {
                writer.formatln("public %s._Builder %s() {", field.instanceType(), field.mutable()).begin();
                if (message.isUnion()) {
                    writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s();", field.resetter()).appendln('}').formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                    writer.appendln("modified = true;");
                } else {
                    writer.formatln("optionals.set(%d);", field.index());
                    writer.formatln("modified.set(%d);", field.index());
                }
                writer.newline().formatln("if (%s != null) {", field.member()).formatln("    %s_builder = %s.mutate();", field.member(), field.member()).formatln("    %s = null;", field.member()).formatln("} else if (%s_builder == null) {", field.member()).formatln("    %s_builder = %s.builder();", field.member(), field.instanceType()).appendln('}').formatln("return %s_builder;", field.member());
                writer.end().appendln('}').newline();
                // Also add "normal" getter for the message field, which will
                // return the message or the builder dependent on which is set.
                // It will not change the state of the builder.
                comment = new BlockCommentBuilder(writer);
                if (field.hasComment()) {
                    comment.comment(field.comment());
                } else {
                    comment.comment("Gets the value for the contained " + field.name() + ".");
                }
                comment.newline().return_("The field value");
                if (JAnnotation.isDeprecated(field)) {
                    String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
                    if (reason != null && reason.trim().length() > 0) {
                        comment.deprecated_(reason);
                    }
                }
                comment.finish();
                if (JAnnotation.isDeprecated(field)) {
                    writer.appendln(JAnnotation.DEPRECATED);
                }
                writer.formatln("public %s %s() {", field.instanceType(), field.getter()).begin();
                if (message.isUnion()) {
                    writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    return null;").appendln('}');
                }
                writer.newline().formatln("if (%s_builder != null) {", field.member()).formatln("    return %s_builder.build();", field.member()).appendln('}').formatln("return %s;", field.member());
                writer.end().appendln('}').newline();
                break;
            }
        case SET:
        case LIST:
        case MAP:
            writer.formatln("public %s %s() {", field.fieldType(), field.mutable()).begin();
            if (message.isUnion()) {
                writer.formatln("if (%s != _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s();", field.resetter()).appendln('}').formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                writer.appendln("modified = true;");
            } else {
                writer.formatln("optionals.set(%d);", field.index());
                writer.formatln("modified.set(%d);", field.index());
            }
            writer.newline().formatln("if (%s == null) {", field.member()).formatln("    %s = new %s<>();", field.member(), field.builderMutableType()).formatln("} else if (!(%s instanceof %s)) {", field.member(), field.builderMutableType()).formatln("    %s = new %s<>(%s);", field.member(), field.builderMutableType(), field.member()).appendln("}");
            writer.formatln("return %s;", field.member());
            writer.end().appendln('}').newline();
            break;
        default:
            throw new GeneratorException("Unexpected field type: " + field.type());
    }
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) GeneratorException(net.morimekta.providence.generator.GeneratorException) PPrimitive(net.morimekta.providence.descriptor.PPrimitive)

Example 17 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendAdder.

private void appendAdder(JMessage message, JField field) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else if (field.type() == PType.MAP) {
        comment.comment("Adds a mapping to " + field.name() + ".");
    } else {
        comment.comment("Adds entries to " + field.name() + ".");
    }
    comment.newline();
    if (field.type() == PType.MAP) {
        comment.param_("key", "The inserted key").param_("value", "The inserted value");
    } else {
        comment.param_("values", "The added value");
    }
    comment.return_("The builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    if (JAnnotation.isDeprecated(field)) {
        writer.appendln(JAnnotation.DEPRECATED);
    }
    writer.appendln(JAnnotation.NON_NULL);
    switch(field.type()) {
        case MAP:
            {
                PMap<?, ?> mType = (PMap<?, ?>) field.field().getDescriptor();
                String mkType = helper.getValueType(mType.keyDescriptor());
                String miType = helper.getValueType(mType.itemDescriptor());
                writer.formatln("public _Builder %s(%s key, %s value) {", field.adder(), mkType, miType).begin();
                if (message.isUnion()) {
                    writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                    writer.appendln("modified = true;");
                } else {
                    writer.formatln("optionals.set(%d);", field.index());
                    writer.formatln("modified.set(%d);", field.index());
                }
                writer.formatln("%s().put(key, value);", field.mutable()).appendln("return this;").end().appendln('}').newline();
                break;
            }
        case SET:
        case LIST:
            {
                PContainer<?> lType = (PContainer<?>) field.field().getDescriptor();
                String liType = helper.getValueType(lType.itemDescriptor());
                writer.formatln("public _Builder %s(%s... values) {", field.adder(), liType).begin();
                if (message.isUnion()) {
                    writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                    writer.appendln("modified = true;");
                } else {
                    writer.formatln("optionals.set(%d);", field.index());
                    writer.formatln("modified.set(%d);", field.index());
                }
                writer.formatln("%s _container = %s();", field.fieldType(), field.mutable()).formatln("for (%s item : values) {", liType).begin().appendln("_container.add(item);").end().appendln('}').appendln("return this;").end().appendln('}').newline();
                break;
            }
    }
}
Also used : PContainer(net.morimekta.providence.descriptor.PContainer) PMap(net.morimekta.providence.descriptor.PMap) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 18 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendSetter.

private void appendSetter(JMessage message, JField field) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Sets the value of " + field.name() + ".");
    }
    comment.newline();
    if (!field.isVoid()) {
        comment.param_("value", "The new value");
    }
    comment.return_("The builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    if (JAnnotation.isDeprecated(field)) {
        writer.appendln(JAnnotation.DEPRECATED);
    }
    writer.appendln(JAnnotation.NON_NULL);
    if (field.isVoid()) {
        // Void fields have no value.
        writer.formatln("public _Builder %s() {", field.setter());
    } else if (field.type() == PType.SET || field.type() == PType.LIST) {
        PContainer<?> cType = (PContainer<?>) field.field().getDescriptor();
        String iType = helper.getFieldType(cType.itemDescriptor());
        writer.formatln("public _Builder %s(%s<%s> value) {", field.setter(), Collection.class.getName(), iType);
    } else {
        writer.formatln("public _Builder %s(%s value) {", field.setter(), field.valueType());
    }
    writer.begin();
    if (!field.isPrimitiveJavaValue()) {
        writer.formatln("if (value == null) {").formatln("    return %s();", field.resetter()).appendln('}').newline();
    }
    if (message.isUnion()) {
        writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
        writer.appendln("modified = true;");
    } else {
        writer.formatln("optionals.set(%d);", field.index());
        writer.formatln("modified.set(%d);", field.index());
    }
    switch(field.type()) {
        case VOID:
            // Void fields have no value.
            break;
        case SET:
        case LIST:
        case MAP:
            writer.formatln("%s = %s;", field.member(), field.fieldInstanceCopy("value"));
            break;
        case MESSAGE:
            writer.formatln("%s = value;", field.member());
            writer.formatln("%s_builder = null;", field.member());
            break;
        default:
            writer.formatln("%s = value;", field.member());
            break;
    }
    writer.appendln("return this;").end().appendln('}').newline();
    if (field.type() == MESSAGE) {
        appendSetterBuilderOverload(field);
    }
}
Also used : PContainer(net.morimekta.providence.descriptor.PContainer) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 19 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendSetterBuilderOverload.

private void appendSetterBuilderOverload(JField field) {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Sets the value of " + field.name() + ".");
    }
    comment.newline();
    if (!field.isVoid()) {
        comment.param_("builder", "builder for the new value");
    }
    comment.return_("The builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    if (JAnnotation.isDeprecated(field)) {
        writer.appendln(JAnnotation.DEPRECATED);
    }
    writer.appendln(JAnnotation.NON_NULL);
    writer.formatln("public _Builder %s(%s builder) {", field.setter(), field.builderMutableType());
    writer.formatln("  return %s(builder == null ? null : builder.build());", field.setter());
    writer.formatln("}");
    writer.newline();
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 20 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class CommonBuilderFormatter method appendStaticMakeBuilder.

private void appendStaticMakeBuilder(JMessage message) {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    comment.comment("Make a " + message.descriptor().getQualifiedName() + " builder.").return_("The builder instance.").finish();
    writer.formatln("public static _Builder builder() {").begin().appendln("return new _Builder();").end().appendln('}').newline();
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Aggregations

BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)25 JField (net.morimekta.providence.generator.format.java.utils.JField)8 Generated (javax.annotation.Generated)3 GeneratorException (net.morimekta.providence.generator.GeneratorException)3 LinkedHashSet (java.util.LinkedHashSet)2 PContainer (net.morimekta.providence.descriptor.PContainer)2 JMessage (net.morimekta.providence.generator.format.java.utils.JMessage)2 JService (net.morimekta.providence.generator.format.java.utils.JService)2 JServiceMethod (net.morimekta.providence.generator.format.java.utils.JServiceMethod)2 CService (net.morimekta.providence.reflect.contained.CService)2 Config (com.hazelcast.config.Config)1 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)1 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)1 Portable (com.hazelcast.nio.serialization.Portable)1 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Optional (java.util.Optional)1