From df3ab0cc762fb6ef35901b2bacca7ffe4cd030c5 Mon Sep 17 00:00:00 2001 From: Jacob Whitaker Abrams Date: Sat, 24 Jan 2026 15:03:25 -0800 Subject: [PATCH] [Java] Improve java API - Makes Java API for vectors cleaner like Rust, Swift and Python - Don't silently drop arrays of incorrect fixed length, throw instead - Restore missing JavaTest.sh - Ensure all vector creation examples in tutorial mention reverse order - Fix Kotlin shell script to ensure bash is used --- docs/source/tutorial.md | 50 +++-- .../google/flatbuffers/FlatBufferBuilder.java | 110 +++++++++++ java/src/test/java/JavaTest.java | 87 +++++++++ src/idl_gen_java.cpp | 180 +++++++++++++++--- tests/JavaTest.sh | 23 +++ tests/KotlinTest.sh | 2 +- tests/MyGame/Example/AbilityT.java | 5 + tests/MyGame/Example/ArrayStructT.java | 39 +++- tests/MyGame/Example/ArrayTableT.java | 5 + tests/MyGame/Example/Monster.java | 21 +- tests/MyGame/Example/MonsterT.java | 62 ++++++ tests/MyGame/Example/NestedStructT.java | 37 +++- tests/MyGame/Example/ReferrableT.java | 4 + tests/MyGame/Example/StatT.java | 6 + .../Example/StructOfStructsOfStructsT.java | 4 + tests/MyGame/Example/StructOfStructsT.java | 6 + .../Example/TestSimpleTableWithEnumT.java | 4 + tests/MyGame/Example/TestT.java | 5 + tests/MyGame/Example/TypeAliases.java | 2 +- tests/MyGame/Example/TypeAliasesT.java | 15 ++ tests/MyGame/Example/Vec3T.java | 9 + tests/MyGame/MonsterExtra.java | 4 +- tests/MyGame/MonsterExtraT.java | 14 ++ tests/union_vector/AttackerT.java | 4 + tests/union_vector/BookReaderT.java | 4 + tests/union_vector/FallingTubT.java | 4 + tests/union_vector/HandFanT.java | 4 + tests/union_vector/MovieT.java | 6 + tests/union_vector/RapunzelT.java | 4 + 29 files changed, 655 insertions(+), 65 deletions(-) create mode 100755 tests/JavaTest.sh diff --git a/docs/source/tutorial.md b/docs/source/tutorial.md index 49665f8f50f..21320ee6fc9 100644 --- a/docs/source/tutorial.md +++ b/docs/source/tutorial.md @@ -1075,9 +1075,7 @@ The Builder provides multiple ways to create `vectors`. ```java // Place the two weapons into an array, and pass it to the // `createWeaponsVector()` method to create a FlatBuffer vector. - int[] weaps = new int[2]; - weaps[0] = sword; - weaps[1] = axe; + int[] weaps = { sword, axe }; // Pass the `weaps` array into the `createWeaponsVector()` method to create // a FlatBuffer vector. @@ -1211,14 +1209,15 @@ bit more directly. Offset> inventory = builder.EndVector(); // Start building a path vector of length 2. - Monster.StartPathVector(fbb, 2); + Monster.StartPathVector(builder, 2); - // Serialize the individual Vec3 structs - Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f); + // Serialize the individual Vec3 structs. + // Note that the intended order should be reversed if order is important. Vec3.CreateVec3(builder, 4.0f, 5.0f, 6.0f); + Vec3.CreateVec3(builder, 1.0f, 2.0f, 3.0f); // End the vector to get the offset - Offset> path = fbb.EndVector(); + Offset> path = builder.EndVector(); ``` === "Dart" @@ -1249,9 +1248,11 @@ bit more directly. } inv := builder.EndVector(10) + // Serialize the individual Vec3 structs. + // Note that the intended order should be reversed if order is important. sample.MonsterStartPathVector(builder, 2) - sample.CreateVec3(builder, 1.0, 2.0, 3.0) sample.CreateVec3(builder, 4.0, 5.0, 6.0) + sample.CreateVec3(builder, 1.0, 2.0, 3.0) path := builder.EndVector(2) ``` @@ -1263,10 +1264,12 @@ bit more directly. byte[] treasure = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int inv = Monster.createInventoryVector(builder, treasure); - Monster.startPathVector(fbb, 2); - Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f); - Vec3.createVec3(builder, 4.0f, 5.0f, 6.0f); - int path = fbb.endVector(); + Vec3T[] pathData = { + new Vec3T(1.0f, 2.0f, 3.0f), + new Vec3T(4.0f, 5.0f, 6.0f) + }; + + int path = Monster.createPathVector(builder, pathData); ``` === "JavaScript" @@ -1277,9 +1280,10 @@ bit more directly. var treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure); + // Note that the intended order should be reversed if order is important. MyGame.Sample.Monster.startPathVector(builder, 2); - MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0); MyGame.Sample.Vec3.createVec3(builder, 4.0, 5.0, 6.0); + MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0); var path = builder.endVector(); ``` @@ -1291,10 +1295,11 @@ bit more directly. val treasure = byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) val inv = Monster.createInventoryVector(builder, treasure) - Monster.startPathVector(fbb, 2) - Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f) + // Note that the intended order should be reversed if order is important. + Monster.startPathVector(builder, 2) Vec3.createVec3(builder, 4.0f, 5.0f, 6.0f) - val path = fbb.endVector() + Vec3.createVec3(builder, 1.0f, 2.0f, 3.0f) + val path = builder.endVector() ``` === "Lobster" @@ -1303,9 +1308,10 @@ bit more directly. // Inventory. let inv = builder.MyGame_Sample_MonsterCreateInventoryVector(map(10): _) + // Note that the intended order should be reversed if order is important. builder.MyGame_Sample_MonsterStartPathVector(2) - builder.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0) builder.MyGame_Sample_CreateVec3(4.0, 5.0, 6.0) + builder.MyGame_Sample_CreateVec3(1.0, 2.0, 3.0) let path = builder.EndVector(2) ``` @@ -1324,8 +1330,8 @@ bit more directly. -- Create a FlatBuffer vector and prepend the path locations. -- Note: Since we prepend the data, prepend them in reverse order. monster.StartPathVector(builder, 2) - vec3.CreateVec3(builder, 1.0, 2.0, 3.0) vec3.CreateVec3(builder, 4.0, 5.0, 6.0) + vec3.CreateVec3(builder, 1.0, 2.0, 3.0) local path = builder:EndVector(2) ``` @@ -1337,8 +1343,9 @@ bit more directly. $treasure = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); $inv = \MyGame\Sample\Monster::CreateInventoryVector($builder, $treasure); + // Note that the intended order should be reversed if order is important. \MyGame\Example\Monster::StartPathVector($builder, 2); - \MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0); + \MyGame\Sample\Vec3::CreateVec3($builder, 4.0, 5.0, 6.0); \MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0); $path = $builder->endVector(); ``` @@ -1376,7 +1383,7 @@ bit more directly. let inventory: [Byte] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] let inventoryOffset = builder.createVector(inventory) - let path = fbb.createVector(ofStructs: [ + let path = builder.createVector(ofStructs: [ Vec3(x: 1, y: 2, z: 3), Vec3(x: 4, y: 5, z: 6) ]) @@ -1390,9 +1397,10 @@ bit more directly. let treasure = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; let inv = MyGame.Sample.Monster.createInventoryVector(builder, treasure); + // Note that the intended order should be reversed if order is important. MyGame.Sample.Monster.startPathVector(builder, 2); - MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0); MyGame.Sample.Vec3.createVec3(builder, 4.0, 5.0, 6.0); + MyGame.Sample.Vec3.createVec3(builder, 1.0, 2.0, 3.0); let path = builder.endVector(); ``` diff --git a/java/src/main/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/src/main/java/com/google/flatbuffers/FlatBufferBuilder.java index bac946d1a51..f8c35d3c27c 100644 --- a/java/src/main/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/src/main/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -689,6 +689,116 @@ public int createByteVector(ByteBuffer byteBuffer) { return endVector(); } + /** + * Create a boolean array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createBooleanVector(boolean[] arr) { + int length = arr.length; + startVector(1, length, 1); + bb.position(space -= length); + for (int i = 0; i < length; i++) bb.put(arr[i] ? (byte) 1 : (byte) 0); + return endVector(); + } + + /** + * Create a short array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createShortVector(short[] arr) { + int length = arr.length * 2; + startVector(2, arr.length, 2); + bb.position(space -= length); + bb.asShortBuffer().put(arr); + return endVector(); + } + + /** + * Create an int array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createIntVector(int[] arr) { + int length = arr.length * 4; + startVector(4, arr.length, 4); + bb.position(space -= length); + bb.asIntBuffer().put(arr); + return endVector(); + } + + /** + * Create a long array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createLongVector(long[] arr) { + int length = arr.length * 8; + startVector(8, arr.length, 8); + bb.position(space -= length); + bb.asLongBuffer().put(arr); + return endVector(); + } + + /** + * Create a float array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createFloatVector(float[] arr) { + int length = arr.length * 4; + startVector(4, arr.length, 4); + bb.position(space -= length); + bb.asFloatBuffer().put(arr); + return endVector(); + } + + /** + * Create a double array in the buffer. + * + * @param arr A source array with data + * @return The offset in the buffer where the encoded array starts. + */ + public int createDoubleVector(double[] arr) { + int length = arr.length * 8; + startVector(8, arr.length, 8); + bb.position(space -= length); + bb.asDoubleBuffer().put(arr); + return endVector(); + } + + /** + * Create a struct array in the buffer. + * + * @param arr A source array with structs + * @param elementSize The size of each struct element + * @param alignment The alignment of the struct + * @return The offset in the buffer where the encoded array starts. + */ + public int createStructVector(Struct[] arr, int elementSize, int alignment) { + int length = arr.length * elementSize; + startVector(elementSize, arr.length, alignment); + bb.position(space -= length); + // Copy the structs + for (int i = 0; i < arr.length; i++) { + Struct s = arr[i]; + // We copy directly from the source buffer to the destination buffer. + ByteBuffer src = s.bb.duplicate(); + src.position(s.bb_pos); + src.limit(s.bb_pos + elementSize); + ByteBuffer dst = bb.duplicate(); + dst.position(space + i * elementSize); + dst.put(src); + } + return endVector(); + } + /// @cond FLATBUFFERS_INTERNAL /** Should not be accessing the final buffer before it is finished. */ public void finished() { diff --git a/java/src/test/java/JavaTest.java b/java/src/test/java/JavaTest.java index 4e12db82f98..92ca8654d16 100644 --- a/java/src/test/java/JavaTest.java +++ b/java/src/test/java/JavaTest.java @@ -79,6 +79,93 @@ public void testFlatBufferBuilder() { TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer()); } + @org.junit.Test + public void testVectorCreationHelpers() { + FlatBufferBuilder fbb = new FlatBufferBuilder(16); + boolean[] bools = { true, false, true }; + int boolVec = Monster.createTestarrayofboolsVector(fbb, bools); + + long[] longs = { 1L, 2L, Long.MAX_VALUE }; + int longVec = Monster.createVectorOfLongsVector(fbb, longs); + + double[] doubles = { 1.0, 2.0, 3.14 }; + int doubleVec = Monster.createVectorOfDoublesVector(fbb, doubles); + + int name = fbb.createString("TestVectors"); + + Monster.startMonster(fbb); + Monster.addName(fbb, name); + Monster.addTestarrayofbools(fbb, boolVec); + Monster.addVectorOfLongs(fbb, longVec); + Monster.addVectorOfDoubles(fbb, doubleVec); + int mon = Monster.endMonster(fbb); + Monster.finishMonsterBuffer(fbb, mon); + + Monster monster = Monster.getRootAsMonster(fbb.dataBuffer()); + + assertThat(monster.testarrayofboolsLength()).isEqualTo(3); + assertThat(monster.testarrayofbools(0)).isTrue(); + assertThat(monster.testarrayofbools(1)).isFalse(); + assertThat(monster.testarrayofbools(2)).isTrue(); + + assertThat(monster.vectorOfLongsLength()).isEqualTo(3); + assertThat(monster.vectorOfLongs(2)).isEqualTo(Long.MAX_VALUE); + + assertThat(monster.vectorOfDoublesLength()).isEqualTo(3); + assertThat(monster.vectorOfDoubles(2)).isEqualTo(3.14); + } + + @org.junit.Test + public void testObjectVectorCreationHelpers() { + FlatBufferBuilder fbb = new FlatBufferBuilder(16); + AbilityT[] abilities = { + new AbilityT(10, 20), + new AbilityT(30, 40) + }; + + int abilityVec = Monster.createTestarrayofsortedstructVector(fbb, abilities); + int name = fbb.createString("TestObjectVectors"); + + Monster.startMonster(fbb); + Monster.addName(fbb, name); + Monster.addTestarrayofsortedstruct(fbb, abilityVec); + int mon = Monster.endMonster(fbb); + Monster.finishMonsterBuffer(fbb, mon); + + Monster monster = Monster.getRootAsMonster(fbb.dataBuffer()); + assertThat(monster.testarrayofsortedstructLength()).isEqualTo(2); + assertThat(monster.testarrayofsortedstruct(0).id()).isEqualTo(10L); + assertThat(monster.testarrayofsortedstruct(0).distance()).isEqualTo(20L); + assertThat(monster.testarrayofsortedstruct(1).id()).isEqualTo(30L); + assertThat(monster.testarrayofsortedstruct(1).distance()).isEqualTo(40L); + } + + @org.junit.Test + public void testFixedSizeArrayValidation() { + ArrayStructT s = new ArrayStructT(); + // Valid length should work + int[] validB = new int[15]; + s.setB(validB); + assertThat(s.getB()).isSameInstanceAs(validB); + + // Invalid length should throw + int[] invalidB = new int[14]; + try { + s.setB(invalidB); + org.junit.Assert.fail("Expected IllegalArgumentException was not thrown"); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isEqualTo("FlatBuffers: fixed-size array \"b\" must have length 15."); + } + + // Constructor should also validate + try { + new ArrayStructT(0.0f, invalidB, (byte)0, null, 0, null); + org.junit.Assert.fail("Expected IllegalArgumentException was not thrown in constructor"); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()).isEqualTo("FlatBuffers: fixed-size array \"b\" must have length 15."); + } + } + @org.junit.Test public void TestEnums() { assertThat(Color.name(Color.Red)).isEqualTo("Red"); diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 130751a749f..cbb50d1092f 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -1193,27 +1193,76 @@ class JavaGenerator : public BaseGenerator { auto vector_type = field.value.type.VectorType(); auto alignment = InlineAlignment(vector_type); auto elem_size = InlineSize(vector_type); - if (!IsStruct(vector_type)) { - field_has_create_set.insert(&field); - // generate a method to create a vector from a java array. - if ((vector_type.base_type == BASE_TYPE_CHAR || - vector_type.base_type == BASE_TYPE_UCHAR)) { - // Handle byte[] and ByteBuffers separately for Java - code += " public static " + GenVectorOffsetType() + " "; - code += namer_.Method("create", field); - code += "Vector(FlatBufferBuilder builder, byte[] data) "; - code += "{ return builder.createByteVector(data); }\n"; - - code += " public static " + GenVectorOffsetType() + " "; - code += namer_.Method("create", field); - code += "Vector(FlatBufferBuilder builder, ByteBuffer data) "; - code += "{ return builder.createByteVector(data); }\n"; + field_has_create_set.insert(&field); + + // generate a method to create a vector from a java array. + if ((vector_type.base_type == BASE_TYPE_CHAR || + vector_type.base_type == BASE_TYPE_UCHAR)) { + // Handle byte[] and ByteBuffers separately for Java + code += " public static " + GenVectorOffsetType() + " "; + code += namer_.Method("create", field); + code += "Vector(FlatBufferBuilder builder, byte[] data) "; + code += "{ return builder.createByteVector(data); }\n"; + + code += " public static " + GenVectorOffsetType() + " "; + code += namer_.Method("create", field); + code += "Vector(FlatBufferBuilder builder, ByteBuffer data) "; + code += "{ return builder.createByteVector(data); }\n"; + } else { + bool is_struct_vector = vector_type.base_type == BASE_TYPE_STRUCT && + vector_type.struct_def->fixed; + code += " public static " + GenVectorOffsetType() + " "; + code += namer_.Method("create", field); + code += "Vector(FlatBufferBuilder builder, "; + std::string type_name; + if (is_struct_vector) { + type_name = GenTypeGet(vector_type); + } else if (IsScalar(vector_type.base_type)) { + type_name = GenTypeBasic(vector_type); + } else { + type_name = "int"; + } + code += type_name + "[] data) "; + + std::string helper_method = ""; + switch (vector_type.base_type) { + case BASE_TYPE_BOOL: + helper_method = "createBooleanVector"; + break; + case BASE_TYPE_SHORT: + helper_method = "createShortVector"; + break; + case BASE_TYPE_INT: + helper_method = "createIntVector"; + break; + case BASE_TYPE_LONG: + case BASE_TYPE_ULONG: + helper_method = "createLongVector"; + break; + case BASE_TYPE_FLOAT: + helper_method = "createFloatVector"; + break; + case BASE_TYPE_DOUBLE: + helper_method = "createDoubleVector"; + break; + case BASE_TYPE_STRUCT: + if (is_struct_vector) { + helper_method = "createStructVector"; + } + break; + default: + break; + } + + if (!helper_method.empty()) { + if (is_struct_vector) { + code += "{ return builder." + helper_method + "(data, " + + NumToString(elem_size) + ", " + NumToString(alignment) + + "); }\n"; + } else { + code += "{ return builder." + helper_method + "(data); }\n"; + } } else { - code += " public static " + GenVectorOffsetType() + " "; - code += namer_.Method("create", field); - code += "Vector(FlatBufferBuilder builder, "; - code += GenTypeBasic(DestinationType(vector_type, false)) + - "[] data) "; code += "{ builder.startVector("; code += NumToString(elem_size); code += ", data.length, "; @@ -1229,6 +1278,23 @@ class JavaGenerator : public BaseGenerator { code += "builder.endVector(); }\n"; } } + + // Generate overload for Object API (POJO array) if enabled + if (parser_.opts.generate_object_based_api && + vector_type.base_type == BASE_TYPE_STRUCT && + vector_type.struct_def->fixed) { + code += " public static " + GenVectorOffsetType() + " "; + code += namer_.Method("create", field); + code += "Vector(FlatBufferBuilder builder, "; + code += GenTypeGet_ObjectAPI(vector_type, true, true) + "[] data) "; + code += "{ builder.startVector(" + NumToString(elem_size) + + ", data.length, " + NumToString(alignment) + "); "; + code += "for (int i = data.length - 1; i >= 0; i--) { "; + // Use pack to serialize the POJO struct + code += GenTypeGet(vector_type) + ".pack(builder, data[i]); } "; + code += "return builder.endVector(); }\n\n"; + } + // Generate a method to start a vector, data to be added manually // after. code += " public static void " + namer_.Method("start", field); @@ -1653,7 +1719,9 @@ class JavaGenerator : public BaseGenerator { break; } case BASE_TYPE_VECTOR: { - if (field_has_create.find(&field) != field_has_create.end()) { + if (field_has_create.find(&field) != field_has_create.end() && + !(field.value.type.struct_def && + field.value.type.struct_def->fixed)) { auto property_name = field_name; auto gen_for_loop = true; std::string array_name = "__" + field_name; @@ -2157,15 +2225,26 @@ class JavaGenerator : public BaseGenerator { code += " public " + type_name + " " + get_field + "() { return " + field_name + "; }\n\n"; - std::string array_validation = ""; + if (field.value.type.base_type == BASE_TYPE_ARRAY) { - array_validation = - "if (" + field_name + " != null && " + field_name + - ".length == " + NumToString(field.value.type.fixed_length) + ") "; + code += " public void " + namer_.Method("set", field) + "(" + + type_name + " " + field_name + ") {\n"; + code += " if (" + field_name + " != null && " + field_name + + ".length != " + NumToString(field.value.type.fixed_length) + + ") {\n"; + code += + " throw new IllegalArgumentException(\"FlatBuffers: " + "fixed-size array \\\"" + + field_name + "\\\" must have length " + + NumToString(field.value.type.fixed_length) + ".\");\n"; + code += " }\n"; + code += " this." + field_name + " = " + field_name + ";\n"; + code += " }\n\n"; + } else { + code += " public void " + namer_.Method("set", field) + "(" + + type_name + " " + field_name + ") { this." + field_name + + " = " + field_name + "; }\n\n"; } - code += " public void " + namer_.Method("set", field) + "(" + type_name + - " " + field_name + ") { " + array_validation + "this." + - field_name + " = " + field_name + "; }\n\n"; } // Generate Constructor code += "\n"; @@ -2210,8 +2289,53 @@ class JavaGenerator : public BaseGenerator { } } code += " }\n"; + + // Generate parameterized constructor + if (!struct_def.fields.vec.empty()) { + code += "\n"; + code += " public " + class_name + "("; + bool first = true; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + const auto& field = **it; + if (field.deprecated) continue; + if (field.value.type.base_type == BASE_TYPE_UTYPE) continue; + if (field.value.type.element == BASE_TYPE_UTYPE) continue; + if (!first) code += ", "; + first = false; + auto type_name = GenTypeGet_ObjectAPI(field.value.type, false, true); + if (field.IsScalarOptional()) + type_name = ConvertPrimitiveTypeToObjectWrapper_ObjectAPI(type_name); + code += type_name + " " + namer_.Field(field); + } + code += ") {\n"; + + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + const auto& field = **it; + if (field.deprecated) continue; + if (field.value.type.base_type == BASE_TYPE_UTYPE) continue; + if (field.value.type.element == BASE_TYPE_UTYPE) continue; + const auto field_name = namer_.Field(field); + if (field.value.type.base_type == BASE_TYPE_ARRAY) { + code += " if (" + field_name + " != null && " + field_name + + ".length != " + NumToString(field.value.type.fixed_length) + + ") {\n"; + code += + " throw new IllegalArgumentException(\"FlatBuffers: " + "fixed-size array \\\"" + + field_name + "\\\" must have length " + + NumToString(field.value.type.fixed_length) + ".\");\n"; + code += " }\n"; + } + code += " this." + field_name + " = " + field_name + ";\n"; + } + code += " }\n"; + } + if (parser_.root_struct_def_ == &struct_def) { const std::string struct_type = namer_.Type(struct_def); + code += "\n"; code += " public static " + class_name + " deserializeFromBinary(byte[] fbBuffer) {\n"; code += " return " + struct_type + "." + diff --git a/tests/JavaTest.sh b/tests/JavaTest.sh new file mode 100755 index 00000000000..f6d55521d3b --- /dev/null +++ b/tests/JavaTest.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# +# Copyright 2014 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit + +echo Running Java tests. + +testdir=$(dirname "$0") +pom_path=$(readlink -f "${testdir}/../java/pom.xml") +mvn -f "$pom_path" test diff --git a/tests/KotlinTest.sh b/tests/KotlinTest.sh index 2b41d5cb1bb..c148647faaf 100755 --- a/tests/KotlinTest.sh +++ b/tests/KotlinTest.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Copyright 2014 Google Inc. All rights reserved. # diff --git a/tests/MyGame/Example/AbilityT.java b/tests/MyGame/Example/AbilityT.java index 1da0dde516d..056e4d781f9 100644 --- a/tests/MyGame/Example/AbilityT.java +++ b/tests/MyGame/Example/AbilityT.java @@ -35,5 +35,10 @@ public AbilityT() { this.id = 0L; this.distance = 0L; } + + public AbilityT(long id, long distance) { + this.id = id; + this.distance = distance; + } } diff --git a/tests/MyGame/Example/ArrayStructT.java b/tests/MyGame/Example/ArrayStructT.java index 96d3ebf92a8..70f0b735b43 100644 --- a/tests/MyGame/Example/ArrayStructT.java +++ b/tests/MyGame/Example/ArrayStructT.java @@ -32,7 +32,12 @@ public class ArrayStructT { public int[] getB() { return b; } - public void setB(int[] b) { if (b != null && b.length == 15) this.b = b; } + public void setB(int[] b) { + if (b != null && b.length != 15) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"b\" must have length 15."); + } + this.b = b; + } public byte getC() { return c; } @@ -40,7 +45,12 @@ public class ArrayStructT { public MyGame.Example.NestedStructT[] getD() { return d; } - public void setD(MyGame.Example.NestedStructT[] d) { if (d != null && d.length == 2) this.d = d; } + public void setD(MyGame.Example.NestedStructT[] d) { + if (d != null && d.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"d\" must have length 2."); + } + this.d = d; + } public int getE() { return e; } @@ -48,7 +58,12 @@ public class ArrayStructT { public long[] getF() { return f; } - public void setF(long[] f) { if (f != null && f.length == 2) this.f = f; } + public void setF(long[] f) { + if (f != null && f.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"f\" must have length 2."); + } + this.f = f; + } public ArrayStructT() { @@ -59,5 +74,23 @@ public ArrayStructT() { this.e = 0; this.f = new long[2]; } + + public ArrayStructT(float a, int[] b, byte c, MyGame.Example.NestedStructT[] d, int e, long[] f) { + this.a = a; + if (b != null && b.length != 15) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"b\" must have length 15."); + } + this.b = b; + this.c = c; + if (d != null && d.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"d\" must have length 2."); + } + this.d = d; + this.e = e; + if (f != null && f.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"f\" must have length 2."); + } + this.f = f; + } } diff --git a/tests/MyGame/Example/ArrayTableT.java b/tests/MyGame/Example/ArrayTableT.java index aa6b32c642e..ef1345e3b2e 100644 --- a/tests/MyGame/Example/ArrayTableT.java +++ b/tests/MyGame/Example/ArrayTableT.java @@ -29,6 +29,11 @@ public class ArrayTableT { public ArrayTableT() { this.a = new MyGame.Example.ArrayStructT(); } + + public ArrayTableT(MyGame.Example.ArrayStructT a) { + this.a = a; + } + public static ArrayTableT deserializeFromBinary(byte[] fbBuffer) { return ArrayTable.getRootAsArrayTable(ByteBuffer.wrap(fbBuffer)).unpack(); } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 79e7815ab83..1f7f16fe3f8 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -257,6 +257,9 @@ public final class Monster extends com.google.flatbuffers.Table { public static void addTestType(FlatBufferBuilder builder, byte testType) { builder.addByte(7, testType, 0); } public static void addTest(FlatBufferBuilder builder, int testOffset) { builder.addOffset(8, testOffset, 0); } public static void addTest4(FlatBufferBuilder builder, int test4Offset) { builder.addOffset(9, test4Offset, 0); } + public static int createTest4Vector(FlatBufferBuilder builder, MyGame.Example.Test[] data) { return builder.createStructVector(data, 4, 2); } + public static int createTest4Vector(FlatBufferBuilder builder, MyGame.Example.TestT[] data) { builder.startVector(4, data.length, 2); for (int i = data.length - 1; i >= 0; i--) { MyGame.Example.Test.pack(builder, data[i]); } return builder.endVector(); } + public static void startTest4Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 2); } public static void addTestarrayofstring(FlatBufferBuilder builder, int testarrayofstringOffset) { builder.addOffset(10, testarrayofstringOffset, 0); } public static int createTestarrayofstringVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } @@ -280,7 +283,7 @@ public final class Monster extends com.google.flatbuffers.Table { public static void addTesthashs64Fnv1a(FlatBufferBuilder builder, long testhashs64Fnv1a) { builder.addLong(22, testhashs64Fnv1a, 0L); } public static void addTesthashu64Fnv1a(FlatBufferBuilder builder, long testhashu64Fnv1a) { builder.addLong(23, testhashu64Fnv1a, 0L); } public static void addTestarrayofbools(FlatBufferBuilder builder, int testarrayofboolsOffset) { builder.addOffset(24, testarrayofboolsOffset, 0); } - public static int createTestarrayofboolsVector(FlatBufferBuilder builder, boolean[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addBoolean(data[i]); return builder.endVector(); } + public static int createTestarrayofboolsVector(FlatBufferBuilder builder, boolean[] data) { return builder.createBooleanVector(data); } public static void startTestarrayofboolsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addTestf(FlatBufferBuilder builder, float testf) { builder.addFloat(25, testf, 3.14159f); } public static void addTestf2(FlatBufferBuilder builder, float testf2) { builder.addFloat(26, testf2, 3.0f); } @@ -289,18 +292,24 @@ public final class Monster extends com.google.flatbuffers.Table { public static int createTestarrayofstring2Vector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } public static void startTestarrayofstring2Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static void addTestarrayofsortedstruct(FlatBufferBuilder builder, int testarrayofsortedstructOffset) { builder.addOffset(29, testarrayofsortedstructOffset, 0); } + public static int createTestarrayofsortedstructVector(FlatBufferBuilder builder, MyGame.Example.Ability[] data) { return builder.createStructVector(data, 8, 4); } + public static int createTestarrayofsortedstructVector(FlatBufferBuilder builder, MyGame.Example.AbilityT[] data) { builder.startVector(8, data.length, 4); for (int i = data.length - 1; i >= 0; i--) { MyGame.Example.Ability.pack(builder, data[i]); } return builder.endVector(); } + public static void startTestarrayofsortedstructVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 4); } public static void addFlex(FlatBufferBuilder builder, int flexOffset) { builder.addOffset(30, flexOffset, 0); } public static int createFlexVector(FlatBufferBuilder builder, byte[] data) { return builder.createByteVector(data); } public static int createFlexVector(FlatBufferBuilder builder, ByteBuffer data) { return builder.createByteVector(data); } public static void startFlexVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addTest5(FlatBufferBuilder builder, int test5Offset) { builder.addOffset(31, test5Offset, 0); } + public static int createTest5Vector(FlatBufferBuilder builder, MyGame.Example.Test[] data) { return builder.createStructVector(data, 4, 2); } + public static int createTest5Vector(FlatBufferBuilder builder, MyGame.Example.TestT[] data) { builder.startVector(4, data.length, 2); for (int i = data.length - 1; i >= 0; i--) { MyGame.Example.Test.pack(builder, data[i]); } return builder.endVector(); } + public static void startTest5Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 2); } public static void addVectorOfLongs(FlatBufferBuilder builder, int vectorOfLongsOffset) { builder.addOffset(32, vectorOfLongsOffset, 0); } - public static int createVectorOfLongsVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); } + public static int createVectorOfLongsVector(FlatBufferBuilder builder, long[] data) { return builder.createLongVector(data); } public static void startVectorOfLongsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addVectorOfDoubles(FlatBufferBuilder builder, int vectorOfDoublesOffset) { builder.addOffset(33, vectorOfDoublesOffset, 0); } - public static int createVectorOfDoublesVector(FlatBufferBuilder builder, double[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addDouble(data[i]); return builder.endVector(); } + public static int createVectorOfDoublesVector(FlatBufferBuilder builder, double[] data) { return builder.createDoubleVector(data); } public static void startVectorOfDoublesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addParentNamespaceTest(FlatBufferBuilder builder, int parentNamespaceTestOffset) { builder.addOffset(34, parentNamespaceTestOffset, 0); } public static void addVectorOfReferrables(FlatBufferBuilder builder, int vectorOfReferrablesOffset) { builder.addOffset(35, vectorOfReferrablesOffset, 0); } @@ -308,18 +317,18 @@ public final class Monster extends com.google.flatbuffers.Table { public static void startVectorOfReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static void addSingleWeakReference(FlatBufferBuilder builder, long singleWeakReference) { builder.addLong(36, singleWeakReference, 0L); } public static void addVectorOfWeakReferences(FlatBufferBuilder builder, int vectorOfWeakReferencesOffset) { builder.addOffset(37, vectorOfWeakReferencesOffset, 0); } - public static int createVectorOfWeakReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); } + public static int createVectorOfWeakReferencesVector(FlatBufferBuilder builder, long[] data) { return builder.createLongVector(data); } public static void startVectorOfWeakReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addVectorOfStrongReferrables(FlatBufferBuilder builder, int vectorOfStrongReferrablesOffset) { builder.addOffset(38, vectorOfStrongReferrablesOffset, 0); } public static int createVectorOfStrongReferrablesVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } public static void startVectorOfStrongReferrablesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static void addCoOwningReference(FlatBufferBuilder builder, long coOwningReference) { builder.addLong(39, coOwningReference, 0L); } public static void addVectorOfCoOwningReferences(FlatBufferBuilder builder, int vectorOfCoOwningReferencesOffset) { builder.addOffset(40, vectorOfCoOwningReferencesOffset, 0); } - public static int createVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); } + public static int createVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, long[] data) { return builder.createLongVector(data); } public static void startVectorOfCoOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addNonOwningReference(FlatBufferBuilder builder, long nonOwningReference) { builder.addLong(41, nonOwningReference, 0L); } public static void addVectorOfNonOwningReferences(FlatBufferBuilder builder, int vectorOfNonOwningReferencesOffset) { builder.addOffset(42, vectorOfNonOwningReferencesOffset, 0); } - public static int createVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, long[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addLong(data[i]); return builder.endVector(); } + public static int createVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, long[] data) { return builder.createLongVector(data); } public static void startVectorOfNonOwningReferencesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addAnyUniqueType(FlatBufferBuilder builder, byte anyUniqueType) { builder.addByte(43, anyUniqueType, 0); } public static void addAnyUnique(FlatBufferBuilder builder, int anyUniqueOffset) { builder.addOffset(44, anyUniqueOffset, 0); } diff --git a/tests/MyGame/Example/MonsterT.java b/tests/MyGame/Example/MonsterT.java index 29e2e982b06..e0a8d7dc914 100644 --- a/tests/MyGame/Example/MonsterT.java +++ b/tests/MyGame/Example/MonsterT.java @@ -371,6 +371,68 @@ public MonsterT() { this.negativeInfinityDefault = Float.NEGATIVE_INFINITY; this.doubleInfDefault = Double.POSITIVE_INFINITY; } + + public MonsterT(MyGame.Example.Vec3T pos, short mana, short hp, String name, int[] inventory, int color, MyGame.Example.AnyUnion test, MyGame.Example.TestT[] test4, String[] testarrayofstring, MyGame.Example.MonsterT[] testarrayoftables, MyGame.Example.MonsterT enemy, int[] testnestedflatbuffer, MyGame.Example.StatT testempty, boolean testbool, int testhashs32Fnv1, long testhashu32Fnv1, long testhashs64Fnv1, long testhashu64Fnv1, int testhashs32Fnv1a, long testhashu32Fnv1a, long testhashs64Fnv1a, long testhashu64Fnv1a, boolean[] testarrayofbools, float testf, float testf2, float testf3, String[] testarrayofstring2, MyGame.Example.AbilityT[] testarrayofsortedstruct, int[] flex, MyGame.Example.TestT[] test5, long[] vectorOfLongs, double[] vectorOfDoubles, MyGame.InParentNamespaceT parentNamespaceTest, MyGame.Example.ReferrableT[] vectorOfReferrables, long singleWeakReference, long[] vectorOfWeakReferences, MyGame.Example.ReferrableT[] vectorOfStrongReferrables, long coOwningReference, long[] vectorOfCoOwningReferences, long nonOwningReference, long[] vectorOfNonOwningReferences, MyGame.Example.AnyUniqueAliasesUnion anyUnique, MyGame.Example.AnyAmbiguousAliasesUnion anyAmbiguous, int[] vectorOfEnums, byte signedEnum, int[] testrequirednestedflatbuffer, MyGame.Example.StatT[] scalarKeySortedTables, MyGame.Example.TestT nativeInline, long longEnumNonEnumDefault, long longEnumNormalDefault, float nanDefault, float infDefault, float positiveInfDefault, float infinityDefault, float positiveInfinityDefault, float negativeInfDefault, float negativeInfinityDefault, double doubleInfDefault) { + this.pos = pos; + this.mana = mana; + this.hp = hp; + this.name = name; + this.inventory = inventory; + this.color = color; + this.test = test; + this.test4 = test4; + this.testarrayofstring = testarrayofstring; + this.testarrayoftables = testarrayoftables; + this.enemy = enemy; + this.testnestedflatbuffer = testnestedflatbuffer; + this.testempty = testempty; + this.testbool = testbool; + this.testhashs32Fnv1 = testhashs32Fnv1; + this.testhashu32Fnv1 = testhashu32Fnv1; + this.testhashs64Fnv1 = testhashs64Fnv1; + this.testhashu64Fnv1 = testhashu64Fnv1; + this.testhashs32Fnv1a = testhashs32Fnv1a; + this.testhashu32Fnv1a = testhashu32Fnv1a; + this.testhashs64Fnv1a = testhashs64Fnv1a; + this.testhashu64Fnv1a = testhashu64Fnv1a; + this.testarrayofbools = testarrayofbools; + this.testf = testf; + this.testf2 = testf2; + this.testf3 = testf3; + this.testarrayofstring2 = testarrayofstring2; + this.testarrayofsortedstruct = testarrayofsortedstruct; + this.flex = flex; + this.test5 = test5; + this.vectorOfLongs = vectorOfLongs; + this.vectorOfDoubles = vectorOfDoubles; + this.parentNamespaceTest = parentNamespaceTest; + this.vectorOfReferrables = vectorOfReferrables; + this.singleWeakReference = singleWeakReference; + this.vectorOfWeakReferences = vectorOfWeakReferences; + this.vectorOfStrongReferrables = vectorOfStrongReferrables; + this.coOwningReference = coOwningReference; + this.vectorOfCoOwningReferences = vectorOfCoOwningReferences; + this.nonOwningReference = nonOwningReference; + this.vectorOfNonOwningReferences = vectorOfNonOwningReferences; + this.anyUnique = anyUnique; + this.anyAmbiguous = anyAmbiguous; + this.vectorOfEnums = vectorOfEnums; + this.signedEnum = signedEnum; + this.testrequirednestedflatbuffer = testrequirednestedflatbuffer; + this.scalarKeySortedTables = scalarKeySortedTables; + this.nativeInline = nativeInline; + this.longEnumNonEnumDefault = longEnumNonEnumDefault; + this.longEnumNormalDefault = longEnumNormalDefault; + this.nanDefault = nanDefault; + this.infDefault = infDefault; + this.positiveInfDefault = positiveInfDefault; + this.infinityDefault = infinityDefault; + this.positiveInfinityDefault = positiveInfinityDefault; + this.negativeInfDefault = negativeInfDefault; + this.negativeInfinityDefault = negativeInfinityDefault; + this.doubleInfDefault = doubleInfDefault; + } + public static MonsterT deserializeFromBinary(byte[] fbBuffer) { return Monster.getRootAsMonster(ByteBuffer.wrap(fbBuffer)).unpack(); } diff --git a/tests/MyGame/Example/NestedStructT.java b/tests/MyGame/Example/NestedStructT.java index 9594675decc..bc8ba520bcb 100644 --- a/tests/MyGame/Example/NestedStructT.java +++ b/tests/MyGame/Example/NestedStructT.java @@ -26,7 +26,12 @@ public class NestedStructT { public int[] getA() { return a; } - public void setA(int[] a) { if (a != null && a.length == 2) this.a = a; } + public void setA(int[] a) { + if (a != null && a.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"a\" must have length 2."); + } + this.a = a; + } public byte getB() { return b; } @@ -34,11 +39,21 @@ public class NestedStructT { public byte[] getC() { return c; } - public void setC(byte[] c) { if (c != null && c.length == 2) this.c = c; } + public void setC(byte[] c) { + if (c != null && c.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"c\" must have length 2."); + } + this.c = c; + } public long[] getD() { return d; } - public void setD(long[] d) { if (d != null && d.length == 2) this.d = d; } + public void setD(long[] d) { + if (d != null && d.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"d\" must have length 2."); + } + this.d = d; + } public NestedStructT() { @@ -47,5 +62,21 @@ public NestedStructT() { this.c = new byte[2]; this.d = new long[2]; } + + public NestedStructT(int[] a, byte b, byte[] c, long[] d) { + if (a != null && a.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"a\" must have length 2."); + } + this.a = a; + this.b = b; + if (c != null && c.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"c\" must have length 2."); + } + this.c = c; + if (d != null && d.length != 2) { + throw new IllegalArgumentException("FlatBuffers: fixed-size array \"d\" must have length 2."); + } + this.d = d; + } } diff --git a/tests/MyGame/Example/ReferrableT.java b/tests/MyGame/Example/ReferrableT.java index 73757c5b6da..048ae7981d0 100644 --- a/tests/MyGame/Example/ReferrableT.java +++ b/tests/MyGame/Example/ReferrableT.java @@ -29,5 +29,9 @@ public class ReferrableT { public ReferrableT() { this.id = 0L; } + + public ReferrableT(long id) { + this.id = id; + } } diff --git a/tests/MyGame/Example/StatT.java b/tests/MyGame/Example/StatT.java index 6696b3080cf..db275ef8b39 100644 --- a/tests/MyGame/Example/StatT.java +++ b/tests/MyGame/Example/StatT.java @@ -41,5 +41,11 @@ public StatT() { this.val = 0L; this.count = 0; } + + public StatT(String id, long val, int count) { + this.id = id; + this.val = val; + this.count = count; + } } diff --git a/tests/MyGame/Example/StructOfStructsOfStructsT.java b/tests/MyGame/Example/StructOfStructsOfStructsT.java index cf4428ae722..39d23c2f2fb 100644 --- a/tests/MyGame/Example/StructOfStructsOfStructsT.java +++ b/tests/MyGame/Example/StructOfStructsOfStructsT.java @@ -29,5 +29,9 @@ public class StructOfStructsOfStructsT { public StructOfStructsOfStructsT() { this.a = new MyGame.Example.StructOfStructsT(); } + + public StructOfStructsOfStructsT(MyGame.Example.StructOfStructsT a) { + this.a = a; + } } diff --git a/tests/MyGame/Example/StructOfStructsT.java b/tests/MyGame/Example/StructOfStructsT.java index eec5ef30e15..4230233536f 100644 --- a/tests/MyGame/Example/StructOfStructsT.java +++ b/tests/MyGame/Example/StructOfStructsT.java @@ -41,5 +41,11 @@ public StructOfStructsT() { this.b = new MyGame.Example.TestT(); this.c = new MyGame.Example.AbilityT(); } + + public StructOfStructsT(MyGame.Example.AbilityT a, MyGame.Example.TestT b, MyGame.Example.AbilityT c) { + this.a = a; + this.b = b; + this.c = c; + } } diff --git a/tests/MyGame/Example/TestSimpleTableWithEnumT.java b/tests/MyGame/Example/TestSimpleTableWithEnumT.java index ab4722654da..a3a25145c3e 100644 --- a/tests/MyGame/Example/TestSimpleTableWithEnumT.java +++ b/tests/MyGame/Example/TestSimpleTableWithEnumT.java @@ -29,5 +29,9 @@ class TestSimpleTableWithEnumT { public TestSimpleTableWithEnumT() { this.color = 2; } + + public TestSimpleTableWithEnumT(int color) { + this.color = color; + } } diff --git a/tests/MyGame/Example/TestT.java b/tests/MyGame/Example/TestT.java index 19f19d74eed..5e4985b3460 100644 --- a/tests/MyGame/Example/TestT.java +++ b/tests/MyGame/Example/TestT.java @@ -35,5 +35,10 @@ public TestT() { this.a = 0; this.b = 0; } + + public TestT(short a, byte b) { + this.a = a; + this.b = b; + } } diff --git a/tests/MyGame/Example/TypeAliases.java b/tests/MyGame/Example/TypeAliases.java index e01721f06bb..f4a65426ee5 100644 --- a/tests/MyGame/Example/TypeAliases.java +++ b/tests/MyGame/Example/TypeAliases.java @@ -106,7 +106,7 @@ public static int createTypeAliases(FlatBufferBuilder builder, public static int createV8Vector(FlatBufferBuilder builder, ByteBuffer data) { return builder.createByteVector(data); } public static void startV8Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addVf64(FlatBufferBuilder builder, int vf64Offset) { builder.addOffset(11, vf64Offset, 0); } - public static int createVf64Vector(FlatBufferBuilder builder, double[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addDouble(data[i]); return builder.endVector(); } + public static int createVf64Vector(FlatBufferBuilder builder, double[] data) { return builder.createDoubleVector(data); } public static void startVf64Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static int endTypeAliases(FlatBufferBuilder builder) { int o = builder.endTable(); diff --git a/tests/MyGame/Example/TypeAliasesT.java b/tests/MyGame/Example/TypeAliasesT.java index 90038ae67ed..10e34368ac3 100644 --- a/tests/MyGame/Example/TypeAliasesT.java +++ b/tests/MyGame/Example/TypeAliasesT.java @@ -95,5 +95,20 @@ public TypeAliasesT() { this.v8 = null; this.vf64 = null; } + + public TypeAliasesT(byte i8, int u8, short i16, int u16, int i32, long u32, long i64, long u64, float f32, double f64, byte[] v8, double[] vf64) { + this.i8 = i8; + this.u8 = u8; + this.i16 = i16; + this.u16 = u16; + this.i32 = i32; + this.u32 = u32; + this.i64 = i64; + this.u64 = u64; + this.f32 = f32; + this.f64 = f64; + this.v8 = v8; + this.vf64 = vf64; + } } diff --git a/tests/MyGame/Example/Vec3T.java b/tests/MyGame/Example/Vec3T.java index 41bff348b37..66133cfd2a6 100644 --- a/tests/MyGame/Example/Vec3T.java +++ b/tests/MyGame/Example/Vec3T.java @@ -59,5 +59,14 @@ public Vec3T() { this.test2 = 0; this.test3 = new MyGame.Example.TestT(); } + + public Vec3T(float x, float y, float z, double test1, int test2, MyGame.Example.TestT test3) { + this.x = x; + this.y = y; + this.z = z; + this.test1 = test1; + this.test2 = test2; + this.test3 = test3; + } } diff --git a/tests/MyGame/MonsterExtra.java b/tests/MyGame/MonsterExtra.java index 373989b1694..2384ef518fb 100644 --- a/tests/MyGame/MonsterExtra.java +++ b/tests/MyGame/MonsterExtra.java @@ -93,10 +93,10 @@ public static int createMonsterExtra(FlatBufferBuilder builder, public static void addF2(FlatBufferBuilder builder, float f2) { builder.addFloat(6, f2, Float.POSITIVE_INFINITY); } public static void addF3(FlatBufferBuilder builder, float f3) { builder.addFloat(7, f3, Float.NEGATIVE_INFINITY); } public static void addDvec(FlatBufferBuilder builder, int dvecOffset) { builder.addOffset(8, dvecOffset, 0); } - public static int createDvecVector(FlatBufferBuilder builder, double[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addDouble(data[i]); return builder.endVector(); } + public static int createDvecVector(FlatBufferBuilder builder, double[] data) { return builder.createDoubleVector(data); } public static void startDvecVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static void addFvec(FlatBufferBuilder builder, int fvecOffset) { builder.addOffset(9, fvecOffset, 0); } - public static int createFvecVector(FlatBufferBuilder builder, float[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addFloat(data[i]); return builder.endVector(); } + public static int createFvecVector(FlatBufferBuilder builder, float[] data) { return builder.createFloatVector(data); } public static void startFvecVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static int endMonsterExtra(FlatBufferBuilder builder) { int o = builder.endTable(); diff --git a/tests/MyGame/MonsterExtraT.java b/tests/MyGame/MonsterExtraT.java index b35a4093aea..b0eb7126b09 100644 --- a/tests/MyGame/MonsterExtraT.java +++ b/tests/MyGame/MonsterExtraT.java @@ -83,6 +83,20 @@ public MonsterExtraT() { this.dvec = null; this.fvec = null; } + + public MonsterExtraT(double d0, double d1, double d2, double d3, float f0, float f1, float f2, float f3, double[] dvec, float[] fvec) { + this.d0 = d0; + this.d1 = d1; + this.d2 = d2; + this.d3 = d3; + this.f0 = f0; + this.f1 = f1; + this.f2 = f2; + this.f3 = f3; + this.dvec = dvec; + this.fvec = fvec; + } + public static MonsterExtraT deserializeFromBinary(byte[] fbBuffer) { return MonsterExtra.getRootAsMonsterExtra(ByteBuffer.wrap(fbBuffer)).unpack(); } diff --git a/tests/union_vector/AttackerT.java b/tests/union_vector/AttackerT.java index c5f45199c20..52062fe8711 100644 --- a/tests/union_vector/AttackerT.java +++ b/tests/union_vector/AttackerT.java @@ -27,5 +27,9 @@ public class AttackerT { public AttackerT() { this.swordAttackDamage = 0; } + + public AttackerT(int swordAttackDamage) { + this.swordAttackDamage = swordAttackDamage; + } } diff --git a/tests/union_vector/BookReaderT.java b/tests/union_vector/BookReaderT.java index b328b91bc99..911a1af735a 100644 --- a/tests/union_vector/BookReaderT.java +++ b/tests/union_vector/BookReaderT.java @@ -27,5 +27,9 @@ public class BookReaderT { public BookReaderT() { this.booksRead = 0; } + + public BookReaderT(int booksRead) { + this.booksRead = booksRead; + } } diff --git a/tests/union_vector/FallingTubT.java b/tests/union_vector/FallingTubT.java index 8c4ab9d1eb9..395dc389ade 100644 --- a/tests/union_vector/FallingTubT.java +++ b/tests/union_vector/FallingTubT.java @@ -27,5 +27,9 @@ public class FallingTubT { public FallingTubT() { this.weight = 0; } + + public FallingTubT(int weight) { + this.weight = weight; + } } diff --git a/tests/union_vector/HandFanT.java b/tests/union_vector/HandFanT.java index 982c03d800d..007a1cfa66a 100644 --- a/tests/union_vector/HandFanT.java +++ b/tests/union_vector/HandFanT.java @@ -27,5 +27,9 @@ public class HandFanT { public HandFanT() { this.length = 0; } + + public HandFanT(int length) { + this.length = length; + } } diff --git a/tests/union_vector/MovieT.java b/tests/union_vector/MovieT.java index 3dd85b94461..d3a09f47ea7 100644 --- a/tests/union_vector/MovieT.java +++ b/tests/union_vector/MovieT.java @@ -33,6 +33,12 @@ public MovieT() { this.mainCharacter = null; this.characters = null; } + + public MovieT(CharacterUnion mainCharacter, CharacterUnion[] characters) { + this.mainCharacter = mainCharacter; + this.characters = characters; + } + public static MovieT deserializeFromBinary(byte[] fbBuffer) { return Movie.getRootAsMovie(ByteBuffer.wrap(fbBuffer)).unpack(); } diff --git a/tests/union_vector/RapunzelT.java b/tests/union_vector/RapunzelT.java index 24f934077ee..cf935c4afc0 100644 --- a/tests/union_vector/RapunzelT.java +++ b/tests/union_vector/RapunzelT.java @@ -27,5 +27,9 @@ public class RapunzelT { public RapunzelT() { this.hairLength = 0; } + + public RapunzelT(int hairLength) { + this.hairLength = hairLength; + } }