Search in sources :

Example 1 with JMessage

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

the class JavaServiceFormatter method appendStructs.

@SuppressWarnings("unchecked")
private void appendStructs(IndentedPrintWriter writer, JService service) throws GeneratorException, IOException {
    for (JServiceMethod method : service.declaredMethods()) {
        JMessage request = new JMessage<>(method.getMethod().getRequestType(), helper);
        writer.formatln("// type --> %s", request.descriptor().getName());
        messageFormat.appendMessageClass(method.getMethod().getRequestType());
        if (method.getMethod().getResponseType() != null) {
            JMessage response = new JMessage(method.getMethod().getResponseType(), helper);
            writer.formatln("// type <-- %s", response.descriptor().getName());
            messageFormat.appendMessageClass(method.getMethod().getResponseType());
        }
    }
}
Also used : JMessage(net.morimekta.providence.generator.format.java.utils.JMessage) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod)

Example 2 with JMessage

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

the class BuilderCoreOverridesFormatter method appendOverrideAdder.

private void appendOverrideAdder(JMessage<?> message) throws GeneratorException {
    writer.appendln("@Override").appendln("public _Builder addTo(int key, Object value) {").begin().appendln("switch (key) {").begin();
    message.numericalOrderFields().stream().filter(field -> field.type() == PType.LIST || field.type() == PType.SET).forEachOrdered(field -> {
        PContainer<?> ct = (PContainer<?>) field.field().getDescriptor();
        PDescriptor itype = ct.itemDescriptor();
        writer.formatln("case %d: %s((%s) value); break;", field.id(), field.adder(), helper.getValueType(itype));
    });
    writer.appendln("default: break;").end().appendln('}').appendln("return this;").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) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PContainer(net.morimekta.providence.descriptor.PContainer)

Example 3 with JMessage

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

the class BaseMessageFormatter method appendMessageClass.

public void appendMessageClass(PMessageDescriptor<?, ?> descriptor) throws GeneratorException {
    @SuppressWarnings("unchecked") JMessage<?> message = new JMessage(descriptor, helper);
    BlockCommentBuilder classComment = null;
    if (message.descriptor() instanceof CAnnotatedDescriptor) {
        CAnnotatedDescriptor annotatedDescriptor = (CAnnotatedDescriptor) message.descriptor();
        if (annotatedDescriptor.getDocumentation() != null) {
            classComment = new BlockCommentBuilder(writer);
            classComment.comment(annotatedDescriptor.getDocumentation());
        }
        String deprecatedReason = annotatedDescriptor.getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (deprecatedReason != null && deprecatedReason.trim().length() > 0) {
            if (classComment == null) {
                classComment = new BlockCommentBuilder(writer);
            } else {
                classComment.newline();
            }
            classComment.deprecated_(deprecatedReason);
        }
    }
    if (classComment != null) {
        classComment.finish();
    }
    formatters.forEach(f -> f.appendClassAnnotations(message));
    writer.formatln("%s %sclass %s", makeProtected ? "protected" : "public", inner ? "static " : "", getClassName(message)).begin().begin();
    appendClassExtends(message);
    writer.end();
    Set<String> impl = new LinkedHashSet<>();
    formatters.forEach(f -> impl.addAll(f.getExtraImplements(message)));
    if (impl.size() > 0) {
        writer.formatln("    implements ").begin("               ");
        boolean first = true;
        for (String i : impl) {
            if (first) {
                first = false;
            } else {
                writer.append(',').appendln();
            }
            writer.append(i);
        }
        writer.end();
    }
    writer.append(" {");
    formatters.forEach(f -> f.appendConstants(message));
    formatters.forEach(f -> f.appendFields(message));
    formatters.forEach(f -> f.appendConstructors(message));
    formatters.forEach(f -> f.appendMethods(message));
    formatters.forEach(f -> f.appendExtraProperties(message));
    writer.end().appendln('}');
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CAnnotatedDescriptor(net.morimekta.providence.reflect.contained.CAnnotatedDescriptor) JMessage(net.morimekta.providence.generator.format.java.utils.JMessage) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 4 with JMessage

use of net.morimekta.providence.generator.format.java.utils.JMessage 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

JMessage (net.morimekta.providence.generator.format.java.utils.JMessage)4 ArrayList (java.util.ArrayList)2 PMessageBuilder (net.morimekta.providence.PMessageBuilder)2 PType (net.morimekta.providence.PType)2 PContainer (net.morimekta.providence.descriptor.PContainer)2 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)2 GeneratorException (net.morimekta.providence.generator.GeneratorException)2 UNION_FIELD (net.morimekta.providence.generator.format.java.messages.CoreOverridesFormatter.UNION_FIELD)2 MessageMemberFormatter (net.morimekta.providence.generator.format.java.shared.MessageMemberFormatter)2 JAnnotation (net.morimekta.providence.generator.format.java.utils.JAnnotation)2 JField (net.morimekta.providence.generator.format.java.utils.JField)2 JHelper (net.morimekta.providence.generator.format.java.utils.JHelper)2 Strings (net.morimekta.util.Strings)2 IndentedPrintWriter (net.morimekta.util.io.IndentedPrintWriter)2 LinkedHashSet (java.util.LinkedHashSet)1 BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)1 JServiceMethod (net.morimekta.providence.generator.format.java.utils.JServiceMethod)1 CAnnotatedDescriptor (net.morimekta.providence.reflect.contained.CAnnotatedDescriptor)1