Skip to content

Commit

Permalink
compiler: add option @generated=omit (#11086)
Browse files Browse the repository at this point in the history
related to #11081
  • Loading branch information
panchenko committed Apr 19, 2024
1 parent 52e65ec commit 8a21afc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
5 changes: 4 additions & 1 deletion compiler/build.gradle
Expand Up @@ -192,7 +192,10 @@ protobuf {
java { option 'lite' }
}
plugins {
grpc { option 'lite' }
grpc {
option 'lite'
option '@generated=omit'
}
}
}
}
Expand Down
19 changes: 6 additions & 13 deletions compiler/src/java_plugin/cpp/java_generator.cpp
Expand Up @@ -1116,7 +1116,8 @@ static void PrintService(const ServiceDescriptor* service,
std::map<std::string, std::string>* 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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -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<std::string, std::string> vars;
Expand Down Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/java_plugin/cpp/java_generator.h
Expand Up @@ -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);

Expand All @@ -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

Expand Down
21 changes: 9 additions & 12 deletions compiler/src/java_plugin/cpp/java_plugin.cpp
Expand Up @@ -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;
}
}
}

Expand All @@ -88,7 +85,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, jakarta_mode);
service, output.get(), flavor, disable_version, generated_annotation);
}
return true;
}
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/testLite/golden/TestDeprecatedService.java.txt
Expand Up @@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName;
* Test service that has been deprecated and should generate with Java's &#64;Deprecated annotation
* </pre>
*/
@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 {
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/testLite/golden/TestService.java.txt
Expand Up @@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName;
* Test service that supports all call types.
* </pre>
*/
@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 {

Expand Down

0 comments on commit 8a21afc

Please sign in to comment.