Search in sources :

Example 16 with JField

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

the class BuilderCommonMemberFormatter method appendMutateConstructor.

private void appendMutateConstructor(JMessage<?> message) {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    comment.comment("Make a mutating builder off a base " + message.descriptor().getQualifiedName() + ".").newline().param_("base", "The base " + message.descriptor().getName()).finish();
    writer.formatln("public _Builder(%s base) {", message.instanceType()).begin().appendln("this();").newline();
    if (message.isUnion()) {
        writer.formatln("%s = base.%s;", UNION_FIELD, UNION_FIELD).newline();
    }
    for (JField field : message.declaredOrderFields()) {
        boolean checkPresence = message.isUnion() ? field.container() : !field.alwaysPresent();
        if (checkPresence) {
            writer.formatln("if (base.%s()) {", field.presence()).begin();
        }
        if (!message.isUnion()) {
            writer.formatln("optionals.set(%d);", field.index());
        }
        if (field.type() != PType.VOID) {
            writer.formatln("%s = base.%s;", field.member(), field.member());
        }
        if (checkPresence) {
            writer.end().appendln('}');
        }
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 17 with JField

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

the class BuilderCommonMemberFormatter method appendEquals.

private void appendEquals(JMessage<?> message) {
    writer.appendln("@Override").appendln("public boolean equals(Object o) {").begin().appendln("if (o == this) return true;").appendln("if (o == null || !o.getClass().equals(getClass())) return false;");
    if (message.numericalOrderFields().size() > 0) {
        writer.formatln("%s._Builder other = (%s._Builder) o;", message.instanceType(), message.instanceType()).appendln("return ");
        if (message.isUnion()) {
            writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), UNION_FIELD, UNION_FIELD);
        } else {
            writer.format("%s.equals(optionals, other.optionals)", Objects.class.getName());
        }
        for (JField field : message.declaredOrderFields()) {
            if (field.isVoid())
                continue;
            writer.append(" &&").appendln("       ");
            if (field.type() == MESSAGE) {
                writer.format("%s.equals(%s(), other.%s())", Objects.class.getName(), field.getter(), field.getter());
            } else {
                writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), field.member(), field.member());
            }
        }
        writer.append(';');
    } else {
        writer.appendln("return true;");
    }
    writer.end().appendln("}").newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) Objects(java.util.Objects)

Example 18 with JField

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

the class BuilderCommonMemberFormatter method appendMethods.

@Override
public void appendMethods(JMessage<?> message) throws GeneratorException {
    for (JField field : message.declaredOrderFields()) {
        appendSetter(message, field);
        if (field.container()) {
            appendAdder(message, field);
        }
        appendIsSet(message, field);
        if (!message.isUnion()) {
            appendIsModified(message, field);
        }
        appendResetter(message, field);
        appendMutableGetters(message, field);
    }
    if (message.isUnion()) {
        appendIsUnionModified(message);
    }
    if (message.isException()) {
        appendInitCause(message);
    }
    appendEquals(message);
    appendHashCode(message);
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField)

Example 19 with JField

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

the class BuilderCoreOverridesFormatter method appendMerge.

private void appendMerge(JMessage<?> message) throws GeneratorException {
    writer.appendln(JAnnotation.NON_NULL).appendln("@Override").formatln("public _Builder merge(%s from) {", message.instanceType()).begin();
    if (message.isUnion()) {
        writer.appendln("if (!from.unionFieldIsSet()) {").appendln("    return this;").appendln("}").newline().appendln("switch (from.unionField()) {").begin();
        for (JField field : message.declaredOrderFields()) {
            writer.formatln("case %s: {", field.fieldEnum()).begin();
            switch(field.type()) {
                case VOID:
                    // Void fields have no value.
                    writer.formatln("%s = _Field.%s;", UNION_FIELD, field.fieldEnum());
                    break;
                case MESSAGE:
                    writer.formatln("if (%s == _Field.%s && %s != null) {", UNION_FIELD, field.fieldEnum(), field.member()).formatln("    %s = %s.mutate().merge(from.%s()).build();", field.member(), field.member(), field.getter()).appendln("} else {").formatln("    %s(from.%s());", field.setter(), field.getter()).appendln('}');
                    break;
                case SET:
                    writer.formatln("if (%s == _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s().addAll(from.%s());", field.mutable(), field.getter()).appendln("} else {").formatln("    %s(from.%s());", field.setter(), field.getter()).appendln('}');
                    break;
                case MAP:
                    writer.formatln("if (%s == _Field.%s) {", UNION_FIELD, field.fieldEnum()).formatln("    %s().putAll(from.%s());", field.mutable(), field.getter()).appendln("} else {").formatln("    %s(from.%s());", field.setter(), field.getter()).appendln('}');
                    break;
                default:
                    writer.formatln("%s(from.%s());", field.setter(), field.getter());
                    break;
            }
            writer.appendln("break;").end().appendln('}');
        }
        writer.end().appendln('}');
    } else {
        boolean first = true;
        for (JField field : message.declaredOrderFields()) {
            if (first) {
                first = false;
            } else {
                writer.newline();
            }
            if (!field.alwaysPresent()) {
                writer.formatln("if (from.%s()) {", field.presence()).begin();
            }
            writer.formatln("optionals.set(%d);", field.index());
            writer.formatln("modified.set(%d);", field.index());
            switch(field.type()) {
                case MESSAGE:
                    // Message fields are merged.
                    writer.formatln("if (%s_builder != null) {", field.member()).formatln("    %s_builder.merge(from.%s());", field.member(), field.getter()).formatln("} else if (%s != null) {", field.member()).formatln("    %s_builder = %s.mutate().merge(from.%s());", field.member(), field.member(), field.getter()).formatln("    %s = null;", field.member()).appendln("} else {").formatln("    %s = from.%s();", field.member(), field.getter()).appendln('}');
                    break;
                case SET:
                    writer.formatln("%s().addAll(from.%s());", field.mutable(), field.getter());
                    break;
                case MAP:
                    writer.formatln("%s().putAll(from.%s());", field.mutable(), field.getter());
                    break;
                default:
                    writer.formatln("%s = from.%s();", field.member(), field.getter());
                    break;
            }
            if (!field.alwaysPresent()) {
                writer.end().appendln('}');
            }
        }
    }
    writer.appendln("return this;").end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField)

Example 20 with JField

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

the class BuilderCoreOverridesFormatter method appendOverrideIsValid.

private void appendOverrideIsValid(JMessage<?> message) {
    writer.appendln("@Override").appendln("public boolean valid() {").begin();
    if (message.isUnion()) {
        writer.formatln("if (%s == null) {", UNION_FIELD).appendln("    return false;").appendln('}').newline().formatln("switch (%s) {", UNION_FIELD).begin();
        message.numericalOrderFields().stream().filter(field -> !field.alwaysPresent()).forEachOrdered(field -> {
            if (field.isVoid()) {
                // Void fields have no value.
                writer.formatln("case %s: return true;", field.fieldEnum());
            } else if (field.type() == PType.MESSAGE) {
                writer.formatln("case %s: return %s != null || %s_builder != null;", field.fieldEnum(), field.member(), field.member());
            } else {
                writer.formatln("case %s: return %s != null;", field.fieldEnum(), field.member());
            }
        });
        writer.appendln("default: return true;").end().appendln('}');
    } else {
        writer.appendln("return ").begin("       ");
        boolean first = true;
        for (JField field : message.declaredOrderFields()) {
            if (field.isRequired()) {
                if (first) {
                    first = false;
                } else {
                    writer.append(" &&").appendln("");
                }
                writer.format("optionals.get(%d)", field.index());
            }
        }
        if (first) {
            writer.append("true");
        }
        // alignment indent
        writer.end().append(';');
    }
    writer.end().appendln('}').newline();
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) MessageMemberFormatter(net.morimekta.providence.generator.format.java.shared.MessageMemberFormatter) GeneratorException(net.morimekta.providence.generator.GeneratorException) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) PContainer(net.morimekta.providence.descriptor.PContainer) JMessage(net.morimekta.providence.generator.format.java.utils.JMessage) ArrayList(java.util.ArrayList) PType(net.morimekta.providence.PType) UNION_FIELD(net.morimekta.providence.generator.format.java.messages.CoreOverridesFormatter.UNION_FIELD) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) Strings(net.morimekta.util.Strings) PMessageBuilder(net.morimekta.providence.PMessageBuilder) JAnnotation(net.morimekta.providence.generator.format.java.utils.JAnnotation) JField(net.morimekta.providence.generator.format.java.utils.JField) JField(net.morimekta.providence.generator.format.java.utils.JField)

Aggregations

JField (net.morimekta.providence.generator.format.java.utils.JField)31 BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)7 IOException (java.io.IOException)3 JServiceMethod (net.morimekta.providence.generator.format.java.utils.JServiceMethod)3 Strings (net.morimekta.util.Strings)3 Objects (java.util.Objects)2 PApplicationExceptionType (net.morimekta.providence.PApplicationExceptionType)2 PServiceCall (net.morimekta.providence.PServiceCall)2 PField (net.morimekta.providence.descriptor.PField)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)1 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 PApplicationException (net.morimekta.providence.PApplicationException)1 PClient (net.morimekta.providence.PClient)1 PMessage (net.morimekta.providence.PMessage)1