Skip to content

Commit

Permalink
Add null to the list of reserved words.
Browse files Browse the repository at this point in the history
The previous change claimed to do this in addition to `true` and `false`, but it was not actually in the code.

To reiterate the text from the earlier change, now actually reflected entirely in the code:

> 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.

This change should not affect any existing client code in Java. If someone had tried to use an extension called `true` (etc), they would have found that the generated proto code did not compile. Now it is possible to reference such an extension as `true_`.

PiperOrigin-RevId: 632174190
  • Loading branch information
eamonnmcmanus authored and copybara-github committed May 9, 2024
1 parent dbc2baf commit 510c143
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/google/protobuf/compiler/java/names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +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", "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",
"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", "null", "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 510c143

Please sign in to comment.