Skip to content

Commit 8a21afc

Browse files
authoredApr 19, 2024··
compiler: add option @generated=omit (#11086)
related to #11081
1 parent 52e65ec commit 8a21afc

File tree

6 files changed

+24
-33
lines changed

6 files changed

+24
-33
lines changed
 

Diff for: ‎compiler/build.gradle

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ protobuf {
192192
java { option 'lite' }
193193
}
194194
plugins {
195-
grpc { option 'lite' }
195+
grpc {
196+
option 'lite'
197+
option '@generated=omit'
198+
}
196199
}
197200
}
198201
}

Diff for: ‎compiler/src/java_plugin/cpp/java_generator.cpp

+6-13
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,8 @@ static void PrintService(const ServiceDescriptor* service,
11161116
std::map<std::string, std::string>* vars,
11171117
Printer* p,
11181118
ProtoFlavor flavor,
1119-
bool disable_version) {
1119+
bool disable_version,
1120+
GeneratedAnnotation generated_annotation) {
11201121
(*vars)["service_name"] = service->name();
11211122
(*vars)["file_name"] = service->file()->name();
11221123
(*vars)["service_class_name"] = ServiceClassName(service);
@@ -1129,24 +1130,17 @@ static void PrintService(const ServiceDescriptor* service,
11291130
// TODO(nmittler): Replace with WriteServiceDocComment once included by protobuf distro.
11301131
GrpcWriteServiceDocComment(p, service, NONE);
11311132

1132-
if ((*vars)["JakartaMode"] == "javax") {
1133+
if (generated_annotation == GeneratedAnnotation::JAVAX) {
11331134
p->Print(
11341135
*vars,
11351136
"@javax.annotation.Generated(\n"
11361137
" value = \"by gRPC proto compiler$grpc_version$\",\n"
11371138
" comments = \"Source: $file_name$\")\n"
11381139
"@$GrpcGenerated$\n");
1139-
} else if ((*vars)["JakartaMode"] == "omit") {
1140+
} else { // GeneratedAnnotation::OMIT
11401141
p->Print(
11411142
*vars,
11421143
"@$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");
11501144
}
11511145

11521146
if (service->options().deprecated()) {
@@ -1232,7 +1226,7 @@ void GenerateService(const ServiceDescriptor* service,
12321226
protobuf::io::ZeroCopyOutputStream* out,
12331227
ProtoFlavor flavor,
12341228
bool disable_version,
1235-
std::string jakarta_mode) {
1229+
GeneratedAnnotation generated_annotation) {
12361230
// All non-generated classes must be referred by fully qualified names to
12371231
// avoid collision with generated classes.
12381232
std::map<std::string, std::string> vars;
@@ -1264,7 +1258,6 @@ void GenerateService(const ServiceDescriptor* service,
12641258
vars["MethodDescriptor"] = "io.grpc.MethodDescriptor";
12651259
vars["StreamObserver"] = "io.grpc.stub.StreamObserver";
12661260
vars["Iterator"] = "java.util.Iterator";
1267-
vars["JakartaMode"] = jakarta_mode;
12681261
vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated";
12691262
vars["ListenableFuture"] =
12701263
"com.google.common.util.concurrent.ListenableFuture";
@@ -1283,7 +1276,7 @@ void GenerateService(const ServiceDescriptor* service,
12831276
if (!vars["Package"].empty()) {
12841277
vars["Package"].append(".");
12851278
}
1286-
PrintService(service, &vars, &printer, flavor, disable_version);
1279+
PrintService(service, &vars, &printer, flavor, disable_version, generated_annotation);
12871280
}
12881281

12891282
std::string ServiceJavaPackage(const FileDescriptor* file) {

Diff for: ‎compiler/src/java_plugin/cpp/java_generator.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ enum ProtoFlavor {
5757
NORMAL, LITE
5858
};
5959

60+
enum GeneratedAnnotation {
61+
OMIT, JAVAX
62+
};
63+
6064
// Returns the package name of the gRPC services defined in the given file.
6165
std::string ServiceJavaPackage(const impl::protobuf::FileDescriptor* file);
6266

@@ -69,7 +73,7 @@ void GenerateService(const impl::protobuf::ServiceDescriptor* service,
6973
impl::protobuf::io::ZeroCopyOutputStream* out,
7074
ProtoFlavor flavor,
7175
bool disable_version,
72-
std::string jakarta_mode);
76+
GeneratedAnnotation generated_annotation);
7377

7478
} // namespace java_grpc_generator
7579

Diff for: ‎compiler/src/java_plugin/cpp/java_plugin.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,21 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
5858

5959
java_grpc_generator::ProtoFlavor flavor =
6060
java_grpc_generator::ProtoFlavor::NORMAL;
61+
java_grpc_generator::GeneratedAnnotation generated_annotation =
62+
java_grpc_generator::GeneratedAnnotation::JAVAX;
6163

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;
6964
bool disable_version = false;
7065
for (size_t i = 0; i < options.size(); i++) {
7166
if (options[i].first == "lite") {
7267
flavor = java_grpc_generator::ProtoFlavor::LITE;
7368
} else if (options[i].first == "noversion") {
7469
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";
70+
} else if (options[i].first == "@generated") {
71+
if (options[i].second == "omit") {
72+
generated_annotation = java_grpc_generator::GeneratedAnnotation::OMIT;
73+
} else if (options[i].second == "javax") {
74+
generated_annotation = java_grpc_generator::GeneratedAnnotation::JAVAX;
75+
}
7976
}
8077
}
8178

@@ -88,7 +85,7 @@ class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
8885
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
8986
context->Open(filename));
9087
java_grpc_generator::GenerateService(
91-
service, output.get(), flavor, disable_version, jakarta_mode);
88+
service, output.get(), flavor, disable_version, generated_annotation);
9289
}
9390
return true;
9491
}

Diff for: ‎compiler/src/testLite/golden/TestDeprecatedService.java.txt

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName;
77
* Test service that has been deprecated and should generate with Java's &#64;Deprecated annotation
88
* </pre>
99
*/
10-
@javax.annotation.Generated(
11-
value = "by gRPC proto compiler (version 1.64.0-SNAPSHOT)",
12-
comments = "Source: grpc/testing/compiler/test.proto")
1310
@io.grpc.stub.annotations.GrpcGenerated
1411
@java.lang.Deprecated
1512
public final class TestDeprecatedServiceGrpc {

Diff for: ‎compiler/src/testLite/golden/TestService.java.txt

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import static io.grpc.MethodDescriptor.generateFullMethodName;
77
* Test service that supports all call types.
88
* </pre>
99
*/
10-
@javax.annotation.Generated(
11-
value = "by gRPC proto compiler (version 1.64.0-SNAPSHOT)",
12-
comments = "Source: grpc/testing/compiler/test.proto")
1310
@io.grpc.stub.annotations.GrpcGenerated
1411
public final class TestServiceGrpc {
1512

0 commit comments

Comments
 (0)
Please sign in to comment.