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();
}
Aggregations