diff --git a/compiler/src/java_plugin/cpp/java_generator.cpp b/compiler/src/java_plugin/cpp/java_generator.cpp index 7cb01238ac..411d6738a0 100644 --- a/compiler/src/java_plugin/cpp/java_generator.cpp +++ b/compiler/src/java_plugin/cpp/java_generator.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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 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]); @@ -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; } diff --git a/compiler/src/test/golden/TestService.java.txt b/compiler/src/test/golden/TestService.java.txt index a14a4c4894..beaded3885 100644 --- a/compiler/src/test/golden/TestService.java.txt +++ b/compiler/src/test/golden/TestService.java.txt @@ -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 METHOD_IMPORT = getImportMethodHelper(); + + private static volatile io.grpc.MethodDescriptor getImportMethod; + + @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") + public static io.grpc.MethodDescriptor getImportMethod() { + return getImportMethodHelper(); + } + + private static io.grpc.MethodDescriptor getImportMethodHelper() { + io.grpc.MethodDescriptor getImportMethod; + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + synchronized (TestServiceGrpc.class) { + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + TestServiceGrpc.getImportMethod = getImportMethod = + io.grpc.MethodDescriptor.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 @@ -304,6 +341,17 @@ public final class TestServiceGrpc { return asyncUnimplementedStreamingCall(getHalfBidiCallMethodHelper(), responseObserver); } + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncUnimplementedStreamingCall(getImportMethodHelper(), responseObserver); + } + @java.lang.Override public final io.grpc.ServerServiceDefinition bindService() { return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) .addMethod( @@ -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(); } } @@ -428,6 +483,18 @@ public final class TestServiceGrpc { return asyncBidiStreamingCall( getChannel().newCall(getHalfBidiCallMethodHelper(), getCallOptions()), responseObserver); } + + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncBidiStreamingCall( + getChannel().newCall(getImportMethodHelper(), getCallOptions()), responseObserver); + } } /** @@ -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 implements io.grpc.stub.ServerCalls.UnaryMethod, @@ -559,6 +627,9 @@ public final class TestServiceGrpc { case METHODID_HALF_BIDI_CALL: return (io.grpc.stub.StreamObserver) serviceImpl.halfBidiCall( (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_IMPORT: + return (io.grpc.stub.StreamObserver) serviceImpl.import_( + (io.grpc.stub.StreamObserver) responseObserver); default: throw new AssertionError(); } @@ -615,6 +686,7 @@ public final class TestServiceGrpc { .addMethod(getStreamingInputCallMethodHelper()) .addMethod(getFullBidiCallMethodHelper()) .addMethod(getHalfBidiCallMethodHelper()) + .addMethod(getImportMethodHelper()) .build(); } } diff --git a/compiler/src/test/proto/test.proto b/compiler/src/test/proto/test.proto index dfaaed571c..b565638a7e 100644 --- a/compiler/src/test/proto/test.proto +++ b/compiler/src/test/proto/test.proto @@ -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); } diff --git a/compiler/src/testLite/golden/TestService.java.txt b/compiler/src/testLite/golden/TestService.java.txt index 0e41f596ac..42a6a45000 100644 --- a/compiler/src/testLite/golden/TestService.java.txt +++ b/compiler/src/testLite/golden/TestService.java.txt @@ -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 METHOD_IMPORT = getImportMethodHelper(); + + private static volatile io.grpc.MethodDescriptor getImportMethod; + + @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") + public static io.grpc.MethodDescriptor getImportMethod() { + return getImportMethodHelper(); + } + + private static io.grpc.MethodDescriptor getImportMethodHelper() { + io.grpc.MethodDescriptor getImportMethod; + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + synchronized (TestServiceGrpc.class) { + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + TestServiceGrpc.getImportMethod = getImportMethod = + io.grpc.MethodDescriptor.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 @@ -299,6 +335,17 @@ public final class TestServiceGrpc { return asyncUnimplementedStreamingCall(getHalfBidiCallMethodHelper(), responseObserver); } + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncUnimplementedStreamingCall(getImportMethodHelper(), responseObserver); + } + @java.lang.Override public final io.grpc.ServerServiceDefinition bindService() { return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) .addMethod( @@ -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(); } } @@ -423,6 +477,18 @@ public final class TestServiceGrpc { return asyncBidiStreamingCall( getChannel().newCall(getHalfBidiCallMethodHelper(), getCallOptions()), responseObserver); } + + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncBidiStreamingCall( + getChannel().newCall(getImportMethodHelper(), getCallOptions()), responseObserver); + } } /** @@ -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 implements io.grpc.stub.ServerCalls.UnaryMethod, @@ -554,6 +621,9 @@ public final class TestServiceGrpc { case METHODID_HALF_BIDI_CALL: return (io.grpc.stub.StreamObserver) serviceImpl.halfBidiCall( (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_IMPORT: + return (io.grpc.stub.StreamObserver) serviceImpl.import_( + (io.grpc.stub.StreamObserver) responseObserver); default: throw new AssertionError(); } @@ -574,6 +644,7 @@ public final class TestServiceGrpc { .addMethod(getStreamingInputCallMethodHelper()) .addMethod(getFullBidiCallMethodHelper()) .addMethod(getHalfBidiCallMethodHelper()) + .addMethod(getImportMethodHelper()) .build(); } } diff --git a/compiler/src/testNano/golden/TestService.java.txt b/compiler/src/testNano/golden/TestService.java.txt index cadb0cf1c0..95996458cd 100644 --- a/compiler/src/testNano/golden/TestService.java.txt +++ b/compiler/src/testNano/golden/TestService.java.txt @@ -222,6 +222,44 @@ public final class TestServiceGrpc { } return getHalfBidiCallMethod; } + private static final int ARG_IN_METHOD_IMPORT = 10; + private static final int ARG_OUT_METHOD_IMPORT = 11; + @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") + @java.lang.Deprecated // Use {@link #getImportMethod()} instead. + public static final io.grpc.MethodDescriptor METHOD_IMPORT = getImportMethodHelper(); + + private static volatile io.grpc.MethodDescriptor getImportMethod; + + @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/1901") + public static io.grpc.MethodDescriptor getImportMethod() { + return getImportMethodHelper(); + } + + private static io.grpc.MethodDescriptor getImportMethodHelper() { + io.grpc.MethodDescriptor getImportMethod; + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + synchronized (TestServiceGrpc.class) { + if ((getImportMethod = TestServiceGrpc.getImportMethod) == null) { + TestServiceGrpc.getImportMethod = getImportMethod = + io.grpc.MethodDescriptor.newBuilder() + .setType(io.grpc.MethodDescriptor.MethodType.BIDI_STREAMING) + .setFullMethodName(generateFullMethodName( + "grpc.testing.TestService", "Import")) + .setSampledToLocalTracing(true) + .setRequestMarshaller(io.grpc.protobuf.nano.NanoUtils.marshaller( + new NanoFactory(ARG_IN_METHOD_IMPORT))) + .setResponseMarshaller(io.grpc.protobuf.nano.NanoUtils.marshaller( + new NanoFactory(ARG_OUT_METHOD_IMPORT))) + .build(); + } + } + } + return getImportMethod; + } private static final class NanoFactory implements io.grpc.protobuf.nano.MessageNanoFactory { @@ -265,6 +303,12 @@ public final class TestServiceGrpc { case ARG_OUT_METHOD_HALF_BIDI_CALL: o = new io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse(); break; + case ARG_IN_METHOD_IMPORT: + o = new io.grpc.testing.integration.nano.Test.StreamingInputCallRequest(); + break; + case ARG_OUT_METHOD_IMPORT: + o = new io.grpc.testing.integration.nano.Test.StreamingInputCallResponse(); + break; default: throw new AssertionError(); } @@ -362,6 +406,17 @@ public final class TestServiceGrpc { return asyncUnimplementedStreamingCall(getHalfBidiCallMethodHelper(), responseObserver); } + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncUnimplementedStreamingCall(getImportMethodHelper(), responseObserver); + } + @java.lang.Override public final io.grpc.ServerServiceDefinition bindService() { return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor()) .addMethod( @@ -399,6 +454,13 @@ public final class TestServiceGrpc { io.grpc.testing.integration.nano.Test.StreamingOutputCallRequest, io.grpc.testing.integration.nano.Test.StreamingOutputCallResponse>( this, METHODID_HALF_BIDI_CALL))) + .addMethod( + getImportMethodHelper(), + asyncBidiStreamingCall( + new MethodHandlers< + io.grpc.testing.integration.nano.Test.StreamingInputCallRequest, + io.grpc.testing.integration.nano.Test.StreamingInputCallResponse>( + this, METHODID_IMPORT))) .build(); } } @@ -486,6 +548,18 @@ public final class TestServiceGrpc { return asyncBidiStreamingCall( getChannel().newCall(getHalfBidiCallMethodHelper(), getCallOptions()), responseObserver); } + + /** + *
+     * An RPC method whose Java name collides with a keyword, and whose generated
+     * method should have a '_' appended.
+     * 
+ */ + public io.grpc.stub.StreamObserver import_( + io.grpc.stub.StreamObserver responseObserver) { + return asyncBidiStreamingCall( + getChannel().newCall(getImportMethodHelper(), getCallOptions()), responseObserver); + } } /** @@ -572,6 +646,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 implements io.grpc.stub.ServerCalls.UnaryMethod, @@ -617,6 +692,9 @@ public final class TestServiceGrpc { case METHODID_HALF_BIDI_CALL: return (io.grpc.stub.StreamObserver) serviceImpl.halfBidiCall( (io.grpc.stub.StreamObserver) responseObserver); + case METHODID_IMPORT: + return (io.grpc.stub.StreamObserver) serviceImpl.import_( + (io.grpc.stub.StreamObserver) responseObserver); default: throw new AssertionError(); } @@ -637,6 +715,7 @@ public final class TestServiceGrpc { .addMethod(getStreamingInputCallMethodHelper()) .addMethod(getFullBidiCallMethodHelper()) .addMethod(getHalfBidiCallMethodHelper()) + .addMethod(getImportMethodHelper()) .build(); } }