From 39d4b7e2bfb521850f28f4e42183a2f420aacdb6 Mon Sep 17 00:00:00 2001 From: Bob Potter Date: Fri, 8 Aug 2014 21:59:28 -0500 Subject: [PATCH] Fix vector of strings for Java Change-Id: If032b450230b15224b2661836c8a740398d207c5 --- java/flatbuffers/Table.java | 1 - src/idl_gen_java.cpp | 2 +- tests/JavaTest.java | 12 ++++++++++++ tests/MyGame/Example/Monster.java | 2 +- tests/monsterdata_test.bin | Bin 176 -> 224 bytes tests/monsterdata_test.golden | 4 ++++ 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/java/flatbuffers/Table.java b/java/flatbuffers/Table.java index f2233fa..bfab3e0 100755 --- a/java/flatbuffers/Table.java +++ b/java/flatbuffers/Table.java @@ -41,7 +41,6 @@ public class Table { // Create a java String from UTF-8 data stored inside the flatbuffer. protected String __string(int offset) { - offset += bb_pos; offset += bb.getInt(offset); return new String(bb.array(), offset + SIZEOF_INT, bb.getInt(offset), Charset.forName("UTF-8")); } diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 4779a1c..6e751eb 100755 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -247,7 +247,7 @@ static void GenStruct(StructDef &struct_def, } break; case BASE_TYPE_STRING: - code += offset_prefix + getter +"(o) : null"; + code += offset_prefix + getter +"(o + bb_pos) : null"; break; case BASE_TYPE_VECTOR: { auto vectortype = field.value.type.VectorType(); diff --git a/tests/JavaTest.java b/tests/JavaTest.java index dd6cbca..902362f 100755 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -52,6 +52,8 @@ class JavaTest { // We set up the same values as monsterdata.json: int str = fbb.createString("MyMonster"); + int test1 = fbb.createString("test1"); + int test2 = fbb.createString("test2"); Monster.startInventoryVector(fbb, 5); for (byte i = 4; i >=0; i--) fbb.addByte(i); @@ -66,6 +68,11 @@ class JavaTest { Test.createTest(fbb, (short)30, (byte)40); int test4 = fbb.endVector(); + Monster.startTestarrayofstringVector(fbb, 2); + fbb.addOffset(test2); + fbb.addOffset(test1); + int testArrayOfString = fbb.endVector(); + Monster.startMonster(fbb); Monster.addPos(fbb, Vec3.createVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, (byte)4, (short)5, (byte)6)); @@ -75,6 +82,7 @@ class JavaTest { Monster.addTestType(fbb, (byte)1); Monster.addTest(fbb, mon2); Monster.addTest4(fbb, test4); + Monster.addTestarrayofstring(fbb, testArrayOfString); int mon = Monster.endMonster(fbb); fbb.finish(mon); @@ -135,6 +143,10 @@ class JavaTest { Test test_1 = monster.test4(1); TestEq(monster.test4Length(), 2); TestEq(test_0.a() + test_0.b() + test_1.a() + test_1.b(), 100); + + TestEq(monster.testarrayofstringLength(), 2); + TestEq(monster.testarrayofstring(0),"test1"); + TestEq(monster.testarrayofstring(1),"test2"); } static void TestEq(T a, T b) { diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index a6c584a..a8ec2f9 100755 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -14,7 +14,7 @@ public class Monster extends Table { public Vec3 pos(Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__init(o + bb_pos, bb) : null; } public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; } public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; } - public String name() { int o = __offset(10); return o != 0 ? __string(o) : null; } + public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; } public byte inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; } public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; } public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; } diff --git a/tests/monsterdata_test.bin b/tests/monsterdata_test.bin index 631374560aaacf61b3a7b0f3b821236f09100627..952a96bef5f8c6a3e603f801aec9852d613f070f 100644 GIT binary patch delta 106 zcmdnM_<&JSg8>2*7+e@QfJ`<910ZPv#H2*7%Uh#fJ`<9kcbWtvoaVkm@qI*RMh7U0CF9G7|3N{nb;c)0IkOc ACIA2c diff --git a/tests/monsterdata_test.golden b/tests/monsterdata_test.golden index c905fe4..a738cae 100644 --- a/tests/monsterdata_test.golden +++ b/tests/monsterdata_test.golden @@ -32,5 +32,9 @@ a: 30, b: 40 } + ], + testarrayofstring: [ + "test1", + "test2" ] } -- 2.7.4