From 2c28d728da1ef044776349aaaf59d83fc3afa5f1 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 18 Jan 2024 08:29:34 -0800 Subject: [PATCH] Add missing Protobuf Java version checks for enums and missing version info when separate Java files are generated. PiperOrigin-RevId: 599515908 --- src/google/protobuf/compiler/java/enum.cc | 6 ++++++ src/google/protobuf/compiler/java/file.cc | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/google/protobuf/compiler/java/enum.cc b/src/google/protobuf/compiler/java/enum.cc index 54ae6ff88c..96dc02f091 100644 --- a/src/google/protobuf/compiler/java/enum.cc +++ b/src/google/protobuf/compiler/java/enum.cc @@ -111,6 +111,12 @@ void EnumGenerator::Generate(io::Printer* printer) { // ----------------------------------------------------------------- + printer->Print("static {\n"); + printer->Indent(); + PrintGencodeVersionValidator(printer, context_->options().opensource_runtime); + printer->Outdent(); + printer->Print("}\n"); + for (int i = 0; i < aliases_.size(); i++) { absl::flat_hash_map vars; vars["classname"] = descriptor_->name(); diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index 4fbaa2bee4..49d1438e8a 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -500,7 +500,7 @@ static void GenerateSibling( const DescriptorClass* descriptor, GeneratorContext* context, std::vector* file_list, bool annotate_code, std::vector* annotation_list, const std::string& name_suffix, - GeneratorClass* generator, + GeneratorClass* generator, bool opensource_runtime, void (GeneratorClass::*pfn)(io::Printer* printer)) { std::string filename = absl::StrCat(package_dir, descriptor->name(), name_suffix, ".java"); @@ -519,8 +519,10 @@ static void GenerateSibling( "// source: $filename$\n" "\n", "filename", descriptor->file()->name()); - printer.Print("// Protobuf Java Version: $protobuf_java_version$\n", - "protobuf_java_version", PROTOBUF_JAVA_VERSION_STRING); + if (opensource_runtime) { + printer.Print("// Protobuf Java Version: $protobuf_java_version$\n", + "protobuf_java_version", PROTOBUF_JAVA_VERSION_STRING); + } if (!java_package.empty()) { printer.Print( "package $package$;\n" @@ -550,14 +552,14 @@ void FileGenerator::GenerateSiblings( GenerateSibling( package_dir, java_package_, file_->enum_type(i), context, file_list, options_.annotate_code, annotation_list, "", &generator, - &EnumGenerator::Generate); + options_.opensource_runtime, &EnumGenerator::Generate); } else { EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, context_.get()); GenerateSibling( package_dir, java_package_, file_->enum_type(i), context, file_list, options_.annotate_code, annotation_list, "", &generator, - &EnumLiteGenerator::Generate); + options_.opensource_runtime, &EnumLiteGenerator::Generate); } } for (int i = 0; i < file_->message_type_count(); i++) { @@ -565,12 +567,14 @@ void FileGenerator::GenerateSiblings( GenerateSibling( package_dir, java_package_, file_->message_type(i), context, file_list, options_.annotate_code, annotation_list, "OrBuilder", - message_generators_[i].get(), &MessageGenerator::GenerateInterface); + message_generators_[i].get(), options_.opensource_runtime, + &MessageGenerator::GenerateInterface); } GenerateSibling( package_dir, java_package_, file_->message_type(i), context, file_list, options_.annotate_code, annotation_list, "", - message_generators_[i].get(), &MessageGenerator::Generate); + message_generators_[i].get(), options_.opensource_runtime, + &MessageGenerator::Generate); } if (HasGenericServices(file_, context_->EnforceLite())) { for (int i = 0; i < file_->service_count(); i++) { @@ -579,7 +583,7 @@ void FileGenerator::GenerateSiblings( GenerateSibling( package_dir, java_package_, file_->service(i), context, file_list, options_.annotate_code, annotation_list, "", generator.get(), - &ServiceGenerator::Generate); + options_.opensource_runtime, &ServiceGenerator::Generate); } } }