sealed classes for java17

This commit is contained in:
andrew (from workstation) 2021-10-23 18:06:47 +02:00
parent 599c05f320
commit f1d5343760
3 changed files with 102 additions and 25 deletions

View File

@ -27,6 +27,10 @@ cmp_natives.remove("String")
cmp_natives.remove("byte[]")
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def split_docs(docs: typing.List[str]) -> typing.List[str]:
tokens = " ".join(docs).split(" ")
result = [tokens[0]]
@ -301,9 +305,11 @@ def serialize_native(output: CodeWriter, arg_type: str, arg_name: str, null_chec
output.close_block(space=True)
def main(input_path: str, output_path: str, headers_path: str):
def main(input_path: str, output_path: str, headers_path: str, java17: str):
data_input = open(input_path)
java17 = java17 == "true"
package: typing.Optional[str] = None
current_constructor: typing.Optional[int] = None
current_class_name: typing.Optional[str] = None
@ -490,7 +496,27 @@ def main(input_path: str, output_path: str, headers_path: str):
output.newline()
output.indent()
output.open_custom_block(f"public abstract static class", container_class_name, "extends Object")
allowed_classess = []
for classes in (object_classes, function_classes):
for class_name, class_meta in classes.items():
if container_class_name == class_meta[1]:
allowed_classess.append(class_name)
allowed_classess_str = "permits "
for i, allowed_classess_group in enumerate(chunker(allowed_classess, 3)):
if i:
allowed_classess_str += "\n"
allowed_classess_str += output.indent_chr * (output.indent_depth + 1)
allowed_classess_str += ", ".join(allowed_classess_group)
if allowed_classess and java17:
output.open_custom_block(f"public abstract static sealed class", container_class_name, "extends Object", allowed_classess_str)
else:
output.open_custom_block(f"public abstract static class", container_class_name, "extends Object")
output.close_block()
output.newline()
@ -519,7 +545,7 @@ def main(input_path: str, output_path: str, headers_path: str):
class_generics += docs.split("@link ")[1].split()[0]
class_generics += ">"
output.open_custom_block("public static class", class_name, "extends", class_meta[1] + class_generics)
output.open_custom_block("public static final class", class_name, "extends", class_meta[1] + class_generics)
for arg_type, arg_name, docs in class_meta[2]:
output.newline()
@ -572,11 +598,10 @@ def main(input_path: str, output_path: str, headers_path: str):
output.indent()
output.write_docs(doc)
output.newline()
output.indent()
output.write_docs()
for arg_name, arg_type, docs in class_meta[2]:
output.newline()
output.indent()
output.write_docs()
docs = split_docs(docs)
output.newline()
output.indent()
@ -607,6 +632,20 @@ def main(input_path: str, output_path: str, headers_path: str):
output.close_block(space=True)
output.newline()
output.indent()
output.open_docs()
for docs in class_meta[3]:
for doc in split_docs([docs]):
output.newline()
output.indent()
output.write_docs(doc)
output.newline()
output.indent()
output.close_docs()
output.newline()
output.indent()
output.open_constructor_function(class_name, [("DataInput", "input")], "IOException")
output.newline()
@ -897,4 +936,4 @@ def main(input_path: str, output_path: str, headers_path: str):
if __name__ == '__main__':
main(sys.argv[-3], sys.argv[-2], sys.argv[-1])
main(sys.argv[-4], sys.argv[-3], sys.argv[-2], sys.argv[-1])

View File

@ -7,6 +7,23 @@ indent = "\t"
class CodeWriter:
fd: typing.TextIO
indent_depth: int
indent_chr = indent
def split_args(self, args: str) -> typing.List[str]:
tokens = args.split(", ")
result = [tokens[0]]
for token in tokens[1:]:
if len(", ".join(result[-1]) + token) < 120:
result[-1] += f", {token}"
else:
result.append(token)
if len(result) > 1 and len(result[-1].split(", ")) < 3:
result[-2] += f", {result[-1]}"
del result[-1]
return result
def __init__(self, file: typing.TextIO, indent_depth: int = 0):
self.fd = file
@ -99,10 +116,31 @@ class CodeWriter:
def open_constructor_function(self, name: str, args: typing.List[typing.Tuple[str, str]], e: str = None):
self.indent_depth += 1
result = "public " + name + "(" + ", ".join((t + " " + n for t, n in args)) + ")"
result = "public " + name + "("
result_args = ", ".join((t + " " + n for t, n in args))
if len(result_args) > 60:
result_args = result_args.split(", ")
for i, result_arg in enumerate(result_args):
if i:
result += (self.indent_depth + 1) * indent
result += result_arg
if i != len(result_args) - 1:
result += ",\n"
else:
result += result_args
result += ")"
if e:
result += " throws " + e
result += " {"
self.fd.write(result)
def declare(self, name: str, typ: str, flags: str, value: str = None):

View File

@ -9,25 +9,25 @@ import java.io.DataInput;
import java.util.Arrays;
import java.util.Objects;
public class TdApi {
public abstract static class Object {
public native String toString();
public abstract static class Object {
public native String toString();
public abstract int getConstructor();
public abstract int getConstructor();
public byte[] serialize() throws IOException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try(DataOutputStream out = new DataOutputStream(baos)) {
serialize(out);
return baos.toByteArray();
}
}
}
public byte[] serialize() throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (DataOutputStream out = new DataOutputStream(baos)) {
serialize(out);
return baos.toByteArray();
}
}
}
public abstract void serialize(DataOutput out) throws IOException;
public abstract void serialize(DataOutput out) throws IOException;
}
public abstract static class Function<R extends TdApi.Object> extends TdApi.Object {
public native String toString();
}
public abstract static class Function<R extends TdApi.Object> extends TdApi.Object {
public native String toString();
}