aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2024-05-07 17:20:54 -0700
committerCopybara-Service <copybara-worker@google.com>2024-05-07 17:23:09 -0700
commit98d5bdd1117c0bc1541cf6d5de7b28d00a333c4f (patch)
treeca7e73802985fd77598eb22edfb8382ddd5f56e0
parent5632d8e616bb96cb783868136e3f5f6e352ce8f0 (diff)
downloadprotobuf-98d5bdd1117c0bc1541cf6d5de7b28d00a333c4f.tar.gz
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
-rw-r--r--src/google/protobuf/compiler/java/names.cc21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/google/protobuf/compiler/java/names.cc b/src/google/protobuf/compiler/java/names.cc
index 2c47db194..ae18ee2e4 100644
--- a/src/google/protobuf/compiler/java/names.cc
+++ b/src/google/protobuf/compiler/java/names.cc
@@ -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);
}