mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 11:04:26 +00:00
[Kotlin] Attach JvmStatic annotation to each method in companion object (#6052)
* Attach JvmStatic annotation to each method of companion object Kotlin does not have static accessor so companion object used instead of static. It's so natural. But when use kotlin companion object methods on java it is very inconvenient. ```java GeneratedClassByFlatBuffer.Companion.someMethod() ``` If use @JvmStatic annotation it can be shorten like below. ```java GeneratedClassByFlatBuffer.someMethod() ``` * Formatting by Idea Google C++ style * Add comments - Commit for missing cla Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add comments - Commit for missing cla Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Reset code formatting except modified lines Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Pass missing flag to validateVersion method Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add annotations to missing method in companion object * addVector * createVector * endVector * tableCreator And also I tried add compiler option for generate annotation who don't like this operation. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Modify unmatched option name in compiler usage Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add missing operation for finishSizePrefixed and finishStructBuffer method. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add compiled example with --kotlin-gen-jvmstatic option. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Fix Compiler.md Change option name from --gen-jvm-static-annotation to --kotlin-gen-jvmstatic Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Use IDLOptions reference instead of bool parameter. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Change option name - kotlin_gen_jvmstatic to gen_jvmstatic Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Use IDLOptions reference instead of bool parameter and missing process @JvmStatic as suffix. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Add code generation for --gen-jvmstatic option Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Miss typo directory for including. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Miss typo variable suffix for including. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Fix camel case to snake case. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Rollback generate code for gen_jvmstatic option. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Delete generated test files. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * add missing new line at end of file. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> * Remove generated test file by command line. Signed-off-by: Yoon KyongSik <sam1287@gmail.com> Co-authored-by: sam <sam@jennifersoft.com>
This commit is contained in:
@@ -116,6 +116,9 @@ Additional options:
|
|||||||
|
|
||||||
- `--gen-generated` : Add @Generated annotation for Java.
|
- `--gen-generated` : Add @Generated annotation for Java.
|
||||||
|
|
||||||
|
- `--gen-jvmstatic` : Add @JvmStatic annotation for Kotlin methods
|
||||||
|
in companion object for interop from Java to Kotlin.
|
||||||
|
|
||||||
- `--gen-all` : Generate not just code for the current schema files, but
|
- `--gen-all` : Generate not just code for the current schema files, but
|
||||||
for all files it includes as well. If the language uses a single file for
|
for all files it includes as well. If the language uses a single file for
|
||||||
output (by default the case for C++ and JS), all code will end up in
|
output (by default the case for C++ and JS), all code will end up in
|
||||||
|
|||||||
@@ -509,6 +509,7 @@ struct ServiceDef : public Definition {
|
|||||||
|
|
||||||
// Container of options that may apply to any of the source/text generators.
|
// Container of options that may apply to any of the source/text generators.
|
||||||
struct IDLOptions {
|
struct IDLOptions {
|
||||||
|
bool gen_jvmstatic;
|
||||||
// Use flexbuffers instead for binary and text generation
|
// Use flexbuffers instead for binary and text generation
|
||||||
bool use_flexbuffers;
|
bool use_flexbuffers;
|
||||||
bool strict_json;
|
bool strict_json;
|
||||||
@@ -604,7 +605,8 @@ struct IDLOptions {
|
|||||||
bool set_empty_vectors_to_null;
|
bool set_empty_vectors_to_null;
|
||||||
|
|
||||||
IDLOptions()
|
IDLOptions()
|
||||||
: use_flexbuffers(false),
|
: gen_jvmstatic(false),
|
||||||
|
use_flexbuffers(false),
|
||||||
strict_json(false),
|
strict_json(false),
|
||||||
skip_js_exports(false),
|
skip_js_exports(false),
|
||||||
use_goog_js_export_format(false),
|
use_goog_js_export_format(false),
|
||||||
|
|||||||
@@ -106,6 +106,8 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
|||||||
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||||
" --java-checkerframe work Add @Pure for Java.\n"
|
" --java-checkerframe work Add @Pure for Java.\n"
|
||||||
" --gen-generated Add @Generated annotation for Java\n"
|
" --gen-generated Add @Generated annotation for Java\n"
|
||||||
|
" --gen-jvmstatic Add @JvmStatic annotation for Kotlin methods\n"
|
||||||
|
" in companion object for interop from Java to Kotlin.\n"
|
||||||
" --gen-all Generate not just code for the current schema files,\n"
|
" --gen-all Generate not just code for the current schema files,\n"
|
||||||
" but for all files it includes as well.\n"
|
" but for all files it includes as well.\n"
|
||||||
" If the language uses a single file for output (by default\n"
|
" If the language uses a single file for output (by default\n"
|
||||||
@@ -363,6 +365,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
|||||||
opts.cs_gen_json_serializer = true;
|
opts.cs_gen_json_serializer = true;
|
||||||
} else if (arg == "--flexbuffers") {
|
} else if (arg == "--flexbuffers") {
|
||||||
opts.use_flexbuffers = true;
|
opts.use_flexbuffers = true;
|
||||||
|
} else if (arg == "--gen-jvmstatic") {
|
||||||
|
opts.gen_jvmstatic = true;
|
||||||
} else if (arg == "--cpp-std") {
|
} else if (arg == "--cpp-std") {
|
||||||
if (++argi >= argc)
|
if (++argi >= argc)
|
||||||
Error("missing C++ standard specification" + arg, true);
|
Error("missing C++ standard specification" + arg, true);
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
auto &struct_def = **it;
|
auto &struct_def = **it;
|
||||||
if (!parser_.opts.one_file)
|
if (!parser_.opts.one_file)
|
||||||
cur_name_space_ = struct_def.defined_namespace;
|
cur_name_space_ = struct_def.defined_namespace;
|
||||||
GenStruct(struct_def, structWriter);
|
GenStruct(struct_def, structWriter, parser_.opts);
|
||||||
if (parser_.opts.one_file) {
|
if (parser_.opts.one_file) {
|
||||||
one_file_code += structWriter.ToString();
|
one_file_code += structWriter.ToString();
|
||||||
} else {
|
} else {
|
||||||
@@ -276,7 +276,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
if (enum_def.MinValue()->IsNonZero())
|
if (enum_def.MinValue()->IsNonZero())
|
||||||
writer += " - " + enum_def.MinValue()->name + ".toInt()\\";
|
writer += " - " + enum_def.MinValue()->name + ".toInt()\\";
|
||||||
writer += "]";
|
writer += "]";
|
||||||
});
|
}, parser_.opts.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
writer.DecrementIdentLevel();
|
writer.DecrementIdentLevel();
|
||||||
@@ -417,7 +417,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
return key_offset;
|
return key_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenStruct(StructDef &struct_def, CodeWriter &writer) const {
|
void GenStruct(StructDef &struct_def, CodeWriter &writer, IDLOptions options) const {
|
||||||
if (struct_def.generated) return;
|
if (struct_def.generated) return;
|
||||||
|
|
||||||
GenerateComment(struct_def.doc_comment, writer, &comment_config);
|
GenerateComment(struct_def.doc_comment, writer, &comment_config);
|
||||||
@@ -458,13 +458,13 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
// runtime.
|
// runtime.
|
||||||
GenerateFunOneLine(writer, "validateVersion", "", "", [&]() {
|
GenerateFunOneLine(writer, "validateVersion", "", "", [&]() {
|
||||||
writer += "Constants.FLATBUFFERS_1_12_0()";
|
writer += "Constants.FLATBUFFERS_1_12_0()";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
|
|
||||||
GenerateGetRootAsAccessors(Esc(struct_def.name), writer);
|
GenerateGetRootAsAccessors(Esc(struct_def.name), writer, options);
|
||||||
GenerateBufferHasIdentifier(struct_def, writer);
|
GenerateBufferHasIdentifier(struct_def, writer, options);
|
||||||
GenerateTableCreator(struct_def, writer);
|
GenerateTableCreator(struct_def, writer, options);
|
||||||
|
|
||||||
GenerateStartStructMethod(struct_def, writer);
|
GenerateStartStructMethod(struct_def, writer, options);
|
||||||
|
|
||||||
// Static Add for fields
|
// Static Add for fields
|
||||||
auto fields = struct_def.fields.vec;
|
auto fields = struct_def.fields.vec;
|
||||||
@@ -474,29 +474,29 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
field_pos++;
|
field_pos++;
|
||||||
if (field.deprecated) continue;
|
if (field.deprecated) continue;
|
||||||
if (field.key) key_field = &field;
|
if (field.key) key_field = &field;
|
||||||
GenerateAddField(NumToString(field_pos), field, writer);
|
GenerateAddField(NumToString(field_pos), field, writer, options);
|
||||||
|
|
||||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||||
auto vector_type = field.value.type.VectorType();
|
auto vector_type = field.value.type.VectorType();
|
||||||
if (!IsStruct(vector_type)) {
|
if (!IsStruct(vector_type)) {
|
||||||
GenerateCreateVectorField(field, writer);
|
GenerateCreateVectorField(field, writer, options);
|
||||||
}
|
}
|
||||||
GenerateStartVectorField(field, writer);
|
GenerateStartVectorField(field, writer, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GenerateEndStructMethod(struct_def, writer);
|
GenerateEndStructMethod(struct_def, writer, options);
|
||||||
auto file_identifier = parser_.file_identifier_;
|
auto file_identifier = parser_.file_identifier_;
|
||||||
if (parser_.root_struct_def_ == &struct_def) {
|
if (parser_.root_struct_def_ == &struct_def) {
|
||||||
GenerateFinishStructBuffer(struct_def, file_identifier, writer);
|
GenerateFinishStructBuffer(struct_def, file_identifier, writer, options);
|
||||||
GenerateFinishSizePrefixed(struct_def, file_identifier, writer);
|
GenerateFinishSizePrefixed(struct_def, file_identifier, writer, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (struct_def.has_key) {
|
if (struct_def.has_key) {
|
||||||
GenerateLookupByKey(key_field, struct_def, writer);
|
GenerateLookupByKey(key_field, struct_def, writer, options);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
GenerateStaticConstructor(struct_def, writer);
|
GenerateStaticConstructor(struct_def, writer, options);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -508,7 +508,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
// TODO: move key_field to reference instead of pointer
|
// TODO: move key_field to reference instead of pointer
|
||||||
void GenerateLookupByKey(FieldDef *key_field, StructDef &struct_def,
|
void GenerateLookupByKey(FieldDef *key_field, StructDef &struct_def,
|
||||||
CodeWriter &writer) const {
|
CodeWriter &writer, const IDLOptions options) const {
|
||||||
std::stringstream params;
|
std::stringstream params;
|
||||||
params << "obj: " << Esc(struct_def.name) << "?"
|
params << "obj: " << Esc(struct_def.name) << "?"
|
||||||
<< ", ";
|
<< ", ";
|
||||||
@@ -564,31 +564,34 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
writer += "return null";
|
writer += "return null";
|
||||||
};
|
};
|
||||||
GenerateFun(writer, "__lookup_by_key", params.str(),
|
GenerateFun(writer, "__lookup_by_key", params.str(),
|
||||||
Esc(struct_def.name) + "?", statements);
|
Esc(struct_def.name) + "?", statements, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateFinishSizePrefixed(StructDef &struct_def,
|
void GenerateFinishSizePrefixed(StructDef &struct_def,
|
||||||
const std::string &identifier,
|
const std::string &identifier,
|
||||||
CodeWriter &writer) const {
|
CodeWriter &writer,
|
||||||
|
const IDLOptions options) const {
|
||||||
auto id = identifier.length() > 0 ? ", \"" + identifier + "\"" : "";
|
auto id = identifier.length() > 0 ? ", \"" + identifier + "\"" : "";
|
||||||
auto params = "builder: FlatBufferBuilder, offset: Int";
|
auto params = "builder: FlatBufferBuilder, offset: Int";
|
||||||
auto method_name = "finishSizePrefixed" + Esc(struct_def.name) + "Buffer";
|
auto method_name = "finishSizePrefixed" + Esc(struct_def.name) + "Buffer";
|
||||||
GenerateFunOneLine(writer, method_name, params, "", [&]() {
|
GenerateFunOneLine(writer, method_name, params, "", [&]() {
|
||||||
writer += "builder.finishSizePrefixed(offset" + id + ")";
|
writer += "builder.finishSizePrefixed(offset" + id + ")";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
void GenerateFinishStructBuffer(StructDef &struct_def,
|
void GenerateFinishStructBuffer(StructDef &struct_def,
|
||||||
const std::string &identifier,
|
const std::string &identifier,
|
||||||
CodeWriter &writer) const {
|
CodeWriter &writer,
|
||||||
|
const IDLOptions options) const {
|
||||||
auto id = identifier.length() > 0 ? ", \"" + identifier + "\"" : "";
|
auto id = identifier.length() > 0 ? ", \"" + identifier + "\"" : "";
|
||||||
auto params = "builder: FlatBufferBuilder, offset: Int";
|
auto params = "builder: FlatBufferBuilder, offset: Int";
|
||||||
auto method_name = "finish" + Esc(struct_def.name) + "Buffer";
|
auto method_name = "finish" + Esc(struct_def.name) + "Buffer";
|
||||||
GenerateFunOneLine(writer, method_name, params, "",
|
GenerateFunOneLine(writer, method_name, params, "",
|
||||||
[&]() { writer += "builder.finish(offset" + id + ")"; });
|
[&]() { writer += "builder.finish(offset" + id + ")"; },
|
||||||
|
options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateEndStructMethod(StructDef &struct_def,
|
void GenerateEndStructMethod(StructDef &struct_def, CodeWriter &writer,
|
||||||
CodeWriter &writer) const {
|
const IDLOptions options) const {
|
||||||
// Generate end{{TableName}}(builder: FlatBufferBuilder) method
|
// Generate end{{TableName}}(builder: FlatBufferBuilder) method
|
||||||
auto name = "end" + Esc(struct_def.name);
|
auto name = "end" + Esc(struct_def.name);
|
||||||
auto params = "builder: FlatBufferBuilder";
|
auto params = "builder: FlatBufferBuilder";
|
||||||
@@ -606,11 +609,12 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
writer.DecrementIdentLevel();
|
writer.DecrementIdentLevel();
|
||||||
writer += "return o";
|
writer += "return o";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a method to create a vector from a Kotlin array.
|
// Generate a method to create a vector from a Kotlin array.
|
||||||
void GenerateCreateVectorField(FieldDef &field, CodeWriter &writer) const {
|
void GenerateCreateVectorField(FieldDef &field, CodeWriter &writer,
|
||||||
|
const IDLOptions options) const {
|
||||||
auto vector_type = field.value.type.VectorType();
|
auto vector_type = field.value.type.VectorType();
|
||||||
auto method_name = "create" + MakeCamel(Esc(field.name)) + "Vector";
|
auto method_name = "create" + MakeCamel(Esc(field.name)) + "Vector";
|
||||||
auto params = "builder: FlatBufferBuilder, data: " +
|
auto params = "builder: FlatBufferBuilder, data: " +
|
||||||
@@ -628,10 +632,11 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
writer.DecrementIdentLevel();
|
writer.DecrementIdentLevel();
|
||||||
writer += "}";
|
writer += "}";
|
||||||
writer += "return builder.endVector()";
|
writer += "return builder.endVector()";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateStartVectorField(FieldDef &field, CodeWriter &writer) const {
|
void GenerateStartVectorField(FieldDef &field, CodeWriter &writer,
|
||||||
|
const IDLOptions options) const {
|
||||||
// Generate a method to start a vector, data to be added manually
|
// Generate a method to start a vector, data to be added manually
|
||||||
// after.
|
// after.
|
||||||
auto vector_type = field.value.type.VectorType();
|
auto vector_type = field.value.type.VectorType();
|
||||||
@@ -643,11 +648,11 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
writer, "start" + MakeCamel(Esc(field.name) + "Vector", true), params,
|
writer, "start" + MakeCamel(Esc(field.name) + "Vector", true), params,
|
||||||
"", [&]() {
|
"", [&]() {
|
||||||
writer += "builder.startVector({{size}}, numElems, {{align}})";
|
writer += "builder.startVector({{size}}, numElems, {{align}})";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateAddField(std::string field_pos, FieldDef &field,
|
void GenerateAddField(std::string field_pos, FieldDef &field,
|
||||||
CodeWriter &writer) const {
|
CodeWriter &writer, const IDLOptions options) const {
|
||||||
auto field_type = GenTypeBasic(field.value.type.base_type);
|
auto field_type = GenTypeBasic(field.value.type.base_type);
|
||||||
auto secondArg = MakeCamel(Esc(field.name), false) + ": " + field_type;
|
auto secondArg = MakeCamel(Esc(field.name), false) + ": " + field_type;
|
||||||
GenerateFunOneLine(writer, "add" + MakeCamel(Esc(field.name), true),
|
GenerateFunOneLine(writer, "add" + MakeCamel(Esc(field.name), true),
|
||||||
@@ -662,7 +667,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
writer += "builder.add{{method_name}}({{pos}}, \\";
|
writer += "builder.add{{method_name}}({{pos}}, \\";
|
||||||
writer += "{{field_name}}{{cast}}, {{default}})";
|
writer += "{{field_name}}{{cast}}, {{default}})";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string ToSignedType(const Type &type) {
|
static std::string ToSignedType(const Type &type) {
|
||||||
@@ -703,17 +708,18 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fun startMonster(builder: FlatBufferBuilder) = builder.startTable(11)
|
// fun startMonster(builder: FlatBufferBuilder) = builder.startTable(11)
|
||||||
void GenerateStartStructMethod(StructDef &struct_def,
|
void GenerateStartStructMethod(StructDef &struct_def, CodeWriter &code,
|
||||||
CodeWriter &code) const {
|
const IDLOptions options) const {
|
||||||
GenerateFunOneLine(code, "start" + Esc(struct_def.name),
|
GenerateFunOneLine(code, "start" + Esc(struct_def.name),
|
||||||
"builder: FlatBufferBuilder", "", [&]() {
|
"builder: FlatBufferBuilder", "", [&]() {
|
||||||
code += "builder.startTable(" +
|
code += "builder.startTable(" +
|
||||||
NumToString(struct_def.fields.vec.size()) +
|
NumToString(struct_def.fields.vec.size()) +
|
||||||
")";
|
")";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateTableCreator(StructDef &struct_def, CodeWriter &writer) const {
|
void GenerateTableCreator(StructDef &struct_def, CodeWriter &writer,
|
||||||
|
const IDLOptions options) const {
|
||||||
// Generate a method that creates a table in one go. This is only possible
|
// Generate a method that creates a table in one go. This is only possible
|
||||||
// when the table has no struct fields, since those have to be created
|
// when the table has no struct fields, since those have to be created
|
||||||
// inline, and there's no way to do so in Java.
|
// inline, and there's no way to do so in Java.
|
||||||
@@ -776,11 +782,11 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer += "return end{{struct_name}}(builder)";
|
writer += "return end{{struct_name}}(builder)";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void GenerateBufferHasIdentifier(StructDef &struct_def,
|
void GenerateBufferHasIdentifier(StructDef &struct_def,
|
||||||
CodeWriter &writer) const {
|
CodeWriter &writer, IDLOptions options) const {
|
||||||
auto file_identifier = parser_.file_identifier_;
|
auto file_identifier = parser_.file_identifier_;
|
||||||
// Check if a buffer has the identifier.
|
// Check if a buffer has the identifier.
|
||||||
if (parser_.root_struct_def_ != &struct_def || !file_identifier.length())
|
if (parser_.root_struct_def_ != &struct_def || !file_identifier.length())
|
||||||
@@ -790,7 +796,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
"Boolean", [&]() {
|
"Boolean", [&]() {
|
||||||
writer += "__has_identifier(_bb, \"" +
|
writer += "__has_identifier(_bb, \"" +
|
||||||
file_identifier + "\")";
|
file_identifier + "\")";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateStructGetters(StructDef &struct_def, CodeWriter &writer) const {
|
void GenerateStructGetters(StructDef &struct_def, CodeWriter &writer) const {
|
||||||
@@ -1218,18 +1224,21 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void GenerateGetRootAsAccessors(const std::string &struct_name,
|
static void GenerateGetRootAsAccessors(const std::string &struct_name,
|
||||||
CodeWriter &writer) {
|
CodeWriter &writer,
|
||||||
|
IDLOptions options) {
|
||||||
// Generate a special accessor for the table that when used as the root
|
// Generate a special accessor for the table that when used as the root
|
||||||
// ex: fun getRootAsMonster(_bb: ByteBuffer): Monster {...}
|
// ex: fun getRootAsMonster(_bb: ByteBuffer): Monster {...}
|
||||||
writer.SetValue("gr_name", struct_name);
|
writer.SetValue("gr_name", struct_name);
|
||||||
writer.SetValue("gr_method", "getRootAs" + struct_name);
|
writer.SetValue("gr_method", "getRootAs" + struct_name);
|
||||||
|
|
||||||
// create convenience method that doesn't require an existing object
|
// create convenience method that doesn't require an existing object
|
||||||
|
GenerateJvmStaticAnnotation(writer, options.gen_jvmstatic);
|
||||||
writer += "fun {{gr_method}}(_bb: ByteBuffer): {{gr_name}} = \\";
|
writer += "fun {{gr_method}}(_bb: ByteBuffer): {{gr_name}} = \\";
|
||||||
writer += "{{gr_method}}(_bb, {{gr_name}}())";
|
writer += "{{gr_method}}(_bb, {{gr_name}}())";
|
||||||
|
|
||||||
// create method that allows object reuse
|
// create method that allows object reuse
|
||||||
// ex: fun Monster getRootAsMonster(_bb: ByteBuffer, obj: Monster) {...}
|
// ex: fun Monster getRootAsMonster(_bb: ByteBuffer, obj: Monster) {...}
|
||||||
|
GenerateJvmStaticAnnotation(writer, options.gen_jvmstatic);
|
||||||
writer +=
|
writer +=
|
||||||
"fun {{gr_method}}"
|
"fun {{gr_method}}"
|
||||||
"(_bb: ByteBuffer, obj: {{gr_name}}): {{gr_name}} {";
|
"(_bb: ByteBuffer, obj: {{gr_name}}): {{gr_name}} {";
|
||||||
@@ -1243,13 +1252,14 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void GenerateStaticConstructor(const StructDef &struct_def,
|
static void GenerateStaticConstructor(const StructDef &struct_def,
|
||||||
CodeWriter &code) {
|
CodeWriter &code,
|
||||||
|
const IDLOptions options) {
|
||||||
// create a struct constructor function
|
// create a struct constructor function
|
||||||
auto params = StructConstructorParams(struct_def);
|
auto params = StructConstructorParams(struct_def);
|
||||||
GenerateFun(code, "create" + Esc(struct_def.name), params, "Int", [&]() {
|
GenerateFun(code, "create" + Esc(struct_def.name), params, "Int", [&]() {
|
||||||
GenStructBody(struct_def, code, "");
|
GenStructBody(struct_def, code, "");
|
||||||
code += "return builder.offset()";
|
code += "return builder.offset()";
|
||||||
});
|
}, options.gen_jvmstatic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string StructConstructorParams(const StructDef &struct_def,
|
static std::string StructConstructorParams(const StructDef &struct_def,
|
||||||
@@ -1323,7 +1333,8 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
static void GenerateFun(CodeWriter &writer, const std::string &name,
|
static void GenerateFun(CodeWriter &writer, const std::string &name,
|
||||||
const std::string ¶ms,
|
const std::string ¶ms,
|
||||||
const std::string &returnType,
|
const std::string &returnType,
|
||||||
const std::function<void()> &body) {
|
const std::function<void()> &body,
|
||||||
|
bool gen_jvmstatic = false) {
|
||||||
// Generates Kotlin function
|
// Generates Kotlin function
|
||||||
// e.g.:
|
// e.g.:
|
||||||
// fun path(j: Int): Vec3 {
|
// fun path(j: Int): Vec3 {
|
||||||
@@ -1333,6 +1344,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
writer.SetValue("name", name);
|
writer.SetValue("name", name);
|
||||||
writer.SetValue("params", params);
|
writer.SetValue("params", params);
|
||||||
writer.SetValue("return_type", noreturn ? "" : ": " + returnType);
|
writer.SetValue("return_type", noreturn ? "" : ": " + returnType);
|
||||||
|
GenerateJvmStaticAnnotation(writer, gen_jvmstatic);
|
||||||
writer += "fun {{name}}({{params}}) {{return_type}} {";
|
writer += "fun {{name}}({{params}}) {{return_type}} {";
|
||||||
writer.IncrementIdentLevel();
|
writer.IncrementIdentLevel();
|
||||||
body();
|
body();
|
||||||
@@ -1343,7 +1355,8 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
static void GenerateFunOneLine(CodeWriter &writer, const std::string &name,
|
static void GenerateFunOneLine(CodeWriter &writer, const std::string &name,
|
||||||
const std::string ¶ms,
|
const std::string ¶ms,
|
||||||
const std::string &returnType,
|
const std::string &returnType,
|
||||||
const std::function<void()> &body) {
|
const std::function<void()> &body,
|
||||||
|
bool gen_jvmstatic = false) {
|
||||||
// Generates Kotlin function
|
// Generates Kotlin function
|
||||||
// e.g.:
|
// e.g.:
|
||||||
// fun path(j: Int): Vec3 = return path(Vec3(), j)
|
// fun path(j: Int): Vec3 = return path(Vec3(), j)
|
||||||
@@ -1351,6 +1364,7 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
writer.SetValue("params", params);
|
writer.SetValue("params", params);
|
||||||
writer.SetValue("return_type_p",
|
writer.SetValue("return_type_p",
|
||||||
returnType.empty() ? "" : " : " + returnType);
|
returnType.empty() ? "" : " : " + returnType);
|
||||||
|
GenerateJvmStaticAnnotation(writer, gen_jvmstatic);
|
||||||
writer += "fun {{name}}({{params}}){{return_type_p}} = \\";
|
writer += "fun {{name}}({{params}}){{return_type_p}} = \\";
|
||||||
body();
|
body();
|
||||||
}
|
}
|
||||||
@@ -1428,6 +1442,14 @@ class KotlinGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepend @JvmStatic to methods in companion object.
|
||||||
|
static void GenerateJvmStaticAnnotation(CodeWriter &code,
|
||||||
|
bool gen_jvmstatic) {
|
||||||
|
if (gen_jvmstatic) {
|
||||||
|
code += "@JvmStatic";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This tracks the current namespace used to determine if a type need to be
|
// This tracks the current namespace used to determine if a type need to be
|
||||||
// prefixed by its namespace
|
// prefixed by its namespace
|
||||||
const Namespace *cur_name_space_;
|
const Namespace *cur_name_space_;
|
||||||
|
|||||||
Reference in New Issue
Block a user