diff options
author | Chuck Atkins <320135+chuckatkins@users.noreply.github.com> | 2023-03-03 00:52:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-02 21:52:03 -0800 |
commit | b90cc35a10568c7453afb94b29ab2f005939fcba (patch) | |
tree | 7b3f61aa6d6619be9b7e344d897c7d636637e3b3 | |
parent | 79d6abb42ed83378b2ef93b2fd1986a2625bb832 (diff) | |
download | flatbuffers-b90cc35a10568c7453afb94b29ab2f005939fcba.tar.gz flatbuffers-b90cc35a10568c7453afb94b29ab2f005939fcba.tar.bz2 flatbuffers-b90cc35a10568c7453afb94b29ab2f005939fcba.zip |
Add a --java-package-prefix option to flatc (#7848)
Co-authored-by: Derek Bailey <derekbailey@google.com>
-rw-r--r-- | include/flatbuffers/idl.h | 1 | ||||
-rw-r--r-- | src/flatc.cpp | 5 | ||||
-rw-r--r-- | src/idl_gen_java.cpp | 72 |
3 files changed, 59 insertions, 19 deletions
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 7f71adb7..8f08003d 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -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; diff --git a/src/flatc.cpp b/src/flatc.cpp index 699a9b83..31291a25 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -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", "", @@ -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") { diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 96425518..2faca5c5 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -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() { @@ -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"; @@ -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); @@ -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"; } @@ -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 { @@ -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)"; @@ -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; @@ -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"; @@ -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; @@ -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; } @@ -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"; } @@ -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"; } @@ -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; @@ -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(); } |