mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
Fixed vector of unions crash in java (#5190)
* Fixed vector of unions crash in java * Regenerated test code * Fixed windows tests
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
69d761d15e
commit
8f8fb2b367
@@ -95,7 +95,7 @@ const LanguageParameters &GetLangParams(IDLOptions::Language lang) {
|
||||
"",
|
||||
"import java.nio.*;\nimport java.lang.*;\nimport "
|
||||
"java.util.*;\nimport com.google.flatbuffers.*;\n",
|
||||
"\n@SuppressWarnings(\"unused\")",
|
||||
"\n@SuppressWarnings(\"unused\")\n",
|
||||
"\n@javax.annotation.Generated(value=\"flatc\")\n",
|
||||
{
|
||||
"/**",
|
||||
@@ -1035,6 +1035,8 @@ class GeneralGenerator : public BaseGenerator {
|
||||
? index
|
||||
: lang_.accessor_prefix + "__indirect(" + index + ")";
|
||||
code += ", " + lang_.accessor_prefix + "bb";
|
||||
} else if (vectortype.base_type == BASE_TYPE_UNION) {
|
||||
code += index + " - bb_pos";
|
||||
} else {
|
||||
code += index;
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@ rem Compile then run the Java test.
|
||||
|
||||
set batch_file_dir=%~d0%~p0
|
||||
|
||||
javac -g -classpath %batch_file_dir%\..\java;%batch_file_dir%;%batch_file_dir%\namespace_test JavaTest.java
|
||||
java -classpath %batch_file_dir%\..\java;%batch_file_dir%;%batch_file_dir%\namespace_test JavaTest
|
||||
javac -g -classpath %batch_file_dir%\..\java;%batch_file_dir%;%batch_file_dir%\namespace_test;%batch_file_dir%\union_vector JavaTest.java
|
||||
java -classpath %batch_file_dir%\..\java;%batch_file_dir%;%batch_file_dir%\namespace_test;%batch_file_dir%\union_vector JavaTest
|
||||
|
||||
@@ -73,6 +73,8 @@ class JavaTest {
|
||||
|
||||
TestSizedInputStream();
|
||||
|
||||
TestVectorOfUnions();
|
||||
|
||||
System.out.println("FlatBuffers test: completed successfully");
|
||||
}
|
||||
|
||||
@@ -415,6 +417,40 @@ class JavaTest {
|
||||
TestEq(pos.x(), 1.0f);
|
||||
}
|
||||
|
||||
static void TestVectorOfUnions() {
|
||||
final FlatBufferBuilder fbb = new FlatBufferBuilder();
|
||||
|
||||
final int swordAttackDamage = 1;
|
||||
|
||||
final int[] characterVector = new int[] {
|
||||
Attacker.createAttacker(fbb, swordAttackDamage),
|
||||
};
|
||||
|
||||
final byte[] characterTypeVector = new byte[]{
|
||||
Character.MuLan,
|
||||
};
|
||||
|
||||
Movie.finishMovieBuffer(
|
||||
fbb,
|
||||
Movie.createMovie(
|
||||
fbb,
|
||||
(byte)0,
|
||||
(byte)0,
|
||||
Movie.createCharactersTypeVector(fbb, characterTypeVector),
|
||||
Movie.createCharactersVector(fbb, characterVector)
|
||||
)
|
||||
);
|
||||
|
||||
final Movie movie = Movie.getRootAsMovie(fbb.dataBuffer());
|
||||
|
||||
TestEq(movie.charactersTypeLength(), characterTypeVector.length);
|
||||
TestEq(movie.charactersLength(), characterVector.length);
|
||||
|
||||
TestEq(movie.charactersType(0), characterTypeVector[0]);
|
||||
|
||||
TestEq(((Attacker)movie.characters(new Attacker(), 0)).swordAttackDamage(), swordAttackDamage);
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
if (!a.equals(b)) {
|
||||
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
|
||||
|
||||
@@ -36,7 +36,7 @@ if ! find "${testdir}/../java" -type f -name "*.class" -delete; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
javac -d "${targetdir}" -classpath "${testdir}/../java:${testdir}:${testdir}/namespace_test" "${testdir}/JavaTest.java"
|
||||
javac -d "${targetdir}" -classpath "${testdir}/../java:${testdir}:${testdir}/namespace_test:${testdir}/union_vector" "${testdir}/JavaTest.java"
|
||||
|
||||
(cd "${testdir}" && java -classpath "${targetdir}" JavaTest )
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Ability extends Struct {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Ability extends Struct {
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
public Ability __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")/**
|
||||
@SuppressWarnings("unused")
|
||||
/**
|
||||
* an example documentation comment: monster object
|
||||
*/
|
||||
public final class Monster extends Table {
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Referrable extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Referrable extends Table {
|
||||
public static Referrable getRootAsReferrable(ByteBuffer _bb) { return getRootAsReferrable(_bb, new Referrable()); }
|
||||
public static Referrable getRootAsReferrable(ByteBuffer _bb, Referrable obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Stat extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Stat extends Table {
|
||||
public static Stat getRootAsStat(ByteBuffer _bb) { return getRootAsStat(_bb, new Stat()); }
|
||||
public static Stat getRootAsStat(ByteBuffer _bb, Stat obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Test extends Struct {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Test extends Struct {
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
public Test __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")final class TestSimpleTableWithEnum extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
final class TestSimpleTableWithEnum extends Table {
|
||||
public static TestSimpleTableWithEnum getRootAsTestSimpleTableWithEnum(ByteBuffer _bb) { return getRootAsTestSimpleTableWithEnum(_bb, new TestSimpleTableWithEnum()); }
|
||||
public static TestSimpleTableWithEnum getRootAsTestSimpleTableWithEnum(ByteBuffer _bb, TestSimpleTableWithEnum obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class TypeAliases extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class TypeAliases extends Table {
|
||||
public static TypeAliases getRootAsTypeAliases(ByteBuffer _bb) { return getRootAsTypeAliases(_bb, new TypeAliases()); }
|
||||
public static TypeAliases getRootAsTypeAliases(ByteBuffer _bb, TypeAliases obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Vec3 extends Struct {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Vec3 extends Struct {
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class Monster extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class Monster extends Table {
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb) { return getRootAsMonster(_bb, new Monster()); }
|
||||
public static Monster getRootAsMonster(ByteBuffer _bb, Monster obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class InParentNamespace extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class InParentNamespace extends Table {
|
||||
public static InParentNamespace getRootAsInParentNamespace(ByteBuffer _bb) { return getRootAsInParentNamespace(_bb, new InParentNamespace()); }
|
||||
public static InParentNamespace getRootAsInParentNamespace(ByteBuffer _bb, InParentNamespace obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class MonsterExtra extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class MonsterExtra extends Table {
|
||||
public static MonsterExtra getRootAsMonsterExtra(ByteBuffer _bb) { return getRootAsMonsterExtra(_bb, new MonsterExtra()); }
|
||||
public static MonsterExtra getRootAsMonsterExtra(ByteBuffer _bb, MonsterExtra obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class StructInNestedNS extends Struct {
|
||||
@SuppressWarnings("unused")
|
||||
public final class StructInNestedNS extends Struct {
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
public StructInNestedNS __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class TableInNestedNS extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class TableInNestedNS extends Table {
|
||||
public static TableInNestedNS getRootAsTableInNestedNS(ByteBuffer _bb) { return getRootAsTableInNestedNS(_bb, new TableInNestedNS()); }
|
||||
public static TableInNestedNS getRootAsTableInNestedNS(ByteBuffer _bb, TableInNestedNS obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class SecondTableInA extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class SecondTableInA extends Table {
|
||||
public static SecondTableInA getRootAsSecondTableInA(ByteBuffer _bb) { return getRootAsSecondTableInA(_bb, new SecondTableInA()); }
|
||||
public static SecondTableInA getRootAsSecondTableInA(ByteBuffer _bb, SecondTableInA obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class TableInFirstNS extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class TableInFirstNS extends Table {
|
||||
public static TableInFirstNS getRootAsTableInFirstNS(ByteBuffer _bb) { return getRootAsTableInFirstNS(_bb, new TableInFirstNS()); }
|
||||
public static TableInFirstNS getRootAsTableInFirstNS(ByteBuffer _bb, TableInFirstNS obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.lang.*;
|
||||
import java.util.*;
|
||||
import com.google.flatbuffers.*;
|
||||
|
||||
@SuppressWarnings("unused")public final class TableInC extends Table {
|
||||
@SuppressWarnings("unused")
|
||||
public final class TableInC extends Table {
|
||||
public static TableInC getRootAsTableInC(ByteBuffer _bb) { return getRootAsTableInC(_bb, new TableInC()); }
|
||||
public static TableInC getRootAsTableInC(ByteBuffer _bb, TableInC obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
|
||||
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
|
||||
|
||||
@@ -19,7 +19,7 @@ public final class Movie extends Table {
|
||||
public int charactersTypeLength() { int o = __offset(8); return o != 0 ? __vector_len(o) : 0; }
|
||||
public ByteBuffer charactersTypeAsByteBuffer() { return __vector_as_bytebuffer(8, 1); }
|
||||
public ByteBuffer charactersTypeInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 8, 1); }
|
||||
public Table characters(Table obj, int j) { int o = __offset(10); return o != 0 ? __union(obj, __vector(o) + j * 4) : null; }
|
||||
public Table characters(Table obj, int j) { int o = __offset(10); return o != 0 ? __union(obj, __vector(o) + j * 4 - bb_pos) : null; }
|
||||
public int charactersLength() { int o = __offset(10); return o != 0 ? __vector_len(o) : 0; }
|
||||
|
||||
public static int createMovie(FlatBufferBuilder builder,
|
||||
|
||||
Reference in New Issue
Block a user