From 8a21afcc9e07ec1c50408c17c2c1415976ccbc71 Mon Sep 17 00:00:00 2001 From: Alex Panchenko Date: Fri, 19 Apr 2024 03:34:04 +0200 Subject: [PATCH] compiler: add option `@generated=omit` (#11086) related to #11081 --- compiler/build.gradle | 5 ++++- .../src/java_plugin/cpp/java_generator.cpp | 19 ++++++----------- compiler/src/java_plugin/cpp/java_generator.h | 6 +++++- compiler/src/java_plugin/cpp/java_plugin.cpp | 21 ++++++++----------- .../golden/TestDeprecatedService.java.txt | 3 --- .../src/testLite/golden/TestService.java.txt | 3 --- 6 files changed, 24 insertions(+), 33 deletions(-) diff --git a/compiler/build.gradle b/compiler/build.gradle index 8bed90b8678..d58d800c478 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -192,7 +192,10 @@ protobuf { java { option 'lite' } } plugins { - grpc { option 'lite' } + grpc { + option 'lite' + option '@generated=omit' + } } } } diff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp index 00855df3d04..8693fad1b66 100644 --- a/compiler/src/java_plugin/cpp/java_generator.cpp +++ b/compiler/src/java_plugin/cpp/java_generator.cpp @@ -1116,7 +1116,8 @@ static void PrintService(const ServiceDescriptor* service, std::map* vars, Printer* p, ProtoFlavor flavor, - bool disable_version) { + bool disable_version, + GeneratedAnnotation generated_annotation) { (*vars)["service_name"] = service->name(); (*vars)["file_name"] = service->file()->name(); (*vars)["service_class_name"] = ServiceClassName(service); @@ -1129,24 +1130,17 @@ static void PrintService(const ServiceDescriptor* service, // TODO(nmittler): Replace with WriteServiceDocComment once included by protobuf distro. GrpcWriteServiceDocComment(p, service, NONE); - if ((*vars)["JakartaMode"] == "javax") { + if (generated_annotation == GeneratedAnnotation::JAVAX) { p->Print( *vars, "@javax.annotation.Generated(\n" " value = \"by gRPC proto compiler$grpc_version$\",\n" " comments = \"Source: $file_name$\")\n" "@$GrpcGenerated$\n"); - } else if ((*vars)["JakartaMode"] == "omit") { + } else { // GeneratedAnnotation::OMIT p->Print( *vars, "@$GrpcGenerated$\n"); - } else { - p->Print( - *vars, - "@javax.annotation.Generated(\n" - " value = \"by gRPC proto compiler$grpc_version$\",\n" - " comments = \"Source: $file_name$\")\n" - "@$GrpcGenerated$\n"); } if (service->options().deprecated()) { @@ -1232,7 +1226,7 @@ void GenerateService(const ServiceDescriptor* service, protobuf::io::ZeroCopyOutputStream* out, ProtoFlavor flavor, bool disable_version, - std::string jakarta_mode) { + GeneratedAnnotation generated_annotation) { // All non-generated classes must be referred by fully qualified names to // avoid collision with generated classes. std::map vars; @@ -1264,7 +1258,6 @@ void GenerateService(const ServiceDescriptor* service, vars["MethodDescriptor"] = "io.grpc.MethodDescriptor"; vars["StreamObserver"] = "io.grpc.stub.StreamObserver"; vars["Iterator"] = "java.util.Iterator"; - vars["JakartaMode"] = jakarta_mode; vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated"; vars["ListenableFuture"] = "com.google.common.util.concurrent.ListenableFuture"; @@ -1283,7 +1276,7 @@ void GenerateService(const ServiceDescriptor* service, if (!vars["Package"].empty()) { vars["Package"].append("."); } - PrintService(service, &vars, &printer, flavor, disable_version); + PrintService(service, &vars, &printer, flavor, disable_version, generated_annotation); } std::string ServiceJavaPackage(const FileDescriptor* file) { diff --git a/compiler/src/java_plugin/cpp/java_generator.h b/compiler/src/java_plugin/cpp/java_generator.h index d30179d334e..857fcab31d0 100644 --- a/compiler/src/java_plugin/cpp/java_generator.h +++ b/compiler/src/java_plugin/cpp/java_generator.h @@ -57,6 +57,10 @@ enum ProtoFlavor { NORMAL, LITE }; +enum GeneratedAnnotation { + OMIT, JAVAX +}; + // Returns the package name of the gRPC services defined in the given file. std::string ServiceJavaPackage(const impl::protobuf::FileDescriptor* file); @@ -69,7 +73,7 @@ void GenerateService(const impl::protobuf::ServiceDescriptor* service, impl::protobuf::io::ZeroCopyOutputStream* out, ProtoFlavor flavor, bool disable_version, - std::string jakarta_mode); + GeneratedAnnotation generated_annotation); } // namespace java_grpc_generator diff --git a/compiler/src/java_plugin/cpp/java_plugin.cpp b/compiler/src/java_plugin/cpp/java_plugin.cpp index 36f22893f63..b1f407e2a3d 100644 --- a/compiler/src/java_plugin/cpp/java_plugin.cpp +++ b/compiler/src/java_plugin/cpp/java_plugin.cpp @@ -58,24 +58,21 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator { java_grpc_generator::ProtoFlavor flavor = java_grpc_generator::ProtoFlavor::NORMAL; + java_grpc_generator::GeneratedAnnotation generated_annotation = + java_grpc_generator::GeneratedAnnotation::JAVAX; - /* - jakarta_mode has these values: - javax, the original behavior - add @javax.annotation.Generated - omit, "less controversial" = just add @io.grpc.stub.annotations.GrpcGenerated - and maybe others in the future - */ - std::string jakarta_mode; bool disable_version = false; for (size_t i = 0; i < options.size(); i++) { if (options[i].first == "lite") { flavor = java_grpc_generator::ProtoFlavor::LITE; } else if (options[i].first == "noversion") { disable_version = true; - } else if (options[i].first == "jakarta_javax") { - jakarta_mode = "javax"; - } else if (options[i].first == "jakarta_omit") { - jakarta_mode = "omit"; + } else if (options[i].first == "@generated") { + if (options[i].second == "omit") { + generated_annotation = java_grpc_generator::GeneratedAnnotation::OMIT; + } else if (options[i].second == "javax") { + generated_annotation = java_grpc_generator::GeneratedAnnotation::JAVAX; + } } } @@ -88,7 +85,7 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator { std::unique_ptr output( context->Open(filename)); java_grpc_generator::GenerateService( - service, output.get(), flavor, disable_version, jakarta_mode); + service, output.get(), flavor, disable_version, generated_annotation); } return true; } diff --git a/compiler/src/testLite/golden/TestDeprecatedService.java.txt b/compiler/src/testLite/golden/TestDeprecatedService.java.txt index 34b672e8319..3a7dba9bbb5 100644 --- a/compiler/src/testLite/golden/TestDeprecatedService.java.txt +++ b/compiler/src/testLite/golden/TestDeprecatedService.java.txt @@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName; * Test service that has been deprecated and should generate with Java's @Deprecated annotation * */ -@javax.annotation.Generated( - value = "by gRPC proto compiler (version 1.64.0-SNAPSHOT)", - comments = "Source: grpc/testing/compiler/test.proto") @io.grpc.stub.annotations.GrpcGenerated @java.lang.Deprecated public final class TestDeprecatedServiceGrpc { diff --git a/compiler/src/testLite/golden/TestService.java.txt b/compiler/src/testLite/golden/TestService.java.txt index 4d8eddf87c4..f86fb50d7dc 100644 --- a/compiler/src/testLite/golden/TestService.java.txt +++ b/compiler/src/testLite/golden/TestService.java.txt @@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName; * Test service that supports all call types. * */ -@javax.annotation.Generated( - value = "by gRPC proto compiler (version 1.64.0-SNAPSHOT)", - comments = "Source: grpc/testing/compiler/test.proto") @io.grpc.stub.annotations.GrpcGenerated public final class TestServiceGrpc {