Skip to content
Merged
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
62 changes: 62 additions & 0 deletions compiler/src/java_plugin/cpp/java_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <vector>
#include <google/protobuf/compiler/java/java_names.h>
#include <google/protobuf/descriptor.h>
Expand Down Expand Up @@ -33,9 +34,67 @@ using google::protobuf::io::Printer;
using google::protobuf::SourceLocation;
using std::to_string;

// java keywords from: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9
static std::set<string> java_keywords = {
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"try",
"void",
"volatile",
"while",
// additional ones added by us
"true",
"false",
};

// Adjust a method name prefix identifier to follow the JavaBean spec:
// - decapitalize the first letter
// - remove embedded underscores & capitalize the following letter
// Finally, if the result is a reserved java keyword, append an underscore.
static string MixedLower(const string& word) {
string w;
w += tolower(word[0]);
Expand All @@ -48,6 +107,9 @@ static string MixedLower(const string& word) {
after_underscore = false;
}
}
if (java_keywords.find(w) != java_keywords.end()) {
return w + "_";
}
return w;
}

Expand Down
72 changes: 72 additions & 0 deletions compiler/src/test/golden/TestService.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,43 @@ public final class TestServiceGrpc {
}
return getHalfBidiCallMethod;
}
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
@java.lang.Deprecated // Use {@link #getImportMethod()} instead.
public static final io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> METHOD_IMPORT = getImportMethodHelper();

private static volatile io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod;

@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
public static io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod() {
return getImportMethodHelper();
}

private static io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethodHelper() {
io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest, io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod;
if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) {
synchronized (TestServiceGrpc.class) {
if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) {
TestServiceGrpc.getImportMethod = getImportMethod =
io.grpc.MethodDescriptor.<io.grpc.testing.integration.Test.StreamingInputCallRequest, io.grpc.testing.integration.Test.StreamingInputCallResponse>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "Import"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallResponse.getDefaultInstance()))
.setSchemaDescriptor(new TestServiceMethodDescriptorSupplier("Import"))
.build();
}
}
}
return getImportMethod;
}

/**
* Creates a new async stub that supports all call types for the service
Expand Down Expand Up @@ -304,6 +341,17 @@ public final class TestServiceGrpc {
return asyncUnimplementedStreamingCall(getHalfBidiCallMethodHelper(), responseObserver);
}

/**
* <pre>
* An RPC method whose Java name collides with a keyword, and whose generated
* method should have a '_' appended.
* </pre>
*/
public io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallRequest> import_(
io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse> responseObserver) {
return asyncUnimplementedStreamingCall(getImportMethodHelper(), responseObserver);
}

@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
Expand Down Expand Up @@ -341,6 +389,13 @@ public final class TestServiceGrpc {
io.grpc.testing.integration.Test.StreamingOutputCallRequest,
io.grpc.testing.integration.Test.StreamingOutputCallResponse>(
this, METHODID_HALF_BIDI_CALL)))
.addMethod(
getImportMethodHelper(),
asyncBidiStreamingCall(
new MethodHandlers<
io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse>(
this, METHODID_IMPORT)))
.build();
}
}
Expand Down Expand Up @@ -428,6 +483,18 @@ public final class TestServiceGrpc {
return asyncBidiStreamingCall(
getChannel().newCall(getHalfBidiCallMethodHelper(), getCallOptions()), responseObserver);
}

/**
* <pre>
* An RPC method whose Java name collides with a keyword, and whose generated
* method should have a '_' appended.
* </pre>
*/
public io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallRequest> import_(
io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse> responseObserver) {
return asyncBidiStreamingCall(
getChannel().newCall(getImportMethodHelper(), getCallOptions()), responseObserver);
}
}

/**
Expand Down Expand Up @@ -514,6 +581,7 @@ public final class TestServiceGrpc {
private static final int METHODID_STREAMING_INPUT_CALL = 2;
private static final int METHODID_FULL_BIDI_CALL = 3;
private static final int METHODID_HALF_BIDI_CALL = 4;
private static final int METHODID_IMPORT = 5;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
Expand Down Expand Up @@ -559,6 +627,9 @@ public final class TestServiceGrpc {
case METHODID_HALF_BIDI_CALL:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.halfBidiCall(
(io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingOutputCallResponse>) responseObserver);
case METHODID_IMPORT:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.import_(
(io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse>) responseObserver);
default:
throw new AssertionError();
}
Expand Down Expand Up @@ -615,6 +686,7 @@ public final class TestServiceGrpc {
.addMethod(getStreamingInputCallMethodHelper())
.addMethod(getFullBidiCallMethodHelper())
.addMethod(getHalfBidiCallMethodHelper())
.addMethod(getImportMethodHelper())
.build();
}
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/test/proto/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ service TestService {
// first request.
rpc HalfBidiCall(stream StreamingOutputCallRequest)
returns (stream StreamingOutputCallResponse);

// An RPC method whose Java name collides with a keyword, and whose generated
// method should have a '_' appended.
rpc Import(stream StreamingInputCallRequest) returns (stream StreamingInputCallResponse);
}
71 changes: 71 additions & 0 deletions compiler/src/testLite/golden/TestService.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,42 @@ public final class TestServiceGrpc {
}
return getHalfBidiCallMethod;
}
@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
@java.lang.Deprecated // Use {@link #getImportMethod()} instead.
public static final io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> METHOD_IMPORT = getImportMethodHelper();

private static volatile io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod;

@io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901")
public static io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod() {
return getImportMethodHelper();
}

private static io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethodHelper() {
io.grpc.MethodDescriptor<io.grpc.testing.integration.Test.StreamingInputCallRequest, io.grpc.testing.integration.Test.StreamingInputCallResponse> getImportMethod;
if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) {
synchronized (TestServiceGrpc.class) {
if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) {
TestServiceGrpc.getImportMethod = getImportMethod =
io.grpc.MethodDescriptor.<io.grpc.testing.integration.Test.StreamingInputCallRequest, io.grpc.testing.integration.Test.StreamingInputCallResponse>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING)
.setFullMethodName(generateFullMethodName(
"grpc.testing.TestService", "Import"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.lite.ProtoLiteUtils.marshaller(
io.grpc.testing.integration.Test.StreamingInputCallResponse.getDefaultInstance()))
.build();
}
}
}
return getImportMethod;
}

/**
* Creates a new async stub that supports all call types for the service
Expand Down Expand Up @@ -299,6 +335,17 @@ public final class TestServiceGrpc {
return asyncUnimplementedStreamingCall(getHalfBidiCallMethodHelper(), responseObserver);
}

/**
* <pre>
* An RPC method whose Java name collides with a keyword, and whose generated
* method should have a '_' appended.
* </pre>
*/
public io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallRequest> import_(
io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse> responseObserver) {
return asyncUnimplementedStreamingCall(getImportMethodHelper(), responseObserver);
}

@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
Expand Down Expand Up @@ -336,6 +383,13 @@ public final class TestServiceGrpc {
io.grpc.testing.integration.Test.StreamingOutputCallRequest,
io.grpc.testing.integration.Test.StreamingOutputCallResponse>(
this, METHODID_HALF_BIDI_CALL)))
.addMethod(
getImportMethodHelper(),
asyncBidiStreamingCall(
new MethodHandlers<
io.grpc.testing.integration.Test.StreamingInputCallRequest,
io.grpc.testing.integration.Test.StreamingInputCallResponse>(
this, METHODID_IMPORT)))
.build();
}
}
Expand Down Expand Up @@ -423,6 +477,18 @@ public final class TestServiceGrpc {
return asyncBidiStreamingCall(
getChannel().newCall(getHalfBidiCallMethodHelper(), getCallOptions()), responseObserver);
}

/**
* <pre>
* An RPC method whose Java name collides with a keyword, and whose generated
* method should have a '_' appended.
* </pre>
*/
public io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallRequest> import_(
io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse> responseObserver) {
return asyncBidiStreamingCall(
getChannel().newCall(getImportMethodHelper(), getCallOptions()), responseObserver);
}
}

/**
Expand Down Expand Up @@ -509,6 +575,7 @@ public final class TestServiceGrpc {
private static final int METHODID_STREAMING_INPUT_CALL = 2;
private static final int METHODID_FULL_BIDI_CALL = 3;
private static final int METHODID_HALF_BIDI_CALL = 4;
private static final int METHODID_IMPORT = 5;

private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
Expand Down Expand Up @@ -554,6 +621,9 @@ public final class TestServiceGrpc {
case METHODID_HALF_BIDI_CALL:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.halfBidiCall(
(io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingOutputCallResponse>) responseObserver);
case METHODID_IMPORT:
return (io.grpc.stub.StreamObserver<Req>) serviceImpl.import_(
(io.grpc.stub.StreamObserver<io.grpc.testing.integration.Test.StreamingInputCallResponse>) responseObserver);
default:
throw new AssertionError();
}
Expand All @@ -574,6 +644,7 @@ public final class TestServiceGrpc {
.addMethod(getStreamingInputCallMethodHelper())
.addMethod(getFullBidiCallMethodHelper())
.addMethod(getHalfBidiCallMethodHelper())
.addMethod(getImportMethodHelper())
.build();
}
}
Expand Down
Loading