From 98d5bdd1117c0bc1541cf6d5de7b28d00a333c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Tue, 7 May 2024 17:20:54 -0700 Subject: [PATCH] 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 --- src/google/protobuf/compiler/java/names.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/google/protobuf/compiler/java/names.cc b/src/google/protobuf/compiler/java/names.cc index 2c47db1942..ae18ee2e4e 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({ - "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); }