Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions java/src/test/java/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public void TestEnums() {
assertThat(Any.name(Any.Monster)).isEqualTo("Monster");
}

@org.junit.Test
public void TestUlongEnums() {
assertThat(Condition.Empty).isEqualTo(0L);
assertThat(Condition.GreaterThanEqual)
.isEqualTo(Long.parseUnsignedLong("34F605C97C571896", 16));
assertThat(Condition.LessThanEqual)
.isEqualTo(Long.parseUnsignedLong("488B1F9FBC949365", 16));
assertThat(Condition.NotEqual)
.isEqualTo(Long.parseUnsignedLong("88C99E99F1FC6C55", 16));
assertThat(Condition.Equal)
.isEqualTo(Long.parseUnsignedLong("B763CBE88B6F844F", 16));
assertThat(Condition.None)
.isEqualTo(Long.parseUnsignedLong("CBF29CE484222645", 16));
assertThat(Condition.Between)
.isEqualTo(Long.parseUnsignedLong("E5E34D77DC75E2EF", 16));
}

static void TestBuffer(ByteBuffer bb) {
assertThat(Monster.MonsterBufferHasIdentifier(bb)).isEqualTo(true);

Expand Down
3 changes: 3 additions & 0 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@ def glob(path, pattern):
dictionary_lookup_schema = "dictionary_lookup.fbs"
flatc(["--java", "--kotlin"], schema=dictionary_lookup_schema)

# Java ulong enum generation test
flatc(["--java"], schema="java_ulong_enum_test.fbs")

# Swift Tests
swift_prefix = "swift/Tests/Flatbuffers"
flatc(
Expand Down
11 changes: 10 additions & 1 deletion src/idl_gen_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ class JavaGenerator : public BaseGenerator {
return GenDefaultValue(field);
}

std::string GenEnumValue(const EnumDef& enum_def, const EnumVal& enum_val) const {
auto value = enum_def.ToString(enum_val);
if (enum_def.underlying_type.base_type == BASE_TYPE_ULONG) {
const uint64_t unsigned_value = StringToUInt(value.c_str());
return NumToString(static_cast<int64_t>(unsigned_value));
}
return value;
}

void GenEnum(EnumDef& enum_def, std::string& code) const {
if (enum_def.generated) return;

Expand All @@ -445,7 +454,7 @@ class JavaGenerator : public BaseGenerator {
code += GenTypeBasic(DestinationType(enum_def.underlying_type, false));
code += " ";
code += namer_.Variant(ev) + " = ";
code += enum_def.ToString(ev);
code += GenEnumValue(enum_def, ev);
if (enum_def.underlying_type.base_type == BASE_TYPE_UINT ||
enum_def.underlying_type.base_type == BASE_TYPE_LONG ||
enum_def.underlying_type.base_type == BASE_TYPE_ULONG) {
Expand Down
16 changes: 16 additions & 0 deletions tests/MyGame/Example/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// automatically generated by the FlatBuffers compiler, do not modify

package MyGame.Example;

@SuppressWarnings("unused")
public final class Condition {
private Condition() { }
public static final long Empty = 0L;
public static final long GreaterThanEqual = 3816244097175722134L;
public static final long LessThanEqual = 5227306563417707365L;
public static final long NotEqual = -8590160430205473707L;
public static final long Equal = -5232114142442191793L;
public static final long None = -3750763034362894779L;
public static final long Between = -1881575042115575057L;
}

11 changes: 11 additions & 0 deletions tests/java_ulong_enum_test.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MyGame.Example;

enum Condition : ulong {
Empty = 0,
GreaterThanEqual = 0x34F605C97C571896,
LessThanEqual = 0x488B1F9FBC949365,
NotEqual = 0x88C99E99F1FC6C55,
Equal = 0xB763CBE88B6F844F,
None = 0xCBF29CE484222645,
Between = 0xE5E34D77DC75E2EF
}