use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class CoreOverridesFormatter method appendFieldEnum.
private void appendFieldEnum(JMessage<?> message) throws GeneratorException {
writer.formatln("public enum _Field implements %s {", PField.class.getName()).begin();
for (JField field : message.numericalOrderFields()) {
String provider = field.getProvider();
String defValue = "null";
if (field.field().hasDefaultValue()) {
defValue = String.format(Locale.US, "new %s<>(%s)", PDefaultValueProvider.class.getName(), field.kDefault());
}
writer.formatln("%s(%d, %s.%s, \"%s\", %s, %s),", field.fieldEnum(), field.id(), PRequirement.class.getName(), field.field().getRequirement().name(), field.name(), provider, defValue);
}
writer.appendln(';').newline();
writer.appendln("private final int mId;").formatln("private final %s mRequired;", PRequirement.class.getName()).appendln("private final String mName;").formatln("private final %s mTypeProvider;", PDescriptorProvider.class.getName()).formatln("private final %s<?> mDefaultValue;", PValueProvider.class.getName()).newline().formatln("_Field(int id, %s required, String name, %s typeProvider, %s<?> defaultValue) {", PRequirement.class.getName(), PDescriptorProvider.class.getName(), PValueProvider.class.getName()).begin().appendln("mId = id;").appendln("mRequired = required;").appendln("mName = name;").appendln("mTypeProvider = typeProvider;").appendln("mDefaultValue = defaultValue;").end().appendln('}').newline();
writer.appendln("@Override").appendln("public int getId() { return mId; }").newline();
writer.appendln("@Override").formatln("public %s getRequirement() { return mRequired; }", PRequirement.class.getName()).newline();
writer.appendln("@Override").formatln("public %s getDescriptor() { return mTypeProvider.descriptor(); }", PDescriptor.class.getName()).newline();
writer.appendln("@Override").appendln("public String getName() { return mName; }").newline();
writer.appendln("@Override").appendln("public boolean hasDefaultValue() { return mDefaultValue != null; }").newline();
writer.appendln("@Override").appendln("public Object getDefaultValue() {").appendln(" return hasDefaultValue() ? mDefaultValue.get() : null;").appendln('}').newline();
writer.appendln("@Override").appendln("public String toString() {").formatln(" return %s.asString(this);", PField.class.getName()).appendln('}').newline();
new BlockCommentBuilder(writer).param_("id", "Field name").return_("The identified field or null").finish();
writer.appendln("public static _Field findById(int id) {").begin().appendln("switch (id) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %d: return _Field.%s;", field.id(), field.fieldEnum());
}
writer.end().appendln('}').appendln("return null;").end().appendln('}').newline();
new BlockCommentBuilder(writer).param_("name", "Field name").return_("The named field or null").finish();
writer.appendln("public static _Field findByName(String name) {").begin().appendln("switch (name) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case \"%s\": return _Field.%s;", field.name(), field.fieldEnum());
}
writer.end().appendln('}').appendln("return null;").end().appendln('}');
new BlockCommentBuilder(writer).param_("id", "Field name").return_("The identified field").throws_("IllegalArgumentException", "If no such field").finish();
writer.appendln("public static _Field fieldForId(int id) {").begin().appendln("_Field field = findById(id);").appendln("if (field == null) {").formatln(" throw new IllegalArgumentException(\"No such field id \" + id + \" in %s\");", message.descriptor().getQualifiedName()).appendln("}").appendln("return field;").end().appendln('}').newline();
new BlockCommentBuilder(writer).param_("name", "Field name").return_("The named field").throws_("IllegalArgumentException", "If no such field").finish();
writer.appendln("public static _Field fieldForName(String name) {").begin().appendln("_Field field = findByName(name);").appendln("if (field == null) {").formatln(" throw new IllegalArgumentException(\"No such field \\\"\" + name + \"\\\" in %s\");", message.descriptor().getQualifiedName()).appendln("}").appendln("return field;").end().appendln('}').newline();
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class BinaryWriterFormatter method appendMethods.
@Override
public void appendMethods(JMessage<?> message) {
writer.appendln("@Override").formatln("public int writeBinary(%s writer) throws %s {", BigEndianBinaryWriter.class.getName(), IOException.class.getName()).begin().appendln("int length = 0;").newline();
if (message.isUnion()) {
writer.formatln("if (%s != null) {", UNION_FIELD).begin().formatln("switch (%s) {", UNION_FIELD).begin();
for (JField field : message.numericalOrderFields()) {
writer.formatln("case %s: {", field.fieldEnum()).begin().formatln("length += writer.writeByte((byte) %d);", forType(field.type())).formatln("length += writer.writeShort((short) %d);", field.id());
appendWriteFieldValue(field.member(), field.field().getDescriptor());
writer.appendln("break;").end().appendln('}');
}
writer.appendln("default: break;").end().appendln('}').end().appendln('}');
} else {
for (JField field : message.numericalOrderFields()) {
if (!field.alwaysPresent()) {
writer.formatln("if (%s()) {", field.presence()).begin();
}
writer.formatln("length += writer.writeByte((byte) %d);", forType(field.type())).formatln("length += writer.writeShort((short) %d);", field.id());
appendWriteFieldValue(field.member(), field.field().getDescriptor());
if (!field.alwaysPresent()) {
writer.end().appendln('}');
}
writer.newline();
}
}
writer.formatln("length += writer.writeByte((byte) %d);", BinaryType.STOP);
writer.appendln("return length;").end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class HazelcastPortableMessageFormatter method appendPortableReader.
/**
* Method to append readPortable from hazelcast_portable.
*
* @param message JMessage with the information.
* <pre>
* {@code
* public void readPortable(com.hazelcast.nio.serialization.PortableReader portableReader) throws java.io.IOException {
* java.util.BitSet __temp_optionals = java.util.BitSet.valueOf(portableReader.readByteArray("__hzOptionalsForClassOptionalFields"));
* ...
* }
* }
* </pre>
*/
private void appendPortableReader(JMessage<?> message) {
writer.appendln("@Override").formatln("public void readPortable(%s %s) throws %s {", PortableReader.class.getName(), PORTABLE_READER, IOException.class.getName()).begin();
// TODO: This should be short[] instead, as field IDs are restricted to 16bit.
writer.formatln("int[] field_ids = %s.readIntArray(\"__fields__\");", PORTABLE_READER).appendln().appendln("for (int id : field_ids) {").begin().appendln("switch (id) {").begin();
for (JField field : message.declaredOrderFields()) {
writer.formatln("case %d: {", field.id()).begin();
readPortableField(field);
writer.appendln("break;").end().appendln("}");
}
writer.end().appendln(// switch
"}").end().appendln(// for loop
"}").end().appendln(// readPortable
"}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class HazelcastPortableMessageFormatter method appendPortableWriter.
/**
* Method to append writePortable from hazelcast_portable.
*
* @param message JMessage with the information.
* <pre>
* {@code
* public void writePortable(com.hazelcast.nio.serialization.PortableWriter portableWriter) throws java.io.IOException {
* ...
* portableWriter.writeByteArray("__hzOptionalsForClassOptionalFields", optionals.toByteArray());
* }
* }
* </pre>
*/
private void appendPortableWriter(JMessage<?> message) {
writer.appendln("@Override").formatln("public void writePortable(%s %s) throws %s {", PortableWriter.class.getName(), PORTABLE_WRITER, IOException.class.getName()).begin();
// TODO: This should be short[] instead, as field IDs are restricted to 16bit.
writer.appendln("int[] setFields = presentFields().stream()").appendln(" .mapToInt(_Field::getId)").appendln(" .toArray();").appendln("portableWriter.writeIntArray(\"__fields__\", setFields);");
for (JField field : message.declaredOrderFields()) {
if (!field.alwaysPresent()) {
writer.formatln("if( %s() ) {", field.isSet()).begin();
}
writePortableField(field);
if (!field.alwaysPresent()) {
writer.end().appendln("} else {").begin();
writeDefaultPortableField(field);
writer.end().appendln("}");
}
}
writer.end().appendln("}").newline();
}
use of net.morimekta.providence.generator.format.java.utils.JField in project providence by morimekta.
the class HazelcastPortableProgramFormatter method appendGetDefinition.
private void appendGetDefinition(JMessage<?> message) {
writer.formatln("public %s %s(int %s) {", ClassDefinition.class.getName(), camelCase("get", message.descriptor().getName() + "Definition"), PORTABLE_VERSION).begin().formatln("return new %s(%s, %s, %s)", ClassDefinitionBuilder.class.getName(), FACTORY_ID, getHazelcastClassId(message.instanceType()), PORTABLE_VERSION).begin().begin();
writer.formatln(".addIntArrayField(\"__fields__\")");
for (JField field : message.declaredOrderFields()) {
appendTypeField(field);
}
writer.appendln(".build();").end().end().end().appendln("}").newline();
}
Aggregations