Skip to content

Commit

Permalink
Add a --java-package-prefix option to flatc (#7848)
Browse files Browse the repository at this point in the history
Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
chuckatkins and dbaileychess authored Mar 3, 2023
1 parent 79d6abb commit b90cc35
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
1 change: 1 addition & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ struct IDLOptions {
CaseStyle cpp_object_api_field_case_style;
bool cpp_direct_copy;
bool gen_nullable;
std::string java_package_prefix;
bool java_checkerframework;
bool gen_generated;
bool gen_json_coders;
Expand Down
5 changes: 5 additions & 0 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ const static FlatCOption flatc_options[] = {
{ "", "gen-compare", "", "Generate operator== for object-based API types." },
{ "", "gen-nullable", "",
"Add Clang _Nullable for C++ pointer. or @Nullable for Java" },
{ "", "java-package-prefix", "",
"Add a prefix to the generated package name for Java." },
{ "", "java-checkerframe", "", "Add @Pure for Java." },
{ "", "gen-generated", "", "Add @Generated annotation for Java." },
{ "", "gen-jvmstatic", "",
Expand Down Expand Up @@ -516,6 +518,9 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
Error("unknown case style: " + std::string(argv[argi]), true);
} else if (arg == "--gen-nullable") {
opts.gen_nullable = true;
} else if (arg == "--java-package-prefix") {
if (++argi >= argc) Error("missing prefix following: " + arg, true);
opts.java_package_prefix = argv[argi];
} else if (arg == "--java-checkerframework") {
opts.java_checkerframework = true;
} else if (arg == "--gen-generated") {
Expand Down
72 changes: 53 additions & 19 deletions src/idl_gen_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ class JavaGenerator : public BaseGenerator {

public:
JavaGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
const std::string &file_name,
const std::string &package_prefix)
: BaseGenerator(parser, path, file_name, "", ".", "java"),
cur_name_space_(nullptr),
cur_name_space_(nullptr),
namer_(WithFlagOptions(JavaDefaultConfig(), parser.opts, path),
JavaKeywords()) {}
JavaKeywords()) {
if (!package_prefix.empty()) {
std::istringstream iss(package_prefix);
std::string component;
while(std::getline(iss, component, '.')) {
package_prefix_ns_.components.push_back(component);
}
package_prefix_ = package_prefix_ns_.GetFullyQualifiedName("") + ".";
}
}

JavaGenerator &operator=(const JavaGenerator &);
bool generate() {
Expand Down Expand Up @@ -173,7 +183,13 @@ class JavaGenerator : public BaseGenerator {
std::string code;
code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";

const std::string namespace_name = FullNamespace(".", ns);
Namespace combined_ns = package_prefix_ns_;
std::copy(
ns.components.begin(),
ns.components.end(),
std::back_inserter(combined_ns.components));

const std::string namespace_name = FullNamespace(".", combined_ns);
if (!namespace_name.empty()) {
code += "package " + namespace_name + ";";
code += "\n\n";
Expand Down Expand Up @@ -207,7 +223,7 @@ class JavaGenerator : public BaseGenerator {

code += classcode;
if (!namespace_name.empty()) code += "";
const std::string dirs = namer_.Directories(ns);
const std::string dirs = namer_.Directories(combined_ns);
EnsureDirExists(dirs);
const std::string filename =
dirs + namer_.File(defname, /*skips=*/SkipFile::Suffix);
Expand Down Expand Up @@ -247,7 +263,8 @@ class JavaGenerator : public BaseGenerator {
switch (type.base_type) {
case BASE_TYPE_STRING: return "String";
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
case BASE_TYPE_STRUCT: return namer_.NamespacedType(*type.struct_def);
case BASE_TYPE_STRUCT:
return Prefixed(namer_.NamespacedType(*type.struct_def));
case BASE_TYPE_UNION: FLATBUFFERS_FALLTHROUGH(); // else fall thru
default: return "Table";
}
Expand Down Expand Up @@ -351,8 +368,9 @@ class JavaGenerator : public BaseGenerator {
FLATBUFFERS_ASSERT(value.type.enum_def);
auto &enum_def = *value.type.enum_def;
auto enum_val = enum_def.FindByValue(value.constant);
return enum_val ? namer_.NamespacedEnumVariant(enum_def, *enum_val)
: value.constant;
return
enum_val ? Prefixed(namer_.NamespacedEnumVariant(enum_def, *enum_val))
: value.constant;
}

std::string GenDefaultValue(const FieldDef &field) const {
Expand Down Expand Up @@ -879,7 +897,7 @@ class JavaGenerator : public BaseGenerator {
for (auto kit = fields.begin(); kit != fields.end(); ++kit) {
auto &key_field = **kit;
if (key_field.key) {
auto qualified_name = namer_.NamespacedType(sd);
auto qualified_name = Prefixed(namer_.NamespacedType(sd));
code += " public " + qualified_name + " ";
code += namer_.Method(field) + "ByKey(";
code += GenTypeNameDest(key_field.value.type) + " key)";
Expand Down Expand Up @@ -957,7 +975,8 @@ class JavaGenerator : public BaseGenerator {
}
// generate object accessors if is nested_flatbuffer
if (field.nested_flatbuffer) {
auto nested_type_name = namer_.NamespacedType(*field.nested_flatbuffer);
auto nested_type_name =
Prefixed(namer_.NamespacedType(*field.nested_flatbuffer));
auto nested_method_name =
namer_.Field(field) + "As" + field.nested_flatbuffer->name;
auto get_nested_method_name = nested_method_name;
Expand Down Expand Up @@ -1437,7 +1456,7 @@ class JavaGenerator : public BaseGenerator {
// deleted when issue #6561 is fixed.
}
code += indent + " case " +
namer_.NamespacedEnumVariant(enum_def, ev) + ":\n";
Prefixed(namer_.NamespacedEnumVariant(enum_def, ev)) + ":\n";
auto actual_type = GenTypeGet(ev.union_type);
code += indent + " " + variable_name + "Value = " + field_name +
"(new " + actual_type + "()" + value_params + ");\n";
Expand Down Expand Up @@ -1635,7 +1654,8 @@ class JavaGenerator : public BaseGenerator {
case BASE_TYPE_UNION:
array_type = "int";
element_type =
namer_.NamespacedType(*field.value.type.enum_def) + "Union";
Prefixed(namer_.NamespacedType(*field.value.type.enum_def))
+ "Union";
to_array = element_type + ".pack(builder, _o." +
namer_.Method("get", property_name) + "()[_j])";
break;
Expand Down Expand Up @@ -1720,11 +1740,11 @@ class JavaGenerator : public BaseGenerator {
field.value.type.enum_def->underlying_type, false)) +
" _" + field_name + "Type = _o." + get_field +
"() == null ? " +
namer_.NamespacedType(*field.value.type.enum_def) +
Prefixed(namer_.NamespacedType(*field.value.type.enum_def)) +
".NONE : " + "_o." + get_field + "().getType();\n";
code += " " + GenOffsetType() + " _" + field_name + " = _o." +
get_field + "() == null ? 0 : " +
namer_.NamespacedType(*field.value.type.enum_def) +
Prefixed(namer_.NamespacedType(*field.value.type.enum_def)) +
"Union.pack(builder, _o." + get_field + "());\n";
break;
}
Expand Down Expand Up @@ -1976,7 +1996,8 @@ class JavaGenerator : public BaseGenerator {
type_name_length, new_type_name);
} else if (type.element == BASE_TYPE_UNION) {
if (wrap_in_namespace) {
type_name = namer_.NamespacedType(*type.enum_def) + "Union";
type_name =
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
} else {
type_name = namer_.Type(*type.enum_def) + "Union";
}
Expand All @@ -1986,7 +2007,8 @@ class JavaGenerator : public BaseGenerator {

case BASE_TYPE_UNION: {
if (wrap_in_namespace) {
type_name = namer_.NamespacedType(*type.enum_def) + "Union";
type_name =
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
} else {
type_name = namer_.Type(*type.enum_def) + "Union";
}
Expand Down Expand Up @@ -2020,13 +2042,15 @@ class JavaGenerator : public BaseGenerator {
type_name.replace(type_name.length() - type_name_length,
type_name_length, new_type_name);
} else if (type.element == BASE_TYPE_UNION) {
type_name = namer_.NamespacedType(*type.enum_def) + "Union";
type_name =
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
}
break;
}

case BASE_TYPE_UNION: {
type_name = namer_.NamespacedType(*type.enum_def) + "Union";
type_name =
Prefixed(namer_.NamespacedType(*type.enum_def)) + "Union";
break;
}
default: break;
Expand Down Expand Up @@ -2160,12 +2184,22 @@ class JavaGenerator : public BaseGenerator {
// prefixed by its namespace
const Namespace *cur_name_space_;
const IdlNamer namer_;

private:
std::string Prefixed(const std::string &str) const {
return package_prefix_ + str;
}

std::string package_prefix_;
Namespace package_prefix_ns_;

};
} // namespace java

bool GenerateJava(const Parser &parser, const std::string &path,
const std::string &file_name) {
java::JavaGenerator generator(parser, path, file_name);
java::JavaGenerator generator(parser, path, file_name,
parser.opts.java_package_prefix);
return generator.generate();
}

Expand Down

0 comments on commit b90cc35

Please sign in to comment.