Search in sources :

Example 31 with JField

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

the class JavaServiceFormatter method appendClient.

private void appendClient(IndentedPrintWriter writer, JService service) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (service.getService().getDocumentation() != null) {
        comment.comment(service.getService().getDocumentation()).newline();
    }
    comment.comment("Client implementation for " + service.getService().getQualifiedName()).finish();
    writer.appendln("public static class Client").formatln("        extends %s", PClient.class.getName()).formatln("        implements Iface {").begin();
    writer.formatln("private final %s handler;", PServiceCallHandler.class.getName()).newline();
    new BlockCommentBuilder(writer).comment("Create " + service.getService().getQualifiedName() + " service client.").newline().param_("handler", "The client handler.").finish();
    writer.formatln("public Client(%s handler) {", PServiceCallHandler.class.getName()).appendln("    this.handler = handler;").appendln('}').newline();
    boolean firstMethod = true;
    for (JServiceMethod method : service.methods()) {
        if (firstMethod) {
            firstMethod = false;
        } else {
            writer.newline();
        }
        writer.appendln("@Override");
        // Field ID 0 is the return type.
        JField ret = method.getResponse();
        writer.appendln("public ");
        if (ret != null) {
            writer.append(ret.valueType());
        } else {
            writer.append("void");
        }
        writer.format(" %s(", method.methodName()).begin("        ");
        boolean first = true;
        for (JField param : method.params()) {
            if (first) {
                first = false;
            } else {
                writer.append(",");
            }
            writer.formatln("%s %s", param.fieldType(), param.param());
        }
        writer.end().format(")").formatln("        throws %s", IOException.class.getName()).begin("               ");
        for (JField ex : method.exceptions()) {
            writer.append(",");
            writer.appendln(ex.instanceType());
        }
        writer.format(" {").end().begin();
        writer.formatln("%s._Builder rq = %s.builder();", service.getRequestClassRef(method), service.getRequestClassRef(method));
        for (JField param : method.params()) {
            if (!param.alwaysPresent()) {
                writer.formatln("if (%s != null) {", param.param()).begin();
            }
            writer.formatln("rq.%s(%s);", param.setter(), param.param());
            if (!param.alwaysPresent()) {
                writer.end().appendln("}");
            }
        }
        String type = method.getMethod().isOneway() ? PServiceCallType.ONEWAY.name() : PServiceCallType.CALL.name();
        writer.newline().formatln("%s call = new %s<>(\"%s\", %s.%s, getNextSequenceId(), rq.build());", PServiceCall.class.getName(), PServiceCall.class.getName(), method.name(), PServiceCallType.class.getName(), type).appendln();
        if (method.getResponseClass() != null) {
            writer.format("%s resp = ", PServiceCall.class.getName());
        }
        writer.format("handler.handleCall(call, %s.kDescriptor);", service.className());
        if (method.getResponseClass() != null) {
            writer.newline().formatln("if (resp.getType() == %s.%s) {", PServiceCallType.class.getName(), PServiceCallType.EXCEPTION.name()).formatln("    throw (%s) resp.getMessage();", PApplicationException.class.getName()).appendln('}').newline().formatln("%s msg = (%s) resp.getMessage();", service.getResponseClassRef(method), service.getResponseClassRef(method));
            writer.appendln("if (msg.unionFieldIsSet()) {").begin().appendln("switch (msg.unionField()) {").begin();
            if (method.exceptions().length > 0) {
                for (JField ex : method.exceptions()) {
                    writer.formatln("case %s:", ex.fieldEnum()).formatln("    throw msg.%s();", ex.getter());
                }
            }
            if (method.getResponse() != null) {
                writer.formatln("case %s:", method.getResponse().fieldEnum());
                if (method.getResponse().isVoid()) {
                    writer.formatln("    return;");
                } else {
                    writer.formatln("    return msg.%s();", method.getResponse().getter());
                }
            }
            writer.end().appendln("}").end().appendln("}").newline();
            // In case there is no return value, and no exception,
            // the union field is not set. This *should* cause an error.
            writer.formatln("throw new %s(\"Result field for %s.%s() not set\",", PApplicationException.class.getName(), service.getService().getQualifiedName(), method.name()).formatln("          %s %s.%s);", PApplicationException.class.getName().replaceAll(".", " "), PApplicationExceptionType.class.getName(), PApplicationExceptionType.MISSING_RESULT.name());
        }
        writer.end().appendln('}');
    }
    writer.end().appendln('}').newline();
}
Also used : PApplicationExceptionType(net.morimekta.providence.PApplicationExceptionType) PClient(net.morimekta.providence.PClient) JField(net.morimekta.providence.generator.format.java.utils.JField) PServiceCallType(net.morimekta.providence.PServiceCallType) PServiceCall(net.morimekta.providence.PServiceCall) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

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