Search in sources :

Example 21 with JField

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

the class BuilderCoreOverridesFormatter method appendOverrideIsModified.

private void appendOverrideIsModified(JMessage<?> message) throws GeneratorException {
    writer.appendln("@Override").appendln("public boolean isModified(int key) {").begin();
    if (!message.isUnion()) {
        writer.appendln("switch (key) {").begin();
        for (JField field : message.numericalOrderFields()) {
            writer.formatln("case %d: return modified.get(%d);", field.id(), field.index());
        }
        writer.appendln("default: break;").end().appendln('}').appendln("return false;");
    } else {
        writer.appendln("return modified;");
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField)

Example 22 with JField

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

the class CommonBuilderFormatter method appendBuilderConstructors.

private void appendBuilderConstructors(JMessage<?> message) throws GeneratorException {
    writer.formatln("private %s(_Builder builder) {", message.instanceType()).begin();
    if (message.isUnion()) {
        writer.formatln("%s = builder.%s;", UNION_FIELD, UNION_FIELD).newline();
        for (JField field : message.declaredOrderFields()) {
            switch(field.type()) {
                case VOID:
                    // Void fields have no value.
                    break;
                case LIST:
                case MAP:
                case SET:
                    writer.formatln("%s = %s == _Field.%s ? %s : null;", field.member(), UNION_FIELD, field.fieldEnum(), field.fieldInstanceCopy("builder." + field.member()), field.member());
                    break;
                case MESSAGE:
                    writer.formatln("%s = %s != _Field.%s", field.member(), UNION_FIELD, field.fieldEnum()).appendln("        ? null").formatln("        : builder.%s_builder != null ? builder.%s_builder.build() : builder.%s;", field.member(), field.member(), field.member());
                    break;
                default:
                    if (field.alwaysPresent()) {
                        writer.formatln("%s = %s == _Field.%s ? builder.%s : %s;", field.member(), UNION_FIELD, field.fieldEnum(), field.member(), field.kDefault());
                    } else {
                        writer.formatln("%s = %s == _Field.%s ? builder.%s : null;", field.member(), UNION_FIELD, field.fieldEnum(), field.member());
                    }
                    break;
            }
        }
    } else {
        if (message.isException()) {
            // If an exception class contains a field named 'message' (no caps), it will use that
            // as the exception message unmodified.
            Optional<JField> msg = message.exceptionMessageField();
            if (msg.isPresent()) {
                writer.formatln("super(builder.%s);", msg.get().member()).newline();
            } else {
                writer.appendln("super(createMessage(").begin("                    ");
                boolean first = true;
                for (JField field : message.declaredOrderFields()) {
                    if (first) {
                        first = false;
                    } else {
                        writer.append(',').appendln();
                    }
                    if (field.container()) {
                        writer.format("builder.%s() ? %s : null", field.isSet(), field.fieldInstanceCopy("builder." + field.member()));
                    } else if (!field.isVoid()) {
                        // Void fields have no value.
                        writer.format("builder.%s", field.member());
                    }
                }
                writer.append("));").end().newline();
            }
        }
        for (JField field : message.declaredOrderFields()) {
            if (field.container()) {
                writer.formatln("if (builder.%s()) {", field.isSet()).formatln("    %s = %s;", field.member(), field.fieldInstanceCopy("builder." + field.member())).appendln("} else {");
                if (field.alwaysPresent()) {
                    writer.formatln("    %s = %s;", field.member(), field.kDefault());
                } else {
                    writer.formatln("    %s = null;", field.member());
                }
                writer.appendln('}');
            } else if (field.type() == PType.MESSAGE) {
                writer.formatln("%s = builder.%s_builder != null ? builder.%s_builder.build() : builder.%s;", field.member(), field.member(), field.member(), field.member());
            } else if (!field.isVoid()) {
                // explicitly set it here.
                if (field.alwaysPresent() && !field.isPrimitiveJavaValue()) {
                    writer.formatln("if (builder.%s()) {", field.isSet()).formatln("    %s = builder.%s;", field.member(), field.member()).appendln("} else {").formatln("    %s = %s;", field.member(), field.kDefault()).appendln('}');
                } else {
                    writer.formatln("%s = builder.%s;", field.member(), field.member());
                }
            }
        }
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField)

Example 23 with JField

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

the class CommonMemberFormatter method appendCreateMessage.

private void appendCreateMessage(JMessage<?> message) throws GeneratorException {
    // as the exception message unmodified.
    if (message.exceptionMessageField().isPresent()) {
        return;
    }
    writer.appendln("private static String createMessage(").begin("                                    ");
    boolean first = true;
    for (JField fld : message.declaredOrderFields()) {
        if (!fld.isVoid()) {
            // Void fields have no value.
            if (first) {
                first = false;
            } else {
                writer.append(',').appendln();
            }
            writer.format("%s %s", fld.fieldType(), fld.param());
        }
    }
    writer.append(") {").end().begin().appendln("StringBuilder out = new StringBuilder();").appendln("out.append('{');");
    boolean firstFirstCheck = true;
    boolean alwaysAfter = false;
    boolean last;
    int i = 0;
    int lastPos = message.declaredOrderFields().size() - 1;
    for (JField field : message.declaredOrderFields()) {
        if (field.isVoid()) {
            // Void fields have no value.
            lastPos--;
            continue;
        }
        last = i == lastPos;
        first = i == 0;
        ++i;
        if (!field.alwaysPresent()) {
            if (!alwaysAfter && firstFirstCheck && !last) {
                writer.appendln("boolean first = true;");
            }
            if (field.container()) {
                writer.formatln("if (%s != null && %s.size() > 0) {", field.param(), field.param());
            } else {
                writer.formatln("if (%s != null) {", field.param());
            }
            writer.begin();
        }
        if (alwaysAfter) {
            writer.appendln("out.append(',');");
        } else if (!field.alwaysPresent()) {
            if (firstFirstCheck || first) {
                if (!last) {
                    writer.appendln("first = false;");
                }
            } else if (last) {
                writer.appendln("if (!first) out.append(',');");
            } else {
                writer.appendln("if (first) first = false;").appendln("else out.append(',');");
            }
        }
        writer.formatln("out.append(\"%s:\")", field.name());
        switch(field.type()) {
            case BOOL:
            case I32:
            case I64:
                writer.formatln("   .append(%s);", field.param());
                break;
            case BYTE:
            case I16:
                writer.formatln("   .append((int) %s);", field.param());
                break;
            case DOUBLE:
            case MAP:
            case SET:
            case LIST:
                writer.formatln("   .append(%s.asString(%s));", Strings.class.getName(), field.param());
                break;
            case STRING:
                writer.formatln("   .append('\\\"')").formatln("   .append(%s.escape(%s))", Strings.class.getName(), field.param()).appendln("   .append('\\\"');");
                break;
            case BINARY:
                writer.appendln("   .append(\"b64(\")").formatln("   .append(%s.toBase64())", field.param()).appendln("   .append(')');");
                break;
            case MESSAGE:
                writer.formatln("   .append(%s.asString());", field.param());
                break;
            default:
                writer.formatln("   .append(%s.asString());", field.param());
                break;
        }
        if (!field.alwaysPresent()) {
            writer.end().appendln('}');
            if (!alwaysAfter && firstFirstCheck) {
                firstFirstCheck = false;
            }
        } else {
            alwaysAfter = true;
        }
    }
    writer.appendln("out.append('}');").appendln("return out.toString();").end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) Strings(net.morimekta.util.Strings)

Example 24 with JField

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

the class CommonMemberFormatter method appendFieldGetters.

private void appendFieldGetters(JMessage<?> message) throws GeneratorException {
    for (JField field : message.declaredOrderFields()) {
        if (message.isUnion()) {
            if (field.container()) {
                writer.formatln("public int %s() {", field.counter()).formatln("    return %s == _Field.%s ? %s.size() : 0;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
            }
            if (field.alwaysPresent() || field.isVoid()) {
                writer.formatln("public boolean %s() {", field.presence()).formatln("    return %s == _Field.%s;", UNION_FIELD, field.fieldEnum()).appendln('}').newline();
            } else {
                writer.formatln("public boolean %s() {", field.presence()).formatln("    return %s == _Field.%s && %s != null;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
            }
        } else {
            if (field.container()) {
                writer.formatln("public int %s() {", field.counter()).formatln("    return %s != null ? %s.size() : 0;", field.member(), field.member()).appendln('}').newline();
            }
            if (field.alwaysPresent()) {
                writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return true;").end().appendln('}').newline();
            } else {
                writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return %s != null;", field.member()).end().appendln('}').newline();
            }
        }
        if (field.isVoid()) {
            // Void fields have no value.
            continue;
        }
        BlockCommentBuilder comment = new BlockCommentBuilder(writer);
        if (field.hasComment()) {
            comment.comment(field.comment()).newline();
        }
        comment.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);
        }
        if (field.alwaysPresent() && !field.isPrimitiveJavaValue()) {
            writer.appendln(JAnnotation.NON_NULL);
        }
        writer.formatln("public %s %s() {", field.valueType(), field.getter());
        if ((field.isPrimitiveJavaValue() || field.field().hasDefaultValue()) && !field.alwaysPresent()) {
            writer.formatln("    return %s() ? %s : %s;", field.presence(), field.member(), field.kDefault());
        } else {
            writer.formatln("    return %s;", field.member());
        }
        writer.appendln('}').newline();
    }
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 25 with JField

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

the class CommonOverridesFormatter 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) {
        boolean first = true;
        writer.formatln("%s other = (%s) o;", message.instanceType(), message.instanceType()).appendln("return ");
        if (message.isUnion()) {
            writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), UNION_FIELD, UNION_FIELD);
            first = false;
        }
        for (JField field : message.declaredOrderFields()) {
            if (field.isVoid())
                continue;
            if (first) {
                first = false;
            } else {
                writer.append(" &&").appendln("       ");
            }
            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)

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