Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@CanIgnoreReturnValue for blocking stub methods returning Empty #11296

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/src/java_plugin/cpp/java_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ static void PrintStub(
for (int i = 0; i < service->method_count(); ++i) {
const MethodDescriptor* method = service->method(i);
(*vars)["input_type"] = MessageFullJavaName(method->input_type());
(*vars)["output_type"] = MessageFullJavaName(method->output_type());
std::string output_type = MessageFullJavaName(method->output_type());
(*vars)["output_type"] = output_type;
(*vars)["lower_method_name"] = LowerMethodName(method);
(*vars)["method_method_name"] = MethodPropertiesGetterName(method);
bool client_streaming = method->client_streaming();
Expand All @@ -682,6 +683,11 @@ static void PrintStub(
// TODO(nmittler): Replace with WriteMethodDocComment once included by the protobuf distro.
GrpcWriteMethodDocComment(p, method);

if (call_type == BLOCKING_CALL &&
!server_streaming &&
output_type == "com.google.protobuf.Empty") {
p->Print(*vars, "@$CanIgnoreReturnValue$\n");
}
if (method->options().deprecated()) {
p->Print(*vars, "@$Deprecated$\n");
}
Expand Down Expand Up @@ -1261,6 +1267,7 @@ void GenerateService(const ServiceDescriptor* service,
vars["GrpcGenerated"] = "io.grpc.stub.annotations.GrpcGenerated";
vars["ListenableFuture"] =
"com.google.common.util.concurrent.ListenableFuture";
vars["CanIgnoreReturnValue"] = "com.google.errorprone.annotations.CanIgnoreReturnValue";

Printer printer(out, '$');
std::string package_name = ServiceJavaPackage(service->file());
Expand Down
83 changes: 79 additions & 4 deletions compiler/src/test/golden/TestService.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,37 @@ public final class TestServiceGrpc {
return getIdempotentCallMethod;
}

private static volatile io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty> getUnaryCallWithoutResultMethod;

@io.grpc.stub.annotations.RpcMethod(
fullMethodName = SERVICE_NAME + '/' + "UnaryCallWithoutResult",
requestType = io.grpc.testing.compiler.Test.SimpleRequest.class,
responseType = com.google.protobuf.Empty.class,
methodType = io.grpc.MethodDescriptor.MethodType.UNARY)
public static io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty> getUnaryCallWithoutResultMethod() {
io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest, com.google.protobuf.Empty> getUnaryCallWithoutResultMethod;
if ((getUnaryCallWithoutResultMethod = TestServiceGrpc.getUnaryCallWithoutResultMethod) == null) {
synchronized (TestServiceGrpc.class) {
if ((getUnaryCallWithoutResultMethod = TestServiceGrpc.getUnaryCallWithoutResultMethod) == null) {
TestServiceGrpc.getUnaryCallWithoutResultMethod = getUnaryCallWithoutResultMethod =
io.grpc.MethodDescriptor.<io.grpc.testing.compiler.Test.SimpleRequest, com.google.protobuf.Empty>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(SERVICE_NAME, "UnaryCallWithoutResult"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.compiler.Test.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
com.google.protobuf.Empty.getDefaultInstance()))
.setSchemaDescriptor(new TestServiceMethodDescriptorSupplier("UnaryCallWithoutResult"))
.build();
}
}
}
return getUnaryCallWithoutResultMethod;
}

/**
* Creates a new async stub that supports all call types for the service
*/
Expand Down Expand Up @@ -407,6 +438,13 @@ public final class TestServiceGrpc {
io.grpc.stub.StreamObserver<io.grpc.testing.compiler.Test.SimpleResponse> responseObserver) {
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getIdempotentCallMethod(), responseObserver);
}

/**
*/
default void unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request,
io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getUnaryCallWithoutResultMethod(), responseObserver);
}
}

/**
Expand Down Expand Up @@ -538,6 +576,14 @@ public final class TestServiceGrpc {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getIdempotentCallMethod(), getCallOptions()), request, responseObserver);
}

/**
*/
public void unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request,
io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getUnaryCallWithoutResultMethod(), getCallOptions()), request, responseObserver);
}
}

/**
Expand Down Expand Up @@ -601,6 +647,14 @@ public final class TestServiceGrpc {
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getIdempotentCallMethod(), getCallOptions(), request);
}

/**
*/
@com.google.errorprone.annotations.CanIgnoreReturnValue
public com.google.protobuf.Empty unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request) {
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getUnaryCallWithoutResultMethod(), getCallOptions(), request);
}
}

/**
Expand Down Expand Up @@ -655,16 +709,25 @@ public final class TestServiceGrpc {
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getIdempotentCallMethod(), getCallOptions()), request);
}

/**
*/
public com.google.common.util.concurrent.ListenableFuture<com.google.protobuf.Empty> unaryCallWithoutResult(
io.grpc.testing.compiler.Test.SimpleRequest request) {
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getUnaryCallWithoutResultMethod(), getCallOptions()), request);
}
}

private static final int METHODID_UNARY_CALL = 0;
private static final int METHODID_STREAMING_OUTPUT_CALL = 1;
private static final int METHODID_SAFE_CALL = 2;
private static final int METHODID_IDEMPOTENT_CALL = 3;
private static final int METHODID_STREAMING_INPUT_CALL = 4;
private static final int METHODID_FULL_BIDI_CALL = 5;
private static final int METHODID_HALF_BIDI_CALL = 6;
private static final int METHODID_IMPORT = 7;
private static final int METHODID_UNARY_CALL_WITHOUT_RESULT = 4;
private static final int METHODID_STREAMING_INPUT_CALL = 5;
private static final int METHODID_FULL_BIDI_CALL = 6;
private static final int METHODID_HALF_BIDI_CALL = 7;
private static final int METHODID_IMPORT = 8;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
Expand Down Expand Up @@ -699,6 +762,10 @@ public final class TestServiceGrpc {
serviceImpl.idempotentCall((io.grpc.testing.compiler.Test.SimpleRequest) request,
(io.grpc.stub.StreamObserver<io.grpc.testing.compiler.Test.SimpleResponse>) responseObserver);
break;
case METHODID_UNARY_CALL_WITHOUT_RESULT:
serviceImpl.unaryCallWithoutResult((io.grpc.testing.compiler.Test.SimpleRequest) request,
(io.grpc.stub.StreamObserver<com.google.protobuf.Empty>) responseObserver);
break;
default:
throw new AssertionError();
}
Expand Down Expand Up @@ -785,6 +852,13 @@ public final class TestServiceGrpc {
io.grpc.testing.compiler.Test.SimpleRequest,
io.grpc.testing.compiler.Test.SimpleResponse>(
service, METHODID_IDEMPOTENT_CALL)))
.addMethod(
getUnaryCallWithoutResultMethod(),
io.grpc.stub.ServerCalls.asyncUnaryCall(
new MethodHandlers<
io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty>(
service, METHODID_UNARY_CALL_WITHOUT_RESULT)))
.build();
}

Expand Down Expand Up @@ -841,6 +915,7 @@ public final class TestServiceGrpc {
.addMethod(getImportMethod())
.addMethod(getSafeCallMethod())
.addMethod(getIdempotentCallMethod())
.addMethod(getUnaryCallWithoutResultMethod())
.build();
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/test/proto/grpc/testing/compiler/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package grpc.testing.compiler;

option java_package = "io.grpc.testing.compiler";

import "google/protobuf/empty.proto";

message SimpleRequest {
}

Expand Down Expand Up @@ -80,6 +82,8 @@ service TestService {
rpc IdempotentCall(SimpleRequest) returns (SimpleResponse) {
option idempotency_level = IDEMPOTENT;
}

rpc UnaryCallWithoutResult(SimpleRequest) returns (google.protobuf.Empty);
}

// Test service that has been deprecated and should generate with Java's @Deprecated annotation
Expand Down
82 changes: 78 additions & 4 deletions compiler/src/testLite/golden/TestService.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,36 @@ public final class TestServiceGrpc {
return getIdempotentCallMethod;
}

private static volatile io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty> getUnaryCallWithoutResultMethod;

@io.grpc.stub.annotations.RpcMethod(
fullMethodName = SERVICE_NAME + '/' + "UnaryCallWithoutResult",
requestType = io.grpc.testing.compiler.Test.SimpleRequest.class,
responseType = com.google.protobuf.Empty.class,
methodType = io.grpc.MethodDescriptor.MethodType.UNARY)
public static io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty> getUnaryCallWithoutResultMethod() {
io.grpc.MethodDescriptor<io.grpc.testing.compiler.Test.SimpleRequest, com.google.protobuf.Empty> getUnaryCallWithoutResultMethod;
if ((getUnaryCallWithoutResultMethod = TestServiceGrpc.getUnaryCallWithoutResultMethod) == null) {
synchronized (TestServiceGrpc.class) {
if ((getUnaryCallWithoutResultMethod = TestServiceGrpc.getUnaryCallWithoutResultMethod) == null) {
TestServiceGrpc.getUnaryCallWithoutResultMethod = getUnaryCallWithoutResultMethod =
io.grpc.MethodDescriptor.<io.grpc.testing.compiler.Test.SimpleRequest, com.google.protobuf.Empty>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(SERVICE_NAME, "UnaryCallWithoutResult"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.compiler.Test.SimpleRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
com.google.protobuf.Empty.getDefaultInstance()))
.build();
}
}
}
return getUnaryCallWithoutResultMethod;
}

/**
* Creates a new async stub that supports all call types for the service
*/
Expand Down Expand Up @@ -396,6 +426,13 @@ public final class TestServiceGrpc {
io.grpc.stub.StreamObserver<io.grpc.testing.compiler.Test.SimpleResponse> responseObserver) {
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getIdempotentCallMethod(), responseObserver);
}

/**
*/
default void unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request,
io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getUnaryCallWithoutResultMethod(), responseObserver);
}
}

/**
Expand Down Expand Up @@ -527,6 +564,14 @@ public final class TestServiceGrpc {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getIdempotentCallMethod(), getCallOptions()), request, responseObserver);
}

/**
*/
public void unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request,
io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getUnaryCallWithoutResultMethod(), getCallOptions()), request, responseObserver);
}
}

/**
Expand Down Expand Up @@ -590,6 +635,14 @@ public final class TestServiceGrpc {
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getIdempotentCallMethod(), getCallOptions(), request);
}

/**
*/
@com.google.errorprone.annotations.CanIgnoreReturnValue
public com.google.protobuf.Empty unaryCallWithoutResult(io.grpc.testing.compiler.Test.SimpleRequest request) {
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getUnaryCallWithoutResultMethod(), getCallOptions(), request);
}
}

/**
Expand Down Expand Up @@ -644,16 +697,25 @@ public final class TestServiceGrpc {
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getIdempotentCallMethod(), getCallOptions()), request);
}

/**
*/
public com.google.common.util.concurrent.ListenableFuture<com.google.protobuf.Empty> unaryCallWithoutResult(
io.grpc.testing.compiler.Test.SimpleRequest request) {
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getUnaryCallWithoutResultMethod(), getCallOptions()), request);
}
}

private static final int METHODID_UNARY_CALL = 0;
private static final int METHODID_STREAMING_OUTPUT_CALL = 1;
private static final int METHODID_SAFE_CALL = 2;
private static final int METHODID_IDEMPOTENT_CALL = 3;
private static final int METHODID_STREAMING_INPUT_CALL = 4;
private static final int METHODID_FULL_BIDI_CALL = 5;
private static final int METHODID_HALF_BIDI_CALL = 6;
private static final int METHODID_IMPORT = 7;
private static final int METHODID_UNARY_CALL_WITHOUT_RESULT = 4;
private static final int METHODID_STREAMING_INPUT_CALL = 5;
private static final int METHODID_FULL_BIDI_CALL = 6;
private static final int METHODID_HALF_BIDI_CALL = 7;
private static final int METHODID_IMPORT = 8;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
Expand Down Expand Up @@ -688,6 +750,10 @@ public final class TestServiceGrpc {
serviceImpl.idempotentCall((io.grpc.testing.compiler.Test.SimpleRequest) request,
(io.grpc.stub.StreamObserver<io.grpc.testing.compiler.Test.SimpleResponse>) responseObserver);
break;
case METHODID_UNARY_CALL_WITHOUT_RESULT:
serviceImpl.unaryCallWithoutResult((io.grpc.testing.compiler.Test.SimpleRequest) request,
(io.grpc.stub.StreamObserver<com.google.protobuf.Empty>) responseObserver);
break;
default:
throw new AssertionError();
}
Expand Down Expand Up @@ -774,6 +840,13 @@ public final class TestServiceGrpc {
io.grpc.testing.compiler.Test.SimpleRequest,
io.grpc.testing.compiler.Test.SimpleResponse>(
service, METHODID_IDEMPOTENT_CALL)))
.addMethod(
getUnaryCallWithoutResultMethod(),
io.grpc.stub.ServerCalls.asyncUnaryCall(
new MethodHandlers<
io.grpc.testing.compiler.Test.SimpleRequest,
com.google.protobuf.Empty>(
service, METHODID_UNARY_CALL_WITHOUT_RESULT)))
.build();
}

Expand All @@ -794,6 +867,7 @@ public final class TestServiceGrpc {
.addMethod(getImportMethod())
.addMethod(getSafeCallMethod())
.addMethod(getIdempotentCallMethod())
.addMethod(getUnaryCallWithoutResultMethod())
.build();
}
}
Expand Down
Loading