Support setting and getting properties

This commit is contained in:
Lonami Exo 2018-07-23 20:03:02 +02:00
parent d546801130
commit b61398d166
2 changed files with 64 additions and 6 deletions

View File

@ -77,23 +77,44 @@ public class Generator {
writer.write(" {\n");
for (int i = 0; i < object.args.size(); ++i) {
if (object.args.get(i).genericDefinition || object.args.get(i).flags) {
final TLArg arg = object.args.get(i);
if (arg.genericDefinition || arg.flags) {
continue;
}
writer.write("private ");
writer.write(object.args.get(i).javaType());
writer.write(arg.javaType());
writer.write(" " + VARIABLE_SUFFIX + i);
writer.write(";\n");
// Use the builder pattern because Java is stupid and has no optional named arguments.
// Objects will be created with nothing set and the arguments set with .argument(value).
writer.write("public ");
writer.write(TLObject.toCamelCase(object.name));
writer.write(' ');
writer.write(arg.javaName());
writer.write('(');
writer.write(arg.javaType());
writer.write(" value) {\n");
writer.write(VARIABLE_SUFFIX + i);
writer.write(" = value;\n");
writer.write("return this;\n");
writer.write("}\n");
// Also allow getting the values in a similar manner without input value .argument()
writer.write("public ");
writer.write(arg.javaType());
writer.write(' ');
writer.write(arg.javaName());
writer.write("() {\n");
writer.write("return " + VARIABLE_SUFFIX + i);
writer.write(";\n");
writer.write("}\n");
}
writer.write("public ");
writer.write(TLObject.toCamelCase(object.name));
writer.write("() {}\n");
// Use the builder pattern because Java is stupid and has no optional named arguments.
// Objects will be created with nothing set and the arguments set with .argument(value).
// TODO
writeSerialize(writer, object);
writeDeserialize(writer, object);

View File

@ -131,6 +131,43 @@ public class TLArg {
return types.size() == 1 && types.get(0).equals("true") ? "" : " != null";
}
public static String toCamelCase(final String string) {
final StringBuilder result = new StringBuilder(string.length());
result.append(Character.toLowerCase(string.charAt(0)));
boolean upperNext = false;
for (int i = 1; i < string.length(); ++i) {
final char c = string.charAt(i);
if (c == '_') {
upperNext = true;
} else if (upperNext) {
upperNext = false;
result.append(Character.toUpperCase(c));
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
public String javaName() {
switch (name) {
case "lat":
return "latitude"; // not reserved but consistent
case "long":
return "longitude";
case "static":
return "isStatic";
case "public":
return "isPublic";
case "private":
return "isPrivate";
case "final":
return "last";
default:
return toCamelCase(name);
}
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();