Skip to content

Commit

Permalink
Add "reserved literals" to the list of reserved names for Java.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eamonnmcmanus authored and Copybara-Service committed May 8, 2024
1 parent 5632d8e commit 98d5bdd
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/google/protobuf/compiler/java/names.cc
Expand Up @@ -39,16 +39,17 @@ const char* DefaultPackage(Options options) {
bool IsReservedName(absl::string_view name) {
static const auto& kReservedNames =
*new absl::flat_hash_set<absl::string_view>({
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while",
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "false", "final", "finally",
"float", "for", "goto", "if", "implements",
"import", "instanceof", "int", "interface", "long",
"native", "new", "package", "private", "protected",
"public", "return", "short", "static", "strictfp",
"super", "switch", "synchronized", "this", "throw",
"throws", "transient", "true", "try", "void",
"volatile", "while",
});
return kReservedNames.contains(name);
}
Expand Down

0 comments on commit 98d5bdd

Please sign in to comment.