Skip to content

Commit 0d39c2c

Browse files
implement ability to skip generation of javax annotation (#10927)
* commit to implement feedback from #10786 to partially fix #9179
1 parent 3994b15 commit 0d39c2c

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

compiler/src/java_plugin/cpp/java_generator.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -1128,12 +1128,26 @@ static void PrintService(const ServiceDescriptor* service,
11281128
#endif
11291129
// TODO(nmittler): Replace with WriteServiceDocComment once included by protobuf distro.
11301130
GrpcWriteServiceDocComment(p, service, NONE);
1131-
p->Print(
1132-
*vars,
1133-
"@$Generated$(\n"
1134-
" value = \"by gRPC proto compiler$grpc_version$\",\n"
1135-
" comments = \"Source: $file_name$\")\n"
1136-
"@$GrpcGenerated$\n");
1131+
1132+
if ((*vars)["JakartaMode"] == "javax") {
1133+
p->Print(
1134+
*vars,
1135+
"@javax.annotation.Generated(\n"
1136+
" value = \"by gRPC proto compiler$grpc_version$\",\n"
1137+
" comments = \"Source: $file_name$\")\n"
1138+
"@$GrpcGenerated$\n");
1139+
} else if ((*vars)["JakartaMode"] == "omit") {
1140+
p->Print(
1141+
*vars,
1142+
"@$GrpcGenerated$\n");
1143+
} else {
1144+
p->Print(
1145+
*vars,
1146+
"@javax.annotation.Generated(\n"
1147+
" value = \"by gRPC proto compiler$grpc_version$\",\n"
1148+
" comments = \"Source: $file_name$\")\n"
1149+
"@$GrpcGenerated$\n");
1150+
}
11371151

11381152
if (service->options().deprecated()) {
11391153
p->Print(*vars, "@$Deprecated$\n");
@@ -1217,7 +1231,8 @@ void PrintImports(Printer* p) {
12171231
void GenerateService(const ServiceDescriptor* service,
12181232
protobuf::io::ZeroCopyOutputStream* out,
12191233
ProtoFlavor flavor,
1220-
bool disable_version) {
1234+
bool disable_version,
1235+
std::string jakarta_mode) {
12211236
// All non-generated classes must be referred by fully qualified names to
12221237
// avoid collision with generated classes.
12231238
std::map<std::string, std::string> vars;
@@ -1249,7 +1264,7 @@ void GenerateService(const ServiceDescriptor* service,
12491264
vars["MethodDescriptor"] = "io.grpc.MethodDescriptor";
12501265
vars["StreamObserver"] = "io.grpc.stub.StreamObserver";
12511266
vars["Iterator"] = "java.util.Iterator";
1252-
vars["Generated"] = "javax.annotation.Generated";
1267+
vars["JakartaMode"] = jakarta_mode;
12531268
vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated";
12541269
vars["ListenableFuture"] =
12551270
"com.google.common.util.concurrent.ListenableFuture";

compiler/src/java_plugin/cpp/java_generator.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ std::string ServiceClassName(const impl::protobuf::ServiceDescriptor* service);
6868
void GenerateService(const impl::protobuf::ServiceDescriptor* service,
6969
impl::protobuf::io::ZeroCopyOutputStream* out,
7070
ProtoFlavor flavor,
71-
bool disable_version);
71+
bool disable_version,
72+
std::string jakarta_mode);
7273

7374
} // namespace java_grpc_generator
7475

compiler/src/java_plugin/cpp/java_plugin.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
5959
java_grpc_generator::ProtoFlavor flavor =
6060
java_grpc_generator::ProtoFlavor::NORMAL;
6161

62+
/*
63+
jakarta_mode has these values:
64+
javax, the original behavior - add @javax.annotation.Generated
65+
omit, "less controversial" = just add @io.grpc.stub.annotations.GrpcGenerated
66+
and maybe others in the future
67+
*/
68+
std::string jakarta_mode;
6269
bool disable_version = false;
6370
for (size_t i = 0; i < options.size(); i++) {
6471
if (options[i].first == "lite") {
6572
flavor = java_grpc_generator::ProtoFlavor::LITE;
6673
} else if (options[i].first == "noversion") {
6774
disable_version = true;
75+
} else if (options[i].first == "jakarta_javax") {
76+
jakarta_mode = "javax";
77+
} else if (options[i].first == "jakarta_omit") {
78+
jakarta_mode = "omit";
6879
}
6980
}
7081

@@ -77,7 +88,7 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
7788
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
7889
context->Open(filename));
7990
java_grpc_generator::GenerateService(
80-
service, output.get(), flavor, disable_version);
91+
service, output.get(), flavor, disable_version, jakarta_mode);
8192
}
8293
return true;
8394
}

0 commit comments

Comments
 (0)