aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSandy Zhang <sandyzhang@google.com>2024-05-10 08:48:01 -0700
committerCopybara-Service <copybara-worker@google.com>2024-05-10 08:49:22 -0700
commitc99cf4bbfaf71f735cd0dea7d6fc0b2c1a589ed2 (patch)
tree6fb4527b1a8e808b28ccbd6dbbb54b966df37b21
parent91b7cf3325d6832409f17be86bb2ce282af95293 (diff)
downloadprotobuf-c99cf4bbfaf71f735cd0dea7d6fc0b2c1a589ed2.tar.gz
Add `java` to reserved names to escape extensions to `java_`. This may break existing references to gencode for extensions named `java`.
This prevents shadowing of `java.lang` package commonly used in protobuf gencode. Existing extensions named `java` may or may not previously fail to compile depending on if the contents of their .proto result in gencode using `java.lang`. This is needed to fix `java_features.proto` lite gencode since enum gencode uses `java.lang`. Fields named `java` should already be escaped. *Warning: This may break user code for existing protos with extensions named `java`. References to the extension should be renamed to use `java_` e.g. registry.add(GeneratedClassName.java_)* PiperOrigin-RevId: 632508249
-rw-r--r--editions/golden/editions_transform_proto2.proto2
-rw-r--r--java/core/BUILD.bazel1
-rw-r--r--java/core/src/main/java/com/google/protobuf/Descriptors.java8
-rw-r--r--java/core/src/test/java/com/google/protobuf/DescriptorsTest.java10
-rw-r--r--java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto5
-rw-r--r--objectivec/Tests/unittest.proto4
-rw-r--r--src/google/protobuf/compiler/java/names.cc22
-rw-r--r--src/google/protobuf/edition_unittest.proto4
-rw-r--r--src/google/protobuf/unittest.proto4
9 files changed, 32 insertions, 28 deletions
diff --git a/editions/golden/editions_transform_proto2.proto b/editions/golden/editions_transform_proto2.proto
index 260a8d26a..84807b6d5 100644
--- a/editions/golden/editions_transform_proto2.proto
+++ b/editions/golden/editions_transform_proto2.proto
@@ -15,7 +15,7 @@ edition = "2023";
package protobuf_editions_test;
import "net/proto/proto1_features.proto";
-import "third_party/java/protobuf/java_features.proto";
+import "google/protobuf/java_features.proto";
import "google/protobuf/cpp_features.proto";
import "google/protobuf/editions/proto/editions_transform_proto3.proto";
diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel
index 6127b4c5c..41802133e 100644
--- a/java/core/BUILD.bazel
+++ b/java/core/BUILD.bazel
@@ -336,6 +336,7 @@ proto_library(
deps = [
"//:any_proto",
"//:descriptor_proto",
+ "//:java_features_proto",
"//:lite_test_protos",
"//:wrappers_proto",
"//src/google/protobuf:generic_test_protos",
diff --git a/java/core/src/main/java/com/google/protobuf/Descriptors.java b/java/core/src/main/java/com/google/protobuf/Descriptors.java
index f4aa9ca3b..bba317861 100644
--- a/java/core/src/main/java/com/google/protobuf/Descriptors.java
+++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java
@@ -94,7 +94,7 @@ public final class Descriptors {
if (javaEditionDefaults == null) {
try {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
- registry.add(JavaFeaturesProto.java);
+ registry.add(JavaFeaturesProto.java_);
setTestJavaEditionDefaults(
FeatureSetDefaults.parseFrom(
JavaEditionDefaults.PROTOBUF_INTERNAL_JAVA_EDITION_DEFAULTS.getBytes(
@@ -679,7 +679,7 @@ public final class Descriptors {
if (getEdition() == Edition.EDITION_PROTO2) {
if (proto.getOptions().getJavaStringCheckUtf8()) {
features.setExtension(
- JavaFeaturesProto.java,
+ JavaFeaturesProto.java_,
JavaFeatures.newBuilder()
.setUtf8Validation(JavaFeatures.Utf8Validation.VERIFY)
.build());
@@ -1320,7 +1320,7 @@ public final class Descriptors {
return true;
}
if (this.features
- .getExtension(JavaFeaturesProto.java)
+ .getExtension(JavaFeaturesProto.java_)
.getUtf8Validation()
.equals(JavaFeatures.Utf8Validation.VERIFY)) {
return true;
@@ -1577,7 +1577,7 @@ public final class Descriptors {
}
return getType() == Type.ENUM
- && (this.features.getExtension(JavaFeaturesProto.java).getLegacyClosedEnum()
+ && (this.features.getExtension(JavaFeaturesProto.java_).getLegacyClosedEnum()
|| enumType.isClosed());
}
diff --git a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
index d64186ad7..2ebdf71e7 100644
--- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
+++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
@@ -1158,7 +1158,7 @@ public class DescriptorsTest {
.setOptions(FileOptions.newBuilder().setJavaStringCheckUtf8(true))
.build(),
new FileDescriptor[0]);
- assertThat(file.features.getExtension(JavaFeaturesProto.java).getUtf8Validation())
+ assertThat(file.features.getExtension(JavaFeaturesProto.java_).getUtf8Validation())
.isEqualTo(JavaFeaturesProto.JavaFeatures.Utf8Validation.VERIFY);
}
@@ -1178,8 +1178,8 @@ public class DescriptorsTest {
assertThat(features.getJsonFormat())
.isEqualTo(DescriptorProtos.FeatureSet.JsonFormat.LEGACY_BEST_EFFORT);
- assertThat(features.getExtension(JavaFeaturesProto.java).getLegacyClosedEnum()).isTrue();
- assertThat(features.getExtension(JavaFeaturesProto.java).getUtf8Validation())
+ assertThat(features.getExtension(JavaFeaturesProto.java_).getLegacyClosedEnum()).isTrue();
+ assertThat(features.getExtension(JavaFeaturesProto.java_).getUtf8Validation())
.isEqualTo(JavaFeaturesProto.JavaFeatures.Utf8Validation.DEFAULT);
}
@@ -1198,8 +1198,8 @@ public class DescriptorsTest {
assertThat(features.getMessageEncoding())
.isEqualTo(DescriptorProtos.FeatureSet.MessageEncoding.LENGTH_PREFIXED);
- assertThat(features.getExtension(JavaFeaturesProto.java).getLegacyClosedEnum()).isFalse();
- assertThat(features.getExtension(JavaFeaturesProto.java).getUtf8Validation())
+ assertThat(features.getExtension(JavaFeaturesProto.java_).getLegacyClosedEnum()).isFalse();
+ assertThat(features.getExtension(JavaFeaturesProto.java_).getUtf8Validation())
.isEqualTo(JavaFeaturesProto.JavaFeatures.Utf8Validation.DEFAULT);
}
diff --git a/java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto b/java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto
index 3af9831d2..fff2805e7 100644
--- a/java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto
+++ b/java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto
@@ -12,7 +12,10 @@ edition = "2023";
package proto2_test_check_utf8;
-option features.utf8_validation = VERIFY;
+import "google/protobuf/java_features.proto";
+
+option features.utf8_validation = NONE;
+option features.(pb.java).utf8_validation = VERIFY;
option java_outer_classname = "TestCheckUtf8";
message StringWrapper {
diff --git a/objectivec/Tests/unittest.proto b/objectivec/Tests/unittest.proto
index 2239fe692..dc9546db7 100644
--- a/objectivec/Tests/unittest.proto
+++ b/objectivec/Tests/unittest.proto
@@ -1109,11 +1109,11 @@ message TestNestedGroupExtensionOuter {
repeated group Layer2RepeatedGroup = 2 {
extensions 3
// NOTE: extension metadata is not supported due to targets such as
- // `//third_party/protobuf_legacy_opensource/src:shell_scripts_test`,
+ // `//google/protobuf_legacy_opensource/src:shell_scripts_test`,
// eee https://screenshot.googleplex.com/Axz2QD8nxjdpyFF
//[metadata = {
// NOTE: can't write type there due to some clever build gen code at
- // http://google3/net/proto2/internal/BUILD;l=1247;rcl=411090862
+ // http://google3/google/protobuf/BUILD;l=1247;rcl=411090862
// type: "objc.protobuf.tests.TestNestedGroupExtensionInnerExtension",
// name: "inner",
// }]
diff --git a/src/google/protobuf/compiler/java/names.cc b/src/google/protobuf/compiler/java/names.cc
index 6df146353..e1381c78c 100644
--- a/src/google/protobuf/compiler/java/names.cc
+++ b/src/google/protobuf/compiler/java/names.cc
@@ -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", "null", "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", "java",
+ "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);
}
diff --git a/src/google/protobuf/edition_unittest.proto b/src/google/protobuf/edition_unittest.proto
index fadde47d2..81848b87e 100644
--- a/src/google/protobuf/edition_unittest.proto
+++ b/src/google/protobuf/edition_unittest.proto
@@ -1254,11 +1254,11 @@ message TestNestedGroupExtensionOuter {
message Layer2RepeatedGroup {
extensions 3
// NOTE: extension metadata is not supported due to targets such as
- // `//third_party/protobuf_legacy_opensource/src:shell_scripts_test`,
+ // `//google/protobuf_legacy_opensource/src:shell_scripts_test`,
// eee https://screenshot.googleplex.com/Axz2QD8nxjdpyFF
//[metadata = {
// NOTE: can't write type there due to some clever build gen code at
- // http://google3/net/proto2/internal/BUILD;l=1247;rcl=411090862
+ // http://google3/google/protobuf/BUILD;l=1247;rcl=411090862
// type: "edition_unittest.TestNestedGroupExtensionInnerExtension",
// name: "inner",
// }]
diff --git a/src/google/protobuf/unittest.proto b/src/google/protobuf/unittest.proto
index 3d9dc3a77..7c65fb594 100644
--- a/src/google/protobuf/unittest.proto
+++ b/src/google/protobuf/unittest.proto
@@ -1226,11 +1226,11 @@ message TestNestedGroupExtensionOuter {
repeated group Layer2RepeatedGroup = 2 {
extensions 3
// NOTE: extension metadata is not supported due to targets such as
- // `//third_party/protobuf_legacy_opensource/src:shell_scripts_test`,
+ // `//google/protobuf_legacy_opensource/src:shell_scripts_test`,
// eee https://screenshot.googleplex.com/Axz2QD8nxjdpyFF
//[metadata = {
// NOTE: can't write type there due to some clever build gen code at
- // http://google3/net/proto2/internal/BUILD;l=1247;rcl=411090862
+ // http://google3/google/protobuf/BUILD;l=1247;rcl=411090862
// type: "protobuf_unittest.TestNestedGroupExtensionInnerExtension",
// name: "inner",
// }]