use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.
the class CommonMemberFormatter method appendCreateConstructor.
private void appendCreateConstructor(JMessage<?> message) throws GeneratorException {
if (message.isUnion()) {
for (JField field : message.declaredOrderFields()) {
BlockCommentBuilder block = new BlockCommentBuilder(writer);
if (field.hasComment()) {
block.comment(field.comment());
}
if (field.isVoid()) {
block.return_("The created union.").finish();
writer.formatln("public static %s %s() {", message.instanceType(), camelCase("with", field.name())).begin().formatln("return new _Builder().%s().build();", field.setter()).end().appendln('}').newline();
} else {
block.param_("value", "The union value").return_("The created union.").finish();
writer.formatln("public static %s %s(%s value) {", message.instanceType(), camelCase("with", field.name()), field.valueType()).begin().formatln("return new _Builder().%s(value).build();", field.setter()).end().appendln('}').newline();
if (field.type() == PType.MESSAGE) {
appendCreateConstructorBuilderOverload(message, field);
}
}
}
} else {
if (message.hasAnnotation(ThriftAnnotation.JAVA_PUBLIC_CONSTRUCTOR) || javaOptions.public_constructors) {
String spaces = message.instanceType().replaceAll("[\\S]", " ");
writer.formatln("public %s(", message.instanceType()).begin(" " + spaces);
boolean first = true;
for (JField field : message.declaredOrderFields()) {
if (field.isVoid()) {
// Void fields have no value.
continue;
}
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
writer.format("%s %s", field.paramType(), field.param());
}
writer.end().append(") {").begin();
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(%s);", msg.get().param()).newline();
} else {
writer.appendln("super(createMessage(").begin(" ");
first = true;
for (JField field : message.declaredOrderFields()) {
// Void fields have no value.
if (field.isVoid()) {
continue;
}
if (first) {
first = false;
} else {
writer.append(',').appendln();
}
writer.format("%s", field.param());
}
writer.append("));").end().newline();
}
}
for (JField field : message.declaredOrderFields()) {
// Void fields have no value.
if (field.isVoid()) {
continue;
}
switch(field.type()) {
case LIST:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
case SET:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
case MAP:
{
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.fieldInstanceCopy(field.param())).appendln("} else {");
if (field.alwaysPresent()) {
writer.formatln(" %s = %s;", field.member(), field.kDefault());
} else {
writer.formatln(" %s = null;", field.member());
}
writer.appendln('}');
break;
}
default:
{
if (field.alwaysPresent() && !(field.isRequired() && field.isPrimitiveJavaValue())) {
writer.formatln("if (%s != null) {", field.param()).formatln(" %s = %s;", field.member(), field.param()).appendln("} else {").formatln(" %s = %s;", field.member(), field.kDefault()).appendln('}');
} else {
writer.formatln("%s = %s;", field.member(), field.param());
}
break;
}
}
}
writer.end().appendln('}').newline();
}
}
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.
the class CommonMemberFormatter method appendCreateConstructorBuilderOverload.
private void appendCreateConstructorBuilderOverload(JMessage<?> message, JField field) {
BlockCommentBuilder block = new BlockCommentBuilder(writer);
if (field.hasComment()) {
block.comment(field.comment());
}
block.param_("value", "The union value").return_("The created union.").finish();
writer.formatln("public static %s %s(%s value) {", message.instanceType(), camelCase("with", field.name()), field.builderMutableType()).begin().formatln("return %s(value == null ? null : value.build());", camelCase("with", field.name())).end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder 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.BlockCommentBuilder in project providence by morimekta.
the class BaseEnumFormatter method appendEnumClass.
public void appendEnumClass(CEnumDescriptor type) throws GeneratorException {
String simpleClass = JUtils.getClassName(type);
BlockCommentBuilder classComment = null;
if (type.getDocumentation() != null) {
classComment = new BlockCommentBuilder(writer);
classComment.comment(type.getDocumentation());
}
String deprecatedReason = type.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(type));
writer.formatln("public enum %s", simpleClass).begin();
Set<String> impl = new LinkedHashSet<>();
formatters.forEach(f -> impl.addAll(f.getExtraImplements(type)));
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(" {");
appendEnumValues(type);
appendEnumFields(type);
appendEnumConstructor(type);
formatters.forEach(f -> f.appendMethods(type));
formatters.forEach(f -> f.appendExtraProperties(type));
writer.end().appendln('}').newline();
}
use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder 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('}');
}
Aggregations