Generate a method to get TLObject from ID

This commit is contained in:
Lonami Exo 2018-07-23 22:49:13 +02:00
parent 3960febf3c
commit 16ab4e3863
2 changed files with 22 additions and 3 deletions

View File

@ -18,10 +18,10 @@ public class Generator {
writeAbstract(writer, types);
}
try (final Writer writer = new BufferedWriter(new FileWriter(typesFile))) {
writeCode(writer, "Types", null, types);
writeCode(writer, "Types", null, types, true);
}
try (final Writer writer = new BufferedWriter(new FileWriter(functionsFile))) {
writeCode(writer, "Functions", "TLRequest", functions);
writeCode(writer, "Functions", "TLRequest", functions, false);
}
}
@ -49,7 +49,7 @@ public class Generator {
private static void writeCode(
final Writer writer, final String className, final String extendsName,
final List<TLObject> objects) throws IOException {
final List<TLObject> objects, final boolean generateGetList) throws IOException {
writer.write("package io.github.lonamiwebs.overgram.tl;\n");
writer.write("import io.github.lonamiwebs.overgram.utils.BinaryReader;\n");
writer.write("import io.github.lonamiwebs.overgram.utils.BinaryWriter;\n");
@ -124,6 +124,25 @@ public class Generator {
writer.write("}\n");
}
}
if (generateGetList) {
writer.write("public static TLObject getFromId(final int id) throws ClassNotFoundException {\n");
writer.write("switch (id) {\n");
for (final TLObject object : objects) {
writer.write("case " + object.code);
writer.write(":\n");
writer.write("return new Types.");
writer.write(TLObject.toCamelCase(object.fullname));
writer.write("();\n");
}
writer.write("default:\n");
writer.write("throw new ClassNotFoundException();\n");
writer.write("}\n");
writer.write("}\n");
}
writer.write("}\n");
}