Apply Namer to Java. (#7194)

* Started applying Namer to Java.

- Java didn't previously have keyword escaping
- Added prefixes and suffixes to the Namer methods
- TODO: migrate previous namer applications to using pre/suffixes
- Java methods / functions are interesting, it's mostly camel case
  except when it involves a struct/enum name. That section is Keep case
- I changed the casing for some internal arguments/variables. This
  violates the "don't change genfiles" rule that I've been using but it
  shouldn't break user code.
- LegacyJavaMethod2 is interesting. Basically, Java has a "mixed" case
  convention where it's camel case, except for the type/variant name
  itself, which is keep case. So a type foo_bar would become getfoo_bar
  instead of getFooBar.

* small fix

* Namer for Namespaces

* removed unused parameter, add const everywhere

* Remove unused argument

* More unused args

* Use mutable reference out parameters

* Made more strings const and inlined const empty strings

* remove do not submit

Co-authored-by: Casper Neo <cneo@google.com>
This commit is contained in:
Casper
2022-03-30 18:13:16 -04:00
committed by GitHub
parent 6c5603fd98
commit fac0d7be02
4 changed files with 405 additions and 389 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -25,6 +25,8 @@ class IdlNamer : public Namer {
using Namer::Variable;
using Namer::Variant;
std::string Constant(const FieldDef &d) const { return Constant(d.name); }
// Types are always structs or enums so we can only expose these two
// overloads.
std::string Type(const StructDef &d) const { return Type(d.name); }
@@ -33,6 +35,9 @@ class IdlNamer : public Namer {
std::string Function(const Definition &s) const { return Function(s.name); }
std::string Field(const FieldDef &s) const { return Field(s.name); }
std::string Field(const FieldDef &d, const std::string &s) const {
return Field(d.name + "_" + s);
}
std::string Variable(const FieldDef &s) const { return Variable(s.name); }
@@ -49,10 +54,21 @@ class IdlNamer : public Namer {
}
std::string ObjectType(const EnumDef &d) const { return ObjectType(d.name); }
std::string Method(const FieldDef &d, const std::string &suffix) const {
return Method(d.name, suffix);
}
std::string Method(const std::string &prefix, const FieldDef &d) const {
return Method(prefix, d.name);
}
std::string Namespace(const struct Namespace &ns) const {
return Namespace(ns.components);
}
std::string NamespacedEnumVariant(const EnumDef &e, const EnumVal &v) const {
return NamespacedString(e.defined_namespace, EnumVariant(e, v));
}
std::string NamespacedType(const Definition &def) const {
return NamespacedString(def.defined_namespace, Type(def.name));
}
@@ -86,6 +102,11 @@ class IdlNamer : public Namer {
return EscapeKeyword(ConvertCase(name, Case::kLowerCamel));
}
std::string LegacyJavaMethod2(const std::string &prefix, const StructDef &sd,
const std::string &suffix) const {
return prefix + sd.name + suffix;
}
private:
std::string NamespacedString(const struct Namespace *ns,
const std::string &str) const {
@@ -111,4 +132,4 @@ inline Namer::Config WithFlagOptions(const Namer::Config &input,
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_NAMER
#endif // FLATBUFFERS_IDL_NAMER

View File

@@ -112,6 +112,11 @@ class Namer {
return Method(s.name);
}
virtual std::string Method(const std::string &pre,
const std::string &suf) const {
return Format(pre + "_" + suf, config_.methods);
}
virtual std::string Method(const std::string &s) const {
return Format(s, config_.methods);
}
@@ -128,6 +133,15 @@ class Namer {
return Format(s, config_.variables);
}
template<typename T>
std::string Variable(const std::string &p, const T &s) const {
return Format(p + "_" + s.name, config_.variables);
}
virtual std::string Variable(const std::string &p,
const std::string &s) const {
return Format(p + "_" + s, config_.variables);
}
virtual std::string Namespace(const std::string &s) const {
return Format(s, config_.namespaces);
}
@@ -191,6 +205,9 @@ class Namer {
virtual std::string Type(const std::string &s) const {
return Format(s, config_.types);
}
virtual std::string Type(const std::string &t, const std::string &s) const {
return Format(t + "_" + s, config_.types);
}
virtual std::string ObjectType(const std::string &s) const {
return config_.object_prefix + Type(s) + config_.object_suffix;

View File

@@ -616,7 +616,7 @@ public final class Monster extends Table {
if (_o.getVectorOfDoubles() != null) {
_vectorOfDoubles = createVectorOfDoublesVector(builder, _o.getVectorOfDoubles());
}
int _parent_namespace_test = _o.getParentNamespaceTest() == null ? 0 : MyGame.InParentNamespace.pack(builder, _o.getParentNamespaceTest());
int _parentNamespaceTest = _o.getParentNamespaceTest() == null ? 0 : MyGame.InParentNamespace.pack(builder, _o.getParentNamespaceTest());
int _vectorOfReferrables = 0;
if (_o.getVectorOfReferrables() != null) {
int[] __vectorOfReferrables = new int[_o.getVectorOfReferrables().length];
@@ -702,7 +702,7 @@ public final class Monster extends Table {
addTest5(builder, _test5);
addVectorOfLongs(builder, _vectorOfLongs);
addVectorOfDoubles(builder, _vectorOfDoubles);
addParentNamespaceTest(builder, _parent_namespace_test);
addParentNamespaceTest(builder, _parentNamespaceTest);
addVectorOfReferrables(builder, _vectorOfReferrables);
addSingleWeakReference(builder, _o.getSingleWeakReference());
addVectorOfWeakReferences(builder, _vectorOfWeakReferences);