Skip to content

Commit

Permalink
implement ability to skip generation of javax annotation (#10927)
Browse files Browse the repository at this point in the history
* commit to implement feedback from #10786 to partially fix #9179
  • Loading branch information
alexanderankin authored Feb 17, 2024
1 parent 3994b15 commit 0d39c2c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
31 changes: 23 additions & 8 deletions compiler/src/java_plugin/cpp/java_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,12 +1128,26 @@ static void PrintService(const ServiceDescriptor* service,
#endif
// TODO(nmittler): Replace with WriteServiceDocComment once included by protobuf distro.
GrpcWriteServiceDocComment(p, service, NONE);
p->Print(
*vars,
"@$Generated$(\n"
" value = \"by gRPC proto compiler$grpc_version$\",\n"
" comments = \"Source: $file_name$\")\n"
"@$GrpcGenerated$\n");

if ((*vars)["JakartaMode"] == "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") {
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()) {
p->Print(*vars, "@$Deprecated$\n");
Expand Down Expand Up @@ -1217,7 +1231,8 @@ void PrintImports(Printer* p) {
void GenerateService(const ServiceDescriptor* service,
protobuf::io::ZeroCopyOutputStream* out,
ProtoFlavor flavor,
bool disable_version) {
bool disable_version,
std::string jakarta_mode) {
// All non-generated classes must be referred by fully qualified names to
// avoid collision with generated classes.
std::map<std::string, std::string> vars;
Expand Down Expand Up @@ -1249,7 +1264,7 @@ void GenerateService(const ServiceDescriptor* service,
vars["MethodDescriptor"] = "io.grpc.MethodDescriptor";
vars["StreamObserver"] = "io.grpc.stub.StreamObserver";
vars["Iterator"] = "java.util.Iterator";
vars["Generated"] = "javax.annotation.Generated";
vars["JakartaMode"] = jakarta_mode;
vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated";
vars["ListenableFuture"] =
"com.google.common.util.concurrent.ListenableFuture";
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/java_plugin/cpp/java_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ std::string ServiceClassName(const impl::protobuf::ServiceDescriptor* service);
void GenerateService(const impl::protobuf::ServiceDescriptor* service,
impl::protobuf::io::ZeroCopyOutputStream* out,
ProtoFlavor flavor,
bool disable_version);
bool disable_version,
std::string jakarta_mode);

} // namespace java_grpc_generator

Expand Down
13 changes: 12 additions & 1 deletion compiler/src/java_plugin/cpp/java_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,23 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
java_grpc_generator::ProtoFlavor flavor =
java_grpc_generator::ProtoFlavor::NORMAL;

/*
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";
}
}

Expand All @@ -77,7 +88,7 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
context->Open(filename));
java_grpc_generator::GenerateService(
service, output.get(), flavor, disable_version);
service, output.get(), flavor, disable_version, jakarta_mode);
}
return true;
}
Expand Down

0 comments on commit 0d39c2c

Please sign in to comment.