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