diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 749159245..9c23c5dd3 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -1026,20 +1026,23 @@ class GeneralGenerator : public BaseGenerator { code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey("; code += GenTypeNameDest(key_field.value.type) + " key)"; code += offset_prefix; - code += qualified_name + ".__lookup_by_key(null, "; - code += lang_.accessor_prefix + "__vector(o), key, "; - code += lang_.accessor_prefix + "bb) : null; "; - code += "}\n"; - - code += " public " + qualified_name + lang_.optional_suffix + " "; - code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey("; - code += qualified_name + lang_.optional_suffix + " obj, "; - code += GenTypeNameDest(key_field.value.type) + " key)"; - code += offset_prefix; - code += qualified_name + ".__lookup_by_key(obj, "; + code += qualified_name + ".__lookup_by_key("; + if (lang_.language == IDLOptions::kJava) + code += "null, "; code += lang_.accessor_prefix + "__vector(o), key, "; code += lang_.accessor_prefix + "bb) : null; "; code += "}\n"; + if (lang_.language == IDLOptions::kJava) { + code += " public " + qualified_name + lang_.optional_suffix + " "; + code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey("; + code += qualified_name + lang_.optional_suffix + " obj, "; + code += GenTypeNameDest(key_field.value.type) + " key)"; + code += offset_prefix; + code += qualified_name + ".__lookup_by_key(obj, "; + code += lang_.accessor_prefix + "__vector(o), key, "; + code += lang_.accessor_prefix + "bb) : null; "; + code += "}\n"; + } break; } } @@ -1363,8 +1366,10 @@ class GeneralGenerator : public BaseGenerator { } code += "\n public static " + struct_def.name + lang_.optional_suffix; - code += " __lookup_by_key(" + struct_def.name; - code += " obj, int vectorLocation, "; + code += " __lookup_by_key("; + if (lang_.language == IDLOptions::kJava) + code += struct_def.name + " obj, "; + code += "int vectorLocation, "; code += GenTypeNameDest(key_field->value.type); code += " key, ByteBuffer bb) {\n"; if (key_field->value.type.base_type == BASE_TYPE_STRING) { @@ -1387,9 +1392,12 @@ class GeneralGenerator : public BaseGenerator { code += " start += middle;\n"; code += " span -= middle;\n"; code += " } else {\n"; - code += " return (obj == null ? "; - code += "new " + struct_def.name; - code += "(), obj).__assign(tableOffset, bb);\n"; + code += " return "; + if (lang_.language == IDLOptions::kJava) + code += "(obj == null ? new " + struct_def.name + "() : obj)"; + else + code += "new " + struct_def.name + "()"; + code += ".__assign(tableOffset, bb);\n"; code += " }\n }\n"; code += " return null;\n"; code += " }\n"; diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index a845ddadb..c554c3cd1 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -43,8 +43,7 @@ public struct Monster : IFlatbufferObject /// multiline too public Monster? Testarrayoftables(int j) { int o = __p.__offset(26); return o != 0 ? (Monster?)(new Monster()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; } public int TestarrayoftablesLength { get { int o = __p.__offset(26); return o != 0 ? __p.__vector_len(o) : 0; } } - public Monster? TestarrayoftablesByKey(string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(null, __p.__vector(o), key, __p.bb) : null; } - public Monster? TestarrayoftablesByKey(Monster? obj, string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(obj, __p.__vector(o), key, __p.bb) : null; } + public Monster? TestarrayoftablesByKey(string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; } public Monster? Enemy { get { int o = __p.__offset(28); return o != 0 ? (Monster?)(new Monster()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } } public byte Testnestedflatbuffer(int j) { int o = __p.__offset(30); return o != 0 ? __p.bb.Get(__p.__vector(o) + j * 1) : (byte)0; } public int TestnestedflatbufferLength { get { int o = __p.__offset(30); return o != 0 ? __p.__vector_len(o) : 0; } } @@ -101,8 +100,7 @@ public struct Monster : IFlatbufferObject public MyGame.InParentNamespace? ParentNamespaceTest { get { int o = __p.__offset(72); return o != 0 ? (MyGame.InParentNamespace?)(new MyGame.InParentNamespace()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } } public Referrable? VectorOfReferrables(int j) { int o = __p.__offset(74); return o != 0 ? (Referrable?)(new Referrable()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; } public int VectorOfReferrablesLength { get { int o = __p.__offset(74); return o != 0 ? __p.__vector_len(o) : 0; } } - public Referrable? VectorOfReferrablesByKey(ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(null, __p.__vector(o), key, __p.bb) : null; } - public Referrable? VectorOfReferrablesByKey(Referrable? obj, ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(obj, __p.__vector(o), key, __p.bb) : null; } + public Referrable? VectorOfReferrablesByKey(ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; } public ulong SingleWeakReference { get { int o = __p.__offset(76); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } } public bool MutateSingleWeakReference(ulong single_weak_reference) { int o = __p.__offset(76); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, single_weak_reference); return true; } else { return false; } } public ulong VectorOfWeakReferences(int j) { int o = __p.__offset(78); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; } @@ -111,8 +109,7 @@ public struct Monster : IFlatbufferObject public bool MutateVectorOfWeakReferences(int j, ulong vector_of_weak_references) { int o = __p.__offset(78); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_weak_references); return true; } else { return false; } } public Referrable? VectorOfStrongReferrables(int j) { int o = __p.__offset(80); return o != 0 ? (Referrable?)(new Referrable()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; } public int VectorOfStrongReferrablesLength { get { int o = __p.__offset(80); return o != 0 ? __p.__vector_len(o) : 0; } } - public Referrable? VectorOfStrongReferrablesByKey(ulong key) { int o = __p.__offset(80); return o != 0 ? Referrable.__lookup_by_key(null, __p.__vector(o), key, __p.bb) : null; } - public Referrable? VectorOfStrongReferrablesByKey(Referrable? obj, ulong key) { int o = __p.__offset(80); return o != 0 ? Referrable.__lookup_by_key(obj, __p.__vector(o), key, __p.bb) : null; } + public Referrable? VectorOfStrongReferrablesByKey(ulong key) { int o = __p.__offset(80); return o != 0 ? Referrable.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; } public ulong CoOwningReference { get { int o = __p.__offset(82); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } } public bool MutateCoOwningReference(ulong co_owning_reference) { int o = __p.__offset(82); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, co_owning_reference); return true; } else { return false; } } public ulong VectorOfCoOwningReferences(int j) { int o = __p.__offset(84); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; } @@ -212,7 +209,7 @@ public struct Monster : IFlatbufferObject return builder.CreateVectorOfTables(offsets); } - public static Monster? __lookup_by_key(Monster obj, int vectorLocation, string key, ByteBuffer bb) { + public static Monster? __lookup_by_key(int vectorLocation, string key, ByteBuffer bb) { byte[] byteKey = System.Text.Encoding.UTF8.GetBytes(key); int span = bb.GetInt(vectorLocation - 4); int start = 0; @@ -227,7 +224,7 @@ public struct Monster : IFlatbufferObject start += middle; span -= middle; } else { - return (obj == null ? new Monster(), obj).__assign(tableOffset, bb); + return new Monster().__assign(tableOffset, bb); } } return null; diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index b5a1c9300..6048dc7b4 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -247,7 +247,7 @@ public final class Monster extends Table { start += middle; span -= middle; } else { - return (obj == null ? new Monster(), obj).__assign(tableOffset, bb); + return (obj == null ? new Monster() : obj).__assign(tableOffset, bb); } } return null; diff --git a/tests/MyGame/Example/Referrable.cs b/tests/MyGame/Example/Referrable.cs index 1367f3aa6..ce0680876 100644 --- a/tests/MyGame/Example/Referrable.cs +++ b/tests/MyGame/Example/Referrable.cs @@ -39,7 +39,7 @@ public struct Referrable : IFlatbufferObject return builder.CreateVectorOfTables(offsets); } - public static Referrable? __lookup_by_key(Referrable obj, int vectorLocation, ulong key, ByteBuffer bb) { + public static Referrable? __lookup_by_key(int vectorLocation, ulong key, ByteBuffer bb) { int span = bb.GetInt(vectorLocation - 4); int start = 0; while (span != 0) { @@ -53,7 +53,7 @@ public struct Referrable : IFlatbufferObject start += middle; span -= middle; } else { - return (obj == null ? new Referrable(), obj).__assign(tableOffset, bb); + return new Referrable().__assign(tableOffset, bb); } } return null; diff --git a/tests/MyGame/Example/Referrable.java b/tests/MyGame/Example/Referrable.java index 5d66d2a58..9398597e9 100644 --- a/tests/MyGame/Example/Referrable.java +++ b/tests/MyGame/Example/Referrable.java @@ -53,7 +53,7 @@ public final class Referrable extends Table { start += middle; span -= middle; } else { - return (obj == null ? new Referrable(), obj).__assign(tableOffset, bb); + return (obj == null ? new Referrable() : obj).__assign(tableOffset, bb); } } return null; diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 1437c7c25..041257698 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -21,7 +21,7 @@ if "%1"=="-b" set buildtype=%2 ..\%buildtype%\flatc.exe -b --schema --bfbs-comments -I include_test monster_test.fbs ..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs cd ../samples -..\%buildtype%\flatc.exe --cpp --gen-mutable --gen-object-api --cpp-ptr-type flatbuffers::unique_ptr monster.fbs +..\%buildtype%\flatc.exe --cpp --gen-mutable --reflect-names --gen-object-api --cpp-ptr-type flatbuffers::unique_ptr monster.fbs cd ../reflection cd ../tests \ No newline at end of file diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index 422c36fee..0093defa3 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/namespace_test/namespace_test2_generated.ts b/tests/namespace_test/namespace_test2_generated.ts index 18f1d6e87..61cf63123 100644 --- a/tests/namespace_test/namespace_test2_generated.ts +++ b/tests/namespace_test/namespace_test2_generated.ts @@ -1,6 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -import * as NS9459827973991502386 from "./namespace_test1_generated"; +import * as NS39599748 from "./namespace_test1_generated"; /** * @constructor */ @@ -39,24 +39,24 @@ static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):T * @param {NamespaceA.NamespaceB.TableInNestedNS=} obj * @returns {NamespaceA.NamespaceB.TableInNestedNS|null} */ -fooTable(obj?:NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS|null { +fooTable(obj?:NS39599748.NamespaceA.NamespaceB.TableInNestedNS):NS39599748.NamespaceA.NamespaceB.TableInNestedNS|null { var offset = this.bb!.__offset(this.bb_pos, 4); - return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null; + return offset ? (obj || new NS39599748.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null; }; /** * @returns {NamespaceA.NamespaceB.EnumInNestedNS} */ -fooEnum():NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS { +fooEnum():NS39599748.NamespaceA.NamespaceB.EnumInNestedNS { var offset = this.bb!.__offset(this.bb_pos, 6); - return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A; + return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS39599748.NamespaceA.NamespaceB.EnumInNestedNS.A; }; /** * @param {NamespaceA.NamespaceB.EnumInNestedNS} value * @returns {boolean} */ -mutate_foo_enum(value:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS):boolean { +mutate_foo_enum(value:NS39599748.NamespaceA.NamespaceB.EnumInNestedNS):boolean { var offset = this.bb!.__offset(this.bb_pos, 6); if (offset === 0) { @@ -71,9 +71,9 @@ mutate_foo_enum(value:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS * @param {NamespaceA.NamespaceB.StructInNestedNS=} obj * @returns {NamespaceA.NamespaceB.StructInNestedNS|null} */ -fooStruct(obj?:NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS|null { +fooStruct(obj?:NS39599748.NamespaceA.NamespaceB.StructInNestedNS):NS39599748.NamespaceA.NamespaceB.StructInNestedNS|null { var offset = this.bb!.__offset(this.bb_pos, 8); - return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null; + return offset ? (obj || new NS39599748.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null; }; /** @@ -95,8 +95,8 @@ static addFooTable(builder:flatbuffers.Builder, fooTableOffset:flatbuffers.Offse * @param {flatbuffers.Builder} builder * @param {NamespaceA.NamespaceB.EnumInNestedNS} fooEnum */ -static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS) { - builder.addFieldInt8(1, fooEnum, NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A); +static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS39599748.NamespaceA.NamespaceB.EnumInNestedNS) { + builder.addFieldInt8(1, fooEnum, NS39599748.NamespaceA.NamespaceB.EnumInNestedNS.A); }; /**