use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.
the class CommonMemberFormatter method appendFieldGetters.
private void appendFieldGetters(JMessage<?> message) throws GeneratorException {
for (JField field : message.declaredOrderFields()) {
if (message.isUnion()) {
if (field.container()) {
writer.formatln("public int %s() {", field.counter()).formatln(" return %s == _Field.%s ? %s.size() : 0;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
}
if (field.alwaysPresent() || field.isVoid()) {
writer.formatln("public boolean %s() {", field.presence()).formatln(" return %s == _Field.%s;", UNION_FIELD, field.fieldEnum()).appendln('}').newline();
} else {
writer.formatln("public boolean %s() {", field.presence()).formatln(" return %s == _Field.%s && %s != null;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
}
} else {
if (field.container()) {
writer.formatln("public int %s() {", field.counter()).formatln(" return %s != null ? %s.size() : 0;", field.member(), field.member()).appendln('}').newline();
}
if (field.alwaysPresent()) {
writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return true;").end().appendln('}').newline();
} else {
writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return %s != null;", field.member()).end().appendln('}').newline();
}
}
if (field.isVoid()) {
// Void fields have no value.
continue;
}
BlockCommentBuilder comment = new BlockCommentBuilder(writer);
if (field.hasComment()) {
comment.comment(field.comment()).newline();
}
comment.return_("The field value");
if (JAnnotation.isDeprecated(field)) {
String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
if (reason != null && reason.trim().length() > 0) {
comment.deprecated_(reason);
}
}
comment.finish();
if (JAnnotation.isDeprecated(field)) {
writer.appendln(JAnnotation.DEPRECATED);
}
if (field.alwaysPresent() && !field.isPrimitiveJavaValue()) {
writer.appendln(JAnnotation.NON_NULL);
}
writer.formatln("public %s %s() {", field.valueType(), field.getter());
if ((field.isPrimitiveJavaValue() || field.field().hasDefaultValue()) && !field.alwaysPresent()) {
writer.formatln(" return %s() ? %s : %s;", field.presence(), field.member(), field.kDefault());
} else {
writer.formatln(" return %s;", field.member());
}
writer.appendln('}').newline();
}
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.
the class HazelcastPortableProgramFormatter method appendProgramClass.
@Override
public void appendProgramClass(CProgram document) throws GeneratorException {
if (document.getDocumentation() != null) {
new BlockCommentBuilder(writer).comment(document.getDocumentation()).finish();
}
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 {", helper.getHazelcastFactoryClassName(document)).begin().newline();
Optional<CConst> factoryID = document.getConstants().stream().filter(t -> t.getName().equals(FACTORY_ID)).findFirst();
if (!factoryID.isPresent()) {
throw new GeneratorException("Need to provide \"const i32 FACTORY_ID = ?\" in the thrift file for " + "hazelcast generation!");
} else {
CConst c = factoryID.get();
String type = helper.getValueType(c.getDescriptor());
String name = c.getName();
writer.formatln("public static final %s %s = %s.%s;", type, name, helper.getConstantsClassName(document), name).newline();
}
List<CStructDescriptor> messages = new ArrayList<>();
for (PDeclaredDescriptor c : document.getDeclaredTypes()) {
try {
if (PType.MESSAGE == c.getType() && c instanceof CStructDescriptor) {
CStructDescriptor message = (CStructDescriptor) c;
if (message.hasAnnotation(ThriftAnnotation.JAVA_HAZELCAST_CLASS_ID)) {
writer.formatln("public static final int %s = %s;", getHazelcastClassId(message.getName()), message.getAnnotationValue(ThriftAnnotation.JAVA_HAZELCAST_CLASS_ID));
messages.add(message);
}
}
} catch (Exception e) {
throw new GeneratorException(e.getMessage());
}
}
writer.newline();
appendPopulateMethod(messages);
if (messages.isEmpty()) {
throw new GeneratorException("No annotations available to generate!");
} else {
writer.formatln("private static class %s implements %s {", FACTORY_IMPL, PortableFactory.class.getName()).begin().newline();
appendCreateMethod(messages);
appendGetDefinitions(messages);
writer.end().appendln("}");
}
writer.end().appendln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.
the class JavaConstantsFormatter method appendProgramClass.
@Override
public void appendProgramClass(CProgram program) throws GeneratorException {
ValueBuilder value = new ValueBuilder(writer, helper);
if (program.getDocumentation() != null) {
new BlockCommentBuilder(writer).comment(program.getDocumentation()).finish();
}
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.appendln("@SuppressWarnings(\"unused\")").formatln("public class %s {", helper.getConstantsClassName(program)).begin().formatln("private %s() {}", helper.getConstantsClassName(program));
for (CField c : program.getConstants()) {
writer.newline();
try {
String name = c.getName();
writer.formatln("public static final %s %s;", helper.getValueType(c.getDescriptor()), name).appendln("static {").begin().formatln("%s = ", name).begin();
value.appendTypedValue(c.getDefaultValue(), c.getDescriptor());
writer.append(';').end().end().appendln('}');
} catch (Exception e) {
throw new GeneratorException("Unable to generate constant " + program.getProgramName() + "." + c.getName(), e);
}
}
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder 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('}');
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder 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