use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BuilderCoreOverridesFormatter method appendOverrideIsModified.
private void appendOverrideIsModified(JMessage<?> message) throws GeneratorException {
writer.appendln("@Override").appendln("public boolean isModified(int key) {").begin();
if (!message.isUnion()) {
writer.appendln("switch (key) {").begin();
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %d: return modified.get(%d);", field.id(), field.index());
}
writer.appendln("default: break;").end().appendln('}').appendln("return false;");
} else {
writer.appendln("return modified;");
}
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CommonBuilderFormatter method appendBuilderConstructors.
private void appendBuilderConstructors(JMessage<?> message) throws GeneratorException {
writer.formatln("private %s(_Builder builder) {", message.instanceType()).begin();
if (message.isUnion()) {
writer.formatln("%s = builder.%s;", UNION_FIELD, UNION_FIELD).newline();
for (JField field : message.declaredOrderFields()) {
switch(field.type()) {
case VOID:
// Void fields have no value.
break;
case LIST:
case MAP:
case SET:
writer.formatln("%s = %s == _Field.%s ? %s : null;", field.member(), UNION_FIELD, field.fieldEnum(), field.fieldInstanceCopy("builder." + field.member()), field.member());
break;
case MESSAGE:
writer.formatln("%s = %s != _Field.%s", field.member(), UNION_FIELD, field.fieldEnum()).appendln(" ? null").formatln(" : builder.%s_builder != null ? builder.%s_builder.build() : builder.%s;", field.member(), field.member(), field.member());
break;
default:
if (field.alwaysPresent()) {
writer.formatln("%s = %s == _Field.%s ? builder.%s : %s;", field.member(), UNION_FIELD, field.fieldEnum(), field.member(), field.kDefault());
} else {
writer.formatln("%s = %s == _Field.%s ? builder.%s : null;", field.member(), UNION_FIELD, field.fieldEnum(), field.member());
}
break;
}
}
} else {
if (message.isException()) {
// If an exception class contains a field named 'message' (no caps), it will use that
// as the exception message unmodified.
Optional<JField> msg = message.exceptionMessageField();
if (msg.isPresent()) {
writer.formatln("super(builder.%s);", msg.get().member()).newline();
} else {
writer.appendln("super(createMessage(").begin(" ");
boolean first = true;
for (JField field : message.declaredOrderFields()) {
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
if (field.container()) {
writer.format("builder.%s() ? %s : null", field.isSet(), field.fieldInstanceCopy("builder." + field.member()));
} else if (!field.isVoid()) {
// Void fields have no value.
writer.format("builder.%s", field.member());
}
}
writer.append("));").end().newline();
}
}
for (JField field : message.declaredOrderFields()) {
if (field.container()) {
writer.formatln("if (builder.%s()) {", field.isSet()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy("builder." + field.member())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
} else if (field.type() == PType.MESSAGE) {
writer.formatln("%s = builder.%s_builder != null ? builder.%s_builder.build() : builder.%s;", field.member(), field.member(), field.member(), field.member());
} else if (!field.isVoid()) {
// explicitly set it here.
if (field.alwaysPresent() && !field.isPrimitiveJavaValue()) {
writer.formatln("if (builder.%s()) {", field.isSet()).formatln(" %s = builder.%s;", field.member(), field.member()).appendln("} else {").formatln(" %s = %s;", field.member(), field.kDefault()).appendln('}');
} else {
writer.formatln("%s = builder.%s;", field.member(), field.member());
}
}
}
}
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CommonMemberFormatter method appendCreateMessage.
private void appendCreateMessage(JMessage<?> message) throws GeneratorException {
// as the exception message unmodified.
if (message.exceptionMessageField().isPresent()) {
return;
}
writer.appendln("private static String createMessage(").begin(" ");
boolean first = true;
for (JField fld : message.declaredOrderFields()) {
if (!fld.isVoid()) {
// Void fields have no value.
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
writer.format("%s %s", fld.fieldType(), fld.param());
}
}
writer.append(") {").end().begin().appendln("StringBuilder out = new StringBuilder();").appendln("out.append('{');");
boolean firstFirstCheck = true;
boolean alwaysAfter = false;
boolean last;
int i = 0;
int lastPos = message.declaredOrderFields().size() - 1;
for (JField field : message.declaredOrderFields()) {
if (field.isVoid()) {
// Void fields have no value.
lastPos--;
continue;
}
last = i == lastPos;
first = i == 0;
++i;
if (!field.alwaysPresent()) {
if (!alwaysAfter && firstFirstCheck && !last) {
writer.appendln("boolean first = true;");
}
if (field.container()) {
writer.formatln("if (%s != null && %s.size() > 0) {", field.param(), field.param());
} else {
writer.formatln("if (%s != null) {", field.param());
}
writer.begin();
}
if (alwaysAfter) {
writer.appendln("out.append(',');");
} else if (!field.alwaysPresent()) {
if (firstFirstCheck || first) {
if (!last) {
writer.appendln("first = false;");
}
} else if (last) {
writer.appendln("if (!first) out.append(',');");
} else {
writer.appendln("if (first) first = false;").appendln("else out.append(',');");
}
}
writer.formatln("out.append(\"%s:\")", field.name());
switch(field.type()) {
case BOOL:
case I32:
case I64:
writer.formatln(" .append(%s);", field.param());
break;
case BYTE:
case I16:
writer.formatln(" .append((int) %s);", field.param());
break;
case DOUBLE:
case MAP:
case SET:
case LIST:
writer.formatln(" .append(%s.asString(%s));", Strings.class.getName(), field.param());
break;
case STRING:
writer.formatln(" .append('\\\"')").formatln(" .append(%s.escape(%s))", Strings.class.getName(), field.param()).appendln(" .append('\\\"');");
break;
case BINARY:
writer.appendln(" .append(\"b64(\")").formatln(" .append(%s.toBase64())", field.param()).appendln(" .append(')');");
break;
case MESSAGE:
writer.formatln(" .append(%s.asString());", field.param());
break;
default:
writer.formatln(" .append(%s.asString());", field.param());
break;
}
if (!field.alwaysPresent()) {
writer.end().appendln('}');
if (!alwaysAfter && firstFirstCheck) {
firstFirstCheck = false;
}
} else {
alwaysAfter = true;
}
}
writer.appendln("out.append('}');").appendln("return out.toString();").end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField 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.JField in project providence by morimekta.
the class CommonOverridesFormatter method appendEquals.
private void appendEquals(JMessage<?> message) {
writer.appendln("@Override").appendln("public boolean equals(Object o) {").begin().appendln("if (o == this) return true;").appendln("if (o == null || !o.getClass().equals(getClass())) return false;");
if (message.numericalOrderFields().size() > 0) {
boolean first = true;
writer.formatln("%s other = (%s) o;", message.instanceType(), message.instanceType()).appendln("return ");
if (message.isUnion()) {
writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), UNION_FIELD, UNION_FIELD);
first = false;
}
for (JField field : message.declaredOrderFields()) {
if (field.isVoid())
continue;
if (first) {
first = false;
} else {
writer.append(" &&").appendln(" ");
}
writer.format("%s.equals(%s, other.%s)", Objects.class.getName(), field.member(), field.member());
}
writer.append(';');
} else {
writer.appendln("return true;");
}
writer.end().appendln("}").newline();
}
Aggregations