Add "reserved literals" to the list of reserved names for Java.

The identifiers `true`, `false`, and `null` are effectively reserved words in Java, although for some reason they are listed separately from the "keywords" in the Java Language Specification.

This doesn't matter for regular fields, because a proto field called `true` will be accessed with `getTrue` and `setTrue`. But for extensions, the generated Java code will have a public static field whose name is the same as the name of the extension field, with `_` appended if the name is a reserved word. Previously there was no `_` for `true` etc, so the generated code would not compile.

PiperOrigin-RevId: 631599695
pull/16776/head
Éamonn McManus 7 months ago committed by Copybara-Service
parent 5632d8e616
commit 98d5bdd111
  1. 21
      src/google/protobuf/compiler/java/names.cc

@ -39,16 +39,17 @@ const char* DefaultPackage(Options options) {
bool IsReservedName(absl::string_view name) { bool IsReservedName(absl::string_view name) {
static const auto& kReservedNames = static const auto& kReservedNames =
*new absl::flat_hash_set<absl::string_view>({ *new absl::flat_hash_set<absl::string_view>({
"abstract", "assert", "boolean", "break", "byte", "abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float", "enum", "extends", "false", "final", "finally",
"for", "goto", "if", "implements", "import", "float", "for", "goto", "if", "implements",
"instanceof", "int", "interface", "long", "native", "import", "instanceof", "int", "interface", "long",
"new", "package", "private", "protected", "public", "native", "new", "package", "private", "protected",
"return", "short", "static", "strictfp", "super", "public", "return", "short", "static", "strictfp",
"switch", "synchronized", "this", "throw", "throws", "super", "switch", "synchronized", "this", "throw",
"transient", "try", "void", "volatile", "while", "throws", "transient", "true", "try", "void",
"volatile", "while",
}); });
return kReservedNames.contains(name); return kReservedNames.contains(name);
} }

Loading…
Cancel
Save