Support setting and getting properties
This commit is contained in:
parent
d546801130
commit
b61398d166
|
@ -77,23 +77,44 @@ public class Generator {
|
||||||
writer.write(" {\n");
|
writer.write(" {\n");
|
||||||
|
|
||||||
for (int i = 0; i < object.args.size(); ++i) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
writer.write("private ");
|
writer.write("private ");
|
||||||
writer.write(object.args.get(i).javaType());
|
writer.write(arg.javaType());
|
||||||
writer.write(" " + VARIABLE_SUFFIX + i);
|
writer.write(" " + VARIABLE_SUFFIX + i);
|
||||||
writer.write(";\n");
|
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("public ");
|
||||||
writer.write(TLObject.toCamelCase(object.name));
|
writer.write(TLObject.toCamelCase(object.name));
|
||||||
writer.write("() {}\n");
|
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);
|
writeSerialize(writer, object);
|
||||||
writeDeserialize(writer, object);
|
writeDeserialize(writer, object);
|
||||||
|
|
||||||
|
|
|
@ -131,6 +131,43 @@ public class TLArg {
|
||||||
return types.size() == 1 && types.get(0).equals("true") ? "" : " != null";
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder builder = new StringBuilder();
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
|
Loading…
Reference in New Issue