From 16ab4e3863f4c666752f5c457cd39c5fccb5a021 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 23 Jul 2018 22:49:13 +0200 Subject: [PATCH] Generate a method to get TLObject from ID --- .../overgram/generator/Generator.java | 25 ++++++++++++++++--- .../overgram/utils/CharacterIterator.java | 0 2 files changed, 22 insertions(+), 3 deletions(-) rename {lib => generator}/src/main/java/io/github/lonamiwebs/overgram/utils/CharacterIterator.java (100%) diff --git a/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java b/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java index 50ff8af..d71cb98 100644 --- a/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java +++ b/generator/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java @@ -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 objects) throws IOException { + final List 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"); } diff --git a/lib/src/main/java/io/github/lonamiwebs/overgram/utils/CharacterIterator.java b/generator/src/main/java/io/github/lonamiwebs/overgram/utils/CharacterIterator.java similarity index 100% rename from lib/src/main/java/io/github/lonamiwebs/overgram/utils/CharacterIterator.java rename to generator/src/main/java/io/github/lonamiwebs/overgram/utils/CharacterIterator.java