From ee6a7a8e339cda5e9fe56878f06301a4bde4c7e6 Mon Sep 17 00:00:00 2001 From: Alex Panchenko Date: Mon, 8 Apr 2024 07:58:39 +0200 Subject: [PATCH 1/3] option '@generated=omit' --- 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 | 14 +++++++++++--- .../golden/TestDeprecatedService.java.txt | 3 --- .../src/testLite/golden/TestService.java.txt | 3 --- 6 files changed, 26 insertions(+), 24 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..e290bcd93a1 100644 --- a/compiler/src/java_plugin/cpp/java_plugin.cpp +++ b/compiler/src/java_plugin/cpp/java_plugin.cpp @@ -58,6 +58,8 @@ 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: @@ -73,9 +75,15 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator { } else if (options[i].first == "noversion") { disable_version = true; } else if (options[i].first == "jakarta_javax") { - jakarta_mode = "javax"; + generated_annotation = java_grpc_generator::GeneratedAnnotation::JAVAX; } else if (options[i].first == "jakarta_omit") { - jakarta_mode = "omit"; + generated_annotation = java_grpc_generator::GeneratedAnnotation::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 +96,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 { From 89f8bd867e627bff3234c68c182013c1c91a0922 Mon Sep 17 00:00:00 2001 From: Alex Panchenko Date: Mon, 8 Apr 2024 08:02:48 +0200 Subject: [PATCH 2/3] remove local var jakarta_mode --- compiler/src/java_plugin/cpp/java_plugin.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/src/java_plugin/cpp/java_plugin.cpp b/compiler/src/java_plugin/cpp/java_plugin.cpp index e290bcd93a1..eeb2ddebb1f 100644 --- a/compiler/src/java_plugin/cpp/java_plugin.cpp +++ b/compiler/src/java_plugin/cpp/java_plugin.cpp @@ -58,7 +58,7 @@ 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 generated_annotation = java_grpc_generator::GeneratedAnnotation::JAVAX; /* @@ -67,7 +67,6 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator { 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") { From 34b4a083d9c928b5ca77a6cf262219dcc1646da9 Mon Sep 17 00:00:00 2001 From: Alex Panchenko Date: Mon, 8 Apr 2024 08:14:29 +0200 Subject: [PATCH 3/3] remove old option and comments --- compiler/src/java_plugin/cpp/java_plugin.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/src/java_plugin/cpp/java_plugin.cpp b/compiler/src/java_plugin/cpp/java_plugin.cpp index eeb2ddebb1f..b1f407e2a3d 100644 --- a/compiler/src/java_plugin/cpp/java_plugin.cpp +++ b/compiler/src/java_plugin/cpp/java_plugin.cpp @@ -61,22 +61,12 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator { 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 - */ 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") { - generated_annotation = java_grpc_generator::GeneratedAnnotation::JAVAX; - } else if (options[i].first == "jakarta_omit") { - generated_annotation = java_grpc_generator::GeneratedAnnotation::OMIT; } else if (options[i].first == "@generated") { if (options[i].second == "omit") { generated_annotation = java_grpc_generator::GeneratedAnnotation::OMIT;