Search in sources :

Example 1 with JField

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

the class JavaServiceFormatter method appendIface.

private void appendIface(IndentedPrintWriter writer, JService service) throws GeneratorException {
    String inherits = "";
    if (service.getService().getExtendsService() != null) {
        CService other = service.getService().getExtendsService();
        inherits = "extends " + helper.getJavaPackage(other) + "." + new JService(other, helper).className() + ".Iface ";
    }
    if (service.getService().getDocumentation() != null) {
        new BlockCommentBuilder(writer).comment(service.getService().getDocumentation()).finish();
    }
    writer.formatln("public interface Iface %s{", inherits).begin();
    boolean firstMethod = true;
    for (JServiceMethod method : service.declaredMethods()) {
        if (firstMethod) {
            firstMethod = false;
        } else {
            writer.newline();
        }
        String methodThrows = service.methodsThrows(method);
        BlockCommentBuilder comment = new BlockCommentBuilder(writer);
        if (method.getMethod().getDocumentation() != null) {
            comment.comment(method.getMethod().getDocumentation()).newline();
        }
        for (JField param : method.params()) {
            if (param.comment() != null) {
                comment.param_(param.param(), param.comment());
            } else {
                comment.param_(param.param(), "The " + param.name() + " value.");
            }
        }
        if (method.getResponse() != null && !method.getResponse().isVoid()) {
            comment.return_("The " + method.name() + " result.");
        }
        if (methodThrows != null) {
            comment.throws_(methodThrows, "On any declared exception.");
        } else {
            for (JField param : method.exceptions()) {
                if (param.comment() != null) {
                    comment.throws_(param.fieldType(), param.comment());
                } else {
                    comment.throws_(param.fieldType(), "The " + param.name() + " exception.");
                }
            }
        }
        comment.throws_(IOException.class, "On providence or non-declared exceptions.").finish();
        JField ret = method.getResponse();
        if (ret != null) {
            writer.appendln(ret.valueType());
        } else {
            writer.appendln("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(")");
        writer.formatln("        throws %s", IOException.class.getName()).begin("               ");
        if (methodThrows != null) {
            writer.append(",");
            writer.formatln("%s", methodThrows);
        } else {
            for (JField ex : method.exceptions()) {
                writer.append(",");
                writer.appendln(ex.instanceType());
            }
        }
        writer.format(";").end();
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) JService(net.morimekta.providence.generator.format.java.utils.JService) CService(net.morimekta.providence.reflect.contained.CService) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) IOException(java.io.IOException)

Example 2 with JField

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

the class JavaServiceFormatter method appendProcessor.

private void appendProcessor(IndentedPrintWriter writer, JService service) throws GeneratorException {
    writer.formatln("public static class Processor implements %s {", PProcessor.class.getName()).begin().appendln("private final Iface impl;");
    writer.formatln("public Processor(Iface impl) {").appendln("    this.impl = impl;").appendln('}').newline();
    writer.appendln("@Override").formatln("public %s getDescriptor() {", PService.class.getName()).appendln("    return kDescriptor;").appendln('}').newline();
    writer.appendln("@Override").formatln("public <Request extends %s<Request, RequestField>,", PMessage.class.getName()).formatln("        Response extends %s<Response, ResponseField>,", PMessage.class.getName()).formatln("        RequestField extends %s,", PField.class.getName()).formatln("        ResponseField extends %s>", PField.class.getName()).formatln("%s<Response, ResponseField> handleCall(", PServiceCall.class.getName()).formatln("        %s<Request, RequestField> call,", PServiceCall.class.getName()).formatln("        %s service)", PService.class.getName()).formatln("        throws %s,", IOException.class.getName()).formatln("               %s {", SerializerException.class.getName()).begin();
    writer.appendln("switch(call.getMethod()) {").begin();
    for (JServiceMethod method : service.methods()) {
        writer.formatln("case \"%s\": {", method.name()).begin();
        if (method.getResponseClass() != null) {
            writer.formatln("%s._Builder rsp = %s.builder();", service.getResponseClassRef(method), service.getResponseClassRef(method));
        }
        String methodThrows = service.methodsThrows(method);
        if (methodThrows != null || method.exceptions().length > 0) {
            writer.appendln("try {").begin();
        }
        writer.formatln("%s req = (%s) call.getMessage();", service.getRequestClassRef(method), service.getRequestClassRef(method));
        String indent = "      " + Strings.times(" ", method.methodName().length());
        if (method.getResponse() != null && !method.getResponse().isVoid()) {
            writer.formatln("%s result =", method.getResponse().valueType());
            writer.appendln("        ");
            indent += "        ";
        } else {
            writer.appendln();
        }
        writer.format("impl.%s(", method.methodName()).begin(indent);
        boolean first = true;
        for (JField param : method.params()) {
            if (first) {
                first = false;
            } else {
                writer.append(',').appendln();
            }
            // An optional primitive value without an explicit default
            // should be called with null value if not present in the
            // request.
            boolean optionalPrimitiveNoDefault = param.isPrimitiveJavaValue() && param.field().getRequirement() == PRequirement.OPTIONAL && param.field().getDefaultValue() == null;
            if (optionalPrimitiveNoDefault) {
                writer.format("req.%s() ? ", param.presence());
            }
            writer.format("req.%s()", param.getter());
            if (optionalPrimitiveNoDefault) {
                writer.append(" : null");
            }
        }
        writer.end().append(");");
        if (method.getResponse() != null) {
            if (method.getResponse().isVoid()) {
                writer.formatln("rsp.%s();", method.getResponse().setter());
            } else {
                writer.formatln("rsp.%s(result);", method.getResponse().setter());
            }
        }
        if (methodThrows != null || method.exceptions().length > 0) {
            writer.end();
            for (JField ex : method.exceptions()) {
                writer.formatln("} catch (%s e) {", ex.instanceType()).formatln("    rsp.%s(e);", ex.setter());
            }
            if (methodThrows != null) {
                writer.formatln("} catch (%s e) {", methodThrows).formatln("    throw new %s(e.getMessage(), e);", IOException.class.getName());
            }
            writer.appendln('}');
        }
        if (method.getResponseClass() != null) {
            String spaces = PServiceCall.class.getName().replaceAll("[\\S]", " ");
            writer.formatln("%s reply =", PServiceCall.class.getName()).formatln("        new %s<>(call.getMethod(),", PServiceCall.class.getName()).formatln("            %s   %s.%s,", spaces, PServiceCallType.class.getName(), PServiceCallType.REPLY.name()).formatln("            %s   call.getSequence(),", spaces).formatln("            %s   rsp.build());", spaces).appendln("return reply;");
        } else {
            // No reply, but it's fine.
            writer.appendln("return null;");
        }
        writer.end().appendln('}');
    }
    writer.appendln("default: {").begin().formatln("%s ex =", PApplicationException.class.getName()).formatln("        new %s(", PApplicationException.class.getName()).formatln("                \"Unknown method \\\"\" + call.getMethod() + \"\\\" on %s.\",", service.getService().getQualifiedName()).formatln("                %s.%s);", PApplicationExceptionType.class.getName(), PApplicationExceptionType.UNKNOWN_METHOD.asString());
    String spaces = PServiceCall.class.getName().replaceAll("[\\S]", " ");
    writer.formatln("%s reply =", PServiceCall.class.getName()).formatln("        new %s(call.getMethod(),", PServiceCall.class.getName()).formatln("            %s %s.%s,", spaces, PServiceCallType.class.getName(), PServiceCallType.EXCEPTION.name()).formatln("            %s call.getSequence(),", spaces).formatln("            %s ex);", spaces).appendln("return reply;");
    writer.end().appendln('}').end().appendln('}');
    writer.end().appendln('}');
    writer.end().appendln('}').newline();
}
Also used : PApplicationExceptionType(net.morimekta.providence.PApplicationExceptionType) JField(net.morimekta.providence.generator.format.java.utils.JField) PField(net.morimekta.providence.descriptor.PField) PProcessor(net.morimekta.providence.PProcessor) PMessage(net.morimekta.providence.PMessage) PServiceCall(net.morimekta.providence.PServiceCall) PApplicationException(net.morimekta.providence.PApplicationException) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod) IOException(java.io.IOException)

Example 3 with JField

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

the class BuilderCommonMemberFormatter method appendDefaultConstructor.

private void appendDefaultConstructor(JMessage<?> message) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    comment.comment("Make a " + message.descriptor().getQualifiedName() + " builder.").finish();
    writer.appendln("public _Builder() {").begin();
    if (!message.isUnion()) {
        writer.formatln("optionals = new %s(%d);", BitSet.class.getName(), message.declaredOrderFields().size());
        writer.formatln("modified = new %s(%d);", BitSet.class.getName(), message.declaredOrderFields().size());
    } else {
        writer.appendln("modified = false;");
    }
    for (JField field : message.declaredOrderFields()) {
        if (field.alwaysPresent()) {
            writer.formatln("%s = %s;", field.member(), field.kDefault());
        }
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) BitSet(java.util.BitSet) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 4 with JField

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

the class BuilderCoreOverridesFormatter method appendOverrideSetter.

private void appendOverrideSetter(JMessage<?> message) throws GeneratorException {
    writer.appendln(JAnnotation.NON_NULL).appendln("@Override").appendln("@SuppressWarnings(\"unchecked\")").appendln("public _Builder set(int key, Object value) {").begin().appendln("if (value == null) return clear(key);").appendln("switch (key) {").begin();
    for (JField field : message.numericalOrderFields()) {
        if (field.isVoid()) {
            // Void fields have no value.
            writer.formatln("case %d: %s(); break;", field.id(), field.setter(), field.valueType());
        } else {
            writer.formatln("case %d: %s((%s) value); break;", field.id(), field.setter(), field.valueType());
        }
    }
    writer.appendln("default: break;").end().appendln('}').appendln("return this;").end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField)

Example 5 with JField

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

the class BuilderCoreOverridesFormatter method appendOverrideIsSet.

private void appendOverrideIsSet(JMessage<?> message) throws GeneratorException {
    writer.appendln("@Override").appendln("public boolean isSet(int key) {").begin().appendln("switch (key) {").begin();
    if (message.isUnion()) {
        for (JField field : message.numericalOrderFields()) {
            writer.formatln("case %d: return %s == _Field.%s;", field.id(), UNION_FIELD, field.fieldEnum());
        }
    } else {
        for (JField field : message.numericalOrderFields()) {
            writer.formatln("case %d: return optionals.get(%d);", field.id(), field.index());
        }
    }
    writer.appendln("default: break;").end().appendln('}').appendln("return false;").end().appendln('}').newline();
}
Also used : 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