Skip to content

Commit

Permalink
Fix a few more problems google#2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ofir Moses committed Apr 20, 2021
1 parent ed4e171 commit 1e3d45a
Show file tree
Hide file tree
Showing 58 changed files with 571 additions and 573 deletions.
99 changes: 47 additions & 52 deletions src/idl_gen_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ class JavaGenerator : public BaseGenerator {
GenPackUnPack_ObjectAPI(struct_def, code_ptr, opts, struct_has_create,
field_has_create_set);
}
code += "}";
code += "}\n\n";
}

std::string GenOptionalScalarCheck(FieldDef &field) const {
Expand Down Expand Up @@ -1298,9 +1298,9 @@ class JavaGenerator : public BaseGenerator {
code += " private Object value;\n";
code += "\n";
// getters and setters
code += " public " + union_type + " getType() { return this.type; }\n\n";
code += " public " + union_type + " getType() { return type; }\n\n";
code += " public void setType(" + union_type + " type) { this.type = type; }\n\n";
code += " public Object getValue() { return this.value; }\n\n";
code += " public Object getValue() { return value; }\n\n";
code += " public void setValue(Object value) { this.value = value; }\n\n";
// Constructor
code += " public " + union_name + "() {\n";
Expand All @@ -1319,7 +1319,7 @@ class JavaGenerator : public BaseGenerator {
} else {
code += " public ";
}
code += type_name + " as" + ev.name + "() { return (" + type_name + ") this.value; }\n";
code += type_name + " as" + ev.name + "() { return (" + type_name + ") value; }\n";
}
code += "\n";
// pack()
Expand All @@ -1333,7 +1333,7 @@ class JavaGenerator : public BaseGenerator {
} else {
code += " case " + enum_def.name + "." + ev.name + ": return ";
if (IsString(ev.union_type)) {
code += "builder.CreateString(_o.As" + ev.name + "()).Value;\n";
code += "builder.createString(_o.as" + ev.name + "());\n";
} else {
code += GenTypeGet(ev.union_type) + ".pack(builder, _o.as" + ev.name +
"());\n";
Expand All @@ -1360,23 +1360,24 @@ class JavaGenerator : public BaseGenerator {
bool is_vector) const {
auto &code = *code_ptr;

std::string varialbe_type = type_name;
std::string varialbe_name = "_o" + MakeCamel(camel_name, true);
std::string variable_type = type_name;
std::string variable_name = "_o" + MakeCamel(camel_name, true);
std::string type_params = "";
std::string func_suffix = "()";
std::string indent = " ";
if (is_vector) {
varialbe_name += "Element";
variable_type = type_name.substr(0, type_name.length() - 2);
variable_name += "Element";
type_params = "_j";
func_suffix = "(_j)";
indent = " ";
}
code += indent + varialbe_type + " " + varialbe_name + " = new " + varialbe_type + "();\n";
code += indent + GenTypeBasic(DestinationType(enum_def.underlying_type, false)) + " " + varialbe_name + "Type = this." + camel_name + "Type(" + type_params + ");\n";
code += indent + varialbe_name + ".setType(" + varialbe_name + "Type);\n";
code += indent + "Table " + varialbe_name + "Value = this." + camel_name + "(" + type_params + ");\n";
code += indent + variable_type + " " + variable_name + " = new " + variable_type + "();\n";
code += indent + GenTypeBasic(DestinationType(enum_def.underlying_type, false)) + " " + variable_name + "Type = " + camel_name + "Type(" + type_params + ");\n";
code += indent + variable_name + ".setType(" + variable_name + "Type);\n";
code += indent + "Table " + variable_name + "Value = " + camel_name + "(" + type_params + ");\n";
code +=
indent + "switch (" + varialbe_name + "Type) {\n";
indent + "switch (" + variable_name + "Type) {\n";
for (auto eit = enum_def.Vals().begin(); eit != enum_def.Vals().end();
++eit) {
auto &ev = **eit;
Expand All @@ -1385,14 +1386,14 @@ class JavaGenerator : public BaseGenerator {
} else {
code += indent + " case " + WrapInNameSpace(enum_def) + "." + ev.name +
":\n";
code += indent + " " + varialbe_name + ".setValue(" + varialbe_name + "Value != null ? ((" + GenTypeGet(ev.union_type) + ") " + varialbe_name + "Value" + ").unpack() : null);\n";
code += indent + " " + variable_name + ".setValue(" + variable_name + "Value != null ? ((" + GenTypeGet(ev.union_type) + ") " + variable_name + "Value" + ").unpack() : null);\n";
code += indent + " break;\n";
}
}
code += indent + " default: break;\n";
code += indent + "}\n";
if (is_vector) {
code += indent + "_o" + MakeCamel(camel_name, true) + ".add(" + varialbe_name + ");\n";
code += indent + "_o" + MakeCamel(camel_name, true) + "[_j] = " + variable_name + ";\n";
}
}

Expand All @@ -1405,7 +1406,7 @@ class JavaGenerator : public BaseGenerator {
// unpack()
code += " public " + struct_name + " unpack() {\n";
code += " " + struct_name + " _o = new " + struct_name + "();\n";
code += " this.unpackTo(_o);\n";
code += " unpackTo(_o);\n";
code += " return _o;\n";
code += " }\n";
// unpackTo()
Expand All @@ -1425,9 +1426,9 @@ class JavaGenerator : public BaseGenerator {
case BASE_TYPE_STRUCT: {
auto fixed = struct_def.fixed && field.value.type.struct_def->fixed;
if (fixed) {
code += start + "this." + camel_name + "().unpack();\n";
code += start + camel_name + "().unpack();\n";
} else {
code += start + "this." + camel_name + "() != null ? this." +
code += start + camel_name + "() != null ? " +
camel_name + "().unpack() : null;\n";
}
break;
Expand All @@ -1439,33 +1440,35 @@ class JavaGenerator : public BaseGenerator {
code += start + "new " + type_name.substr(0, type_name.length() - 1) +
length_str + "];\n";
code += " for (int _j = 0; _j < " + length_str + "; ++_j) { _o" +
camel_name_with_first + "[_j] = this." + camel_name + "(_j)" +
camel_name_with_first + "[_j] = " + camel_name + "(_j)" +
unpack_method + "; }\n";
break;
}
case BASE_TYPE_VECTOR:
if (field.value.type.element == BASE_TYPE_UNION) {
code += start + "new " +
GenConcreteTypeGet_ObjectAPI(field.value.type, opts) + "();\n";
code += " for (int _j = 0; _j < this." + camel_name +
GenConcreteTypeGet_ObjectAPI(field.value.type, opts).substr(0, type_name.length() - 1) +
camel_name + "Length()];\n";
code += " for (int _j = 0; _j < " + camel_name +
"Length(); ++_j) {\n";
GenUnionUnPack_ObjectAPI(*field.value.type.enum_def, code_ptr, type_name,
camel_name, true);
code += " }\n";
} else if (field.value.type.element != BASE_TYPE_UTYPE) {
auto fixed = field.value.type.struct_def == nullptr;
code += start + "new " +
GenConcreteTypeGet_ObjectAPI(field.value.type, opts) + "();\n";
code += " for (int _j = 0; _j < this." + camel_name +
GenConcreteTypeGet_ObjectAPI(field.value.type, opts).substr(0, type_name.length() - 1) +
camel_name + "Length()];\n";
code += " for (int _j = 0; _j < " + camel_name +
"Length(); ++_j) {";
code += "_o" + camel_name_with_first + ".add(";
code += "_o" + camel_name_with_first + "[_j] = ";
if (fixed) {
code += "this." + camel_name + "(_j)";
code += camel_name + "(_j)";
} else {
code += "this." + camel_name + "(_j) != null ? this." +
camel_name + "(_j).unpack() : null";
code += "(" + camel_name + "(_j) != null ? " +
camel_name + "(_j).unpack() : null)";
}
code += ");}\n";
code += ";}\n";
}
break;
case BASE_TYPE_UTYPE: break;
Expand All @@ -1476,9 +1479,9 @@ class JavaGenerator : public BaseGenerator {
}
default: {
if (field.IsScalarOptional()) {
code += start + "this.has" + camel_name_with_first + "() ? this." + camel_name + "() : null;\n";
code += start + "has" + camel_name_with_first + "() ? " + camel_name + "() : null;\n";
} else {
code += start + "this." + camel_name + "();\n";
code += start + camel_name + "();\n";
}
break;
}
Expand Down Expand Up @@ -1548,15 +1551,17 @@ class JavaGenerator : public BaseGenerator {
property_name = camel_name.substr(0, camel_name.size() - 4);
array_type = GenTypeBasic(DestinationType(field.value.type.enum_def->underlying_type, false));
element_type = field.value.type.enum_def->name + "Union";
to_array = "_o." + GenGetterFuncName_ObjectAPI(property_name) + "().get(_j).getType()";
to_array = "_o." + GenGetterFuncName_ObjectAPI(property_name) + "()[_j].getType()";
break;
case BASE_TYPE_UNION:
array_type = "int";
element_type = WrapInNameSpace(*field.value.type.enum_def) + "Union";
to_array = WrapInNameSpace(*field.value.type.enum_def) +
"Union.pack(builder, _o." + GenGetterFuncName_ObjectAPI(property_name) + "().get(_j))";
"Union.pack(builder, _o." + GenGetterFuncName_ObjectAPI(property_name) + "()[_j])";
break;
default:
gen_for_loop = false;
array_name = "_o." + GenGetterFuncName_ObjectAPI(property_name) + "()";
array_type = GenTypeNameDest(field.value.type);
element_type = array_type;
to_array = "_e";
Expand All @@ -1565,13 +1570,10 @@ class JavaGenerator : public BaseGenerator {
code += " int _" + camel_name + " = 0;\n";
code += " if (_o." + GenGetterFuncName_ObjectAPI(property_name) + "() != null) {\n";
if (gen_for_loop) {
code += " " + array_type + "[] " + array_name + " = new " + array_type + "[_o." + GenGetterFuncName_ObjectAPI(property_name) + "().size()];\n";
code += " " + array_type + "[] " + array_name + " = new " + array_type + "[_o." + GenGetterFuncName_ObjectAPI(property_name) + "().length];\n";
code += " int _j = 0;\n";
code += " for (" + element_type + " _e : _o." + GenGetterFuncName_ObjectAPI(property_name) + "()) { ";
code += array_name + "[_j] = " + to_array + "; _j++;}\n";
} else {
code += " " + array_type + "[] " + array_name + " = _o." + GenGetterFuncName_ObjectAPI(property_name) +
"().toArray();\n";
}
code += " _" + camel_name + " = create" + camel_name_with_first +
"Vector(builder, " + array_name + ");\n";
Expand All @@ -1586,7 +1588,7 @@ class JavaGenerator : public BaseGenerator {
: type_name + ".pack(builder, _e);";
code += " int _" + camel_name + " = 0;\n";
code += " if (_o." + GenGetterFuncName_ObjectAPI(field.name) + "() != null) {\n";
code += " start" + camel_name_with_first + "Vector(builder, _o." + GenGetterFuncName_ObjectAPI(field.name) + "().size());\n";
code += " start" + camel_name_with_first + "Vector(builder, _o." + GenGetterFuncName_ObjectAPI(field.name) + "().length);\n";
code += " for (" + element_type_name + " _e : _o." + GenGetterFuncName_ObjectAPI(field.name) + "()) { ";
code += pack_method + "}\n";
code += " _" + camel_name + " = builder.endVector();\n";
Expand Down Expand Up @@ -1615,7 +1617,7 @@ class JavaGenerator : public BaseGenerator {
".NONE : " + "_o.get" + camel_name_with_first + "().getType();\n";
code +=
" " + GenOffsetType() + " _" + camel_name + " = _o.get" + camel_name_with_first +
"() == null ? 0 : " + field.value.type.enum_def->name +
"() == null ? 0 : " + WrapInNameSpace(*field.value.type.enum_def) +
"Union.pack(builder, _o.get" + camel_name_with_first + "());\n";
break;
}
Expand Down Expand Up @@ -1717,7 +1719,7 @@ class JavaGenerator : public BaseGenerator {
// scalar
default: {
if (field.IsScalarOptional()) {
code += " if (_o." + GenGetterFuncName_ObjectAPI(field.name) + "() != null) { add" + camel_name_with_first + "(builder, _o." + GenGetterFuncName_ObjectAPI(field.name) + "(); }\n";
code += " if (_o." + GenGetterFuncName_ObjectAPI(field.name) + "() != null) { add" + camel_name_with_first + "(builder, _o." + GenGetterFuncName_ObjectAPI(field.name) + "()); }\n";
} else {
code += " add" + camel_name_with_first + "(builder, _o." + GenGetterFuncName_ObjectAPI(field.name) + "());\n";
}
Expand Down Expand Up @@ -1889,13 +1891,9 @@ class JavaGenerator : public BaseGenerator {
return type_name;
}
switch (type.base_type) {
case BASE_TYPE_ARRAY: {
type_name = type_name + "[]";
break;
}
case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
case BASE_TYPE_VECTOR: {
type_name = ConvertPrimitiveTypeToObjectWrapper_ObjectAPI(type_name);
type_name = "List<" + type_name + ">";
type_name = type_name + "[]";
break;
}
default: break;
Expand Down Expand Up @@ -1931,12 +1929,9 @@ class JavaGenerator : public BaseGenerator {
}

switch (type.base_type) {
case BASE_TYPE_ARRAY: {
type_name = type_name + "[]";
break;
}
case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
case BASE_TYPE_VECTOR: {
type_name = "ArrayList<>";
type_name = type_name + "[]";
break;
}
default: break;
Expand Down Expand Up @@ -1979,7 +1974,7 @@ class JavaGenerator : public BaseGenerator {
auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts, false, true);
if (field.IsScalarOptional()) type_name = ConvertPrimitiveTypeToObjectWrapper_ObjectAPI(type_name);
auto camel_name = MakeCamel(field.name, false);
code += " public " + type_name + " " + GenGetterFuncName_ObjectAPI(field.name) + "() { return this." + camel_name + "; }\n\n";
code += " public " + type_name + " " + GenGetterFuncName_ObjectAPI(field.name) + "() { return " + camel_name + "; }\n\n";
code += " public void " + GenSetterFuncName_ObjectAPI(field.name) + "(" + type_name + " " + camel_name + ") { this." + camel_name + " = "+ camel_name + "; }\n\n";
}
// Generate Constructor
Expand Down Expand Up @@ -2035,7 +2030,7 @@ class JavaGenerator : public BaseGenerator {
code += " return fbb.sizedByteArray();\n";
code += " }\n";
}
code += "}";
code += "}\n\n";
}

// This tracks the current namespace used to determine if a type need to be
Expand Down
9 changes: 5 additions & 4 deletions tests/MyGame/Example/Ability.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static final class Vector extends BaseVector {
}
public AbilityT unpack() {
AbilityT _o = new AbilityT();
this.unpackTo(_o);
unpackTo(_o);
return _o;
}
public void unpackTo(AbilityT _o) {
long _oId = this.id();
long _oId = id();
_o.setId(_oId);
long _oDistance = this.distance();
long _oDistance = distance();
_o.setDistance(_oDistance);
}
public static int pack(FlatBufferBuilder builder, AbilityT _o) {
Expand All @@ -48,4 +48,5 @@ public static int pack(FlatBufferBuilder builder, AbilityT _o) {
_o.getId(),
_o.getDistance());
}
}
}

7 changes: 4 additions & 3 deletions tests/MyGame/Example/AbilityT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class AbilityT {
private long id;
private long distance;

public long getId() { return this.id; }
public long getId() { return id; }

public void setId(long id) { this.id = id; }

public long getDistance() { return this.distance; }
public long getDistance() { return distance; }

public void setDistance(long distance) { this.distance = distance; }

Expand All @@ -24,4 +24,5 @@ public AbilityT() {
this.id = 0L;
this.distance = 0L;
}
}
}

10 changes: 5 additions & 5 deletions tests/MyGame/Example/AnyAmbiguousAliasesUnion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class AnyAmbiguousAliasesUnion {
private byte type;
private Object value;

public byte getType() { return this.type; }
public byte getType() { return type; }

public void setType(byte type) { this.type = type; }

public Object getValue() { return this.value; }
public Object getValue() { return value; }

public void setValue(Object value) { this.value = value; }

Expand All @@ -21,9 +21,9 @@ public AnyAmbiguousAliasesUnion() {
this.value = null;
}

public MyGame.Example.MonsterT asM1() { return (MyGame.Example.MonsterT) this.value; }
public MyGame.Example.MonsterT asM2() { return (MyGame.Example.MonsterT) this.value; }
public MyGame.Example.MonsterT asM3() { return (MyGame.Example.MonsterT) this.value; }
public MyGame.Example.MonsterT asM1() { return (MyGame.Example.MonsterT) value; }
public MyGame.Example.MonsterT asM2() { return (MyGame.Example.MonsterT) value; }
public MyGame.Example.MonsterT asM3() { return (MyGame.Example.MonsterT) value; }

public static int pack(FlatBufferBuilder builder, AnyAmbiguousAliasesUnion _o) {
switch (_o.type) {
Expand Down
10 changes: 5 additions & 5 deletions tests/MyGame/Example/AnyUnion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class AnyUnion {
private byte type;
private Object value;

public byte getType() { return this.type; }
public byte getType() { return type; }

public void setType(byte type) { this.type = type; }

public Object getValue() { return this.value; }
public Object getValue() { return value; }

public void setValue(Object value) { this.value = value; }

Expand All @@ -21,9 +21,9 @@ public AnyUnion() {
this.value = null;
}

public MyGame.Example.MonsterT asMonster() { return (MyGame.Example.MonsterT) this.value; }
MyGame.Example.TestSimpleTableWithEnumT asTestSimpleTableWithEnum() { return (MyGame.Example.TestSimpleTableWithEnumT) this.value; }
public MyGame.Example2.MonsterT asMyGame_Example2_Monster() { return (MyGame.Example2.MonsterT) this.value; }
public MyGame.Example.MonsterT asMonster() { return (MyGame.Example.MonsterT) value; }
MyGame.Example.TestSimpleTableWithEnumT asTestSimpleTableWithEnum() { return (MyGame.Example.TestSimpleTableWithEnumT) value; }
public MyGame.Example2.MonsterT asMyGame_Example2_Monster() { return (MyGame.Example2.MonsterT) value; }

public static int pack(FlatBufferBuilder builder, AnyUnion _o) {
switch (_o.type) {
Expand Down
Loading

0 comments on commit 1e3d45a

Please sign in to comment.