use of net.morimekta.providence.generator.format.java.utils.JService 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();
}
use of net.morimekta.providence.generator.format.java.utils.JService in project providence by morimekta.
the class JavaServiceFormatter method appendDescriptor.
private void appendDescriptor(IndentedPrintWriter writer, JService service) throws GeneratorException {
writer.formatln("public enum Method implements %s {", PServiceMethod.class.getName()).begin();
for (JServiceMethod method : service.methods()) {
String responseDesc = method.getResponseClass() == null ? "null" : service.getResponseClassRef(method) + ".kDescriptor";
writer.formatln("%s(\"%s\", %b, %s.kDescriptor, %s),", method.constant(), method.name(), method.getMethod().isOneway(), service.getRequestClassRef(method), responseDesc);
}
writer.appendln(';').newline();
writer.appendln("private final String name;").appendln("private final boolean oneway;").formatln("private final %s request;", PStructDescriptor.class.getName()).formatln("private final %s response;", PUnionDescriptor.class.getName()).newline();
writer.formatln("private Method(String name, boolean oneway, %s request, %s response) {", PStructDescriptor.class.getName(), PUnionDescriptor.class.getName()).appendln(" this.name = name;").appendln(" this.oneway = oneway;").appendln(" this.request = request;").appendln(" this.response = response;").appendln('}').newline();
writer.appendln("public String getName() {").appendln(" return name;").appendln('}').newline().appendln("public boolean isOneway() {").appendln(" return oneway;").appendln('}').newline().formatln("public %s getRequestType() {", PStructDescriptor.class.getName()).formatln(" return request;").appendln('}').newline().formatln("public %s getResponseType() {", PUnionDescriptor.class.getName()).formatln(" return response;").appendln('}').newline();
writer.appendln("public static Method findByName(String name) {").begin().appendln("switch (name) {").begin();
for (JServiceMethod method : service.methods()) {
writer.formatln("case \"%s\": return %s;", method.name(), method.constant());
}
writer.end().appendln('}').appendln("return null;").end().appendln('}');
writer.appendln(JAnnotation.NON_NULL).appendln("public static Method methodForName(String name) {").begin().appendln("Method method = findByName(name);").appendln("if (method == null) {").formatln(" throw new IllegalArgumentException(\"No such method \\\"\" + name + \"\\\" in service %s\");", service.getService().getQualifiedName()).appendln("}").appendln("return method;").end().appendln('}');
writer.end().appendln('}').newline();
// Ended methods enum.
String inherits = "null";
if (service.getService().getExtendsService() != null) {
CService other = service.getService().getExtendsService();
inherits = helper.getJavaPackage(other) + "." + new JService(other, helper).className() + ".provider()";
}
writer.formatln("private static class _Descriptor extends %s {", PService.class.getName()).begin().appendln("private _Descriptor() {").formatln(" super(\"%s\", \"%s\", %s, Method.values());", service.getService().getProgramName(), service.getService().getName(), inherits).appendln('}').newline().appendln("@Override").appendln("public Method getMethod(String name) {").appendln(" return Method.findByName(name);").appendln("}").end().appendln('}').newline();
writer.formatln("private static class _Provider implements %s {", PServiceProvider.class.getName()).begin().appendln("@Override").formatln("public %s getService() {", PService.class.getName()).appendln(" return kDescriptor;").appendln("}").end().appendln('}').newline();
writer.formatln("public static final %s kDescriptor = new _Descriptor();", PService.class.getName()).newline();
writer.formatln("public static %s provider() {", PServiceProvider.class.getName()).appendln(" return new _Provider();").appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JService in project providence by morimekta.
the class JavaServiceFormatter method appendServiceClass.
@Override
public void appendServiceClass(CService cs) throws GeneratorException, IOException {
JService service = new JService(cs, helper);
if (cs.getDocumentation() != null) {
new BlockCommentBuilder(writer).comment(cs.getDocumentation()).finish();
}
String inherits = "";
if (service.getService().getExtendsService() != null) {
CService other = service.getService().getExtendsService();
inherits = "extends " + helper.getJavaPackage(other) + "." + new JService(other, helper).className() + " ";
}
writer.appendln("@SuppressWarnings(\"unused\")");
if (javaOptions.generated_annotation_version) {
writer.formatln("@%s(\"%s %s\")", Generated.class.getName(), generatorOptions.generator_program_name, generatorOptions.program_version);
} else {
writer.formatln("@%s(\"%s\")", Generated.class.getName(), generatorOptions.generator_program_name);
}
writer.formatln("public class %s %s{", service.className(), inherits).begin();
appendIface(writer, service);
appendClient(writer, service);
appendProcessor(writer, service);
appendDescriptor(writer, service);
appendStructs(writer, service);
// protected constructor should defeat instantiation, but can inherit
// from parent to be able to get access to inner protected classes.
writer.formatln("protected %s() {}", service.className());
writer.end().appendln('}');
}
Aggregations