From 9dfe7e9d8b7dab86d487968b15426f4e3f17bafa Mon Sep 17 00:00:00 2001 From: Deanna Garcia Date: Tue, 8 Nov 2022 09:14:49 -0800 Subject: [PATCH] Add template comment and package to previously empty generated kotlin proto files. We generate a Kotlin file to correspond to each message, which left the per-file generated file blank. We would still like to generate a file to deal with edge cases where there are no messages defined in a proto file. To fix, we print the default comment about the file being generated as well as a package statement. PiperOrigin-RevId: 486969373 --- src/google/protobuf/compiler/java/file.cc | 17 +++++++++++++++++ src/google/protobuf/compiler/java/file.h | 1 + .../protobuf/compiler/java/kotlin_generator.cc | 2 ++ 3 files changed, 20 insertions(+) diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index 0f61f3185aca..6f61be85e892 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -689,6 +689,23 @@ std::string FileGenerator::GetKotlinClassname() { return name_resolver_->GetFileClassName(file_, immutable_api_, true); } +void FileGenerator::GenerateKotlin(io::Printer* printer) { + printer->Print( + "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" + "// source: $filename$\n" + "\n", + "filename", file_->name()); + printer->Print( + "// Generated files should ignore deprecation warnings\n" + "@file:Suppress(\"DEPRECATION\")"); + if (!java_package_.empty()) { + printer->Print( + "package $package$;\n" + "\n", + "package", EscapeKotlinKeywords(java_package_)); + } +} + void FileGenerator::GenerateKotlinSiblings( const std::string& package_dir, GeneratorContext* context, std::vector* file_list, diff --git a/src/google/protobuf/compiler/java/file.h b/src/google/protobuf/compiler/java/file.h index bf1e23ba1f2e..862242f44c81 100644 --- a/src/google/protobuf/compiler/java/file.h +++ b/src/google/protobuf/compiler/java/file.h @@ -82,6 +82,7 @@ class FileGenerator { void Generate(io::Printer* printer); std::string GetKotlinClassname(); + void GenerateKotlin(io::Printer* printer); void GenerateKotlinSiblings(const std::string& package_dir, GeneratorContext* generator_context, std::vector* file_list, diff --git a/src/google/protobuf/compiler/java/kotlin_generator.cc b/src/google/protobuf/compiler/java/kotlin_generator.cc index 3470ccbb2f4a..685b1e8cf092 100644 --- a/src/google/protobuf/compiler/java/kotlin_generator.cc +++ b/src/google/protobuf/compiler/java/kotlin_generator.cc @@ -119,6 +119,8 @@ bool KotlinGenerator::Generate(const FileDescriptor* file, output.get(), '$', file_options.annotate_code ? &annotation_collector : nullptr); + file_generator->GenerateKotlin(&printer); + file_generator->GenerateKotlinSiblings(package_dir, context, &all_files, &all_annotations);