From 5993e86986ab5368c6b84d1aeb276bc34a48852e Mon Sep 17 00:00:00 2001 From: Sandy Zhang Date: Tue, 27 Feb 2024 13:37:44 -0800 Subject: [PATCH] Force descriptor initialization of dependencies at the beginning of the static init block. This ensures that any dependent descriptors are already initialized before we use them and we're not initializing them lazily as-we-go. This should help ensure that dependent descriptors are fully initialized before we need them. PiperOrigin-RevId: 610858264 --- src/google/protobuf/compiler/java/file.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index 03da25a895..848ee90ce0 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -402,6 +402,16 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( "final", options_.opensource_runtime ? "" : "final"); printer->Indent(); + // Force descriptor initialization of all dependencies. + for (int i = 0; i < file_->dependency_count(); i++) { + if (ShouldIncludeDependency(file_->dependency(i), true)) { + std::string dependency = + name_resolver_->GetImmutableClassName(file_->dependency(i)); + printer->Print("$dependency$.getDescriptor();\n", "dependency", + dependency); + } + } + if (options_.opensource_runtime) { SharedCodeGenerator shared_code_generator(file_, options_); shared_code_generator.GenerateDescriptors(printer); @@ -461,16 +471,6 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable( }); } - // Force descriptor initialization of all dependencies. - for (int i = 0; i < file_->dependency_count(); i++) { - if (ShouldIncludeDependency(file_->dependency(i), true)) { - std::string dependency = - name_resolver_->GetImmutableClassName(file_->dependency(i)); - printer->Print("$dependency$.getDescriptor();\n", "dependency", - dependency); - } - } - if (!extensions.empty()) { // Must construct an ExtensionRegistry containing all existing extensions // and use it to parse the descriptor data again to recognize extensions.