Skip to content

Commit f7f57b7

Browse files
author
Xudong Ma
committed
Put nano generated code into nano package, unless option javanano_use_deprecated_package is set.
Also fix the behaior while java_package is set to empty.
1 parent f15f672 commit f7f57b7

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

android-interop-testing/app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protobuf {
3333
}
3434
plugins {
3535
grpc {
36-
artifact = 'io.grpc:protoc-gen-grpc-java:0.9.0-SNAPSHOT'
36+
artifact = 'io.grpc:protoc-gen-grpc-java:0.10.0-SNAPSHOT'
3737
}
3838
}
3939
generateProtoTasks {
@@ -64,10 +64,10 @@ dependencies {
6464
compile 'com.squareup.okhttp:okhttp:2.2.0'
6565
testCompile 'junit:junit:4.12'
6666
// You need to build grpc-java to obtain these libraries below.
67-
compile 'io.grpc:grpc-core:0.9.0-SNAPSHOT'
68-
compile 'io.grpc:grpc-protobuf-nano:0.9.0-SNAPSHOT'
69-
compile 'io.grpc:grpc-okhttp:0.9.0-SNAPSHOT'
70-
compile 'io.grpc:grpc-stub:0.9.0-SNAPSHOT'
71-
compile 'io.grpc:grpc-testing:0.9.0-SNAPSHOT'
67+
compile 'io.grpc:grpc-core:0.10.0-SNAPSHOT'
68+
compile 'io.grpc:grpc-protobuf-nano:0.10.0-SNAPSHOT'
69+
compile 'io.grpc:grpc-okhttp:0.10.0-SNAPSHOT'
70+
compile 'io.grpc:grpc-stub:0.10.0-SNAPSHOT'
71+
compile 'io.grpc:grpc-testing:0.10.0-SNAPSHOT'
7272
compile 'javax.annotation:javax.annotation-api:1.2'
7373
}

android-interop-testing/app/src/main/java/io/grpc/android/integrationtest/InteropTester.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import io.grpc.android.integrationtest.nano.Messages.StreamingInputCallResponse;
6161
import io.grpc.android.integrationtest.nano.Messages.StreamingOutputCallRequest;
6262
import io.grpc.android.integrationtest.nano.Messages.StreamingOutputCallResponse;
63+
import io.grpc.android.integrationtest.nano.TestServiceGrpc;
64+
import io.grpc.android.integrationtest.nano.UnimplementedServiceGrpc;
6365
import io.grpc.okhttp.OkHttpChannelBuilder;
6466
import io.grpc.stub.StreamObserver;
6567
import io.grpc.testing.StreamRecorder;

compiler/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ artifacts {
208208

209209
test.dependsOn('testGolden', 'testNanoGolden')
210210

211-
def configureTestTask(Task task, String suffix) {
211+
def configureTestTask(Task task, String suffix, String extraPackage) {
212212
task.dependsOn "generateTest${suffix}Proto"
213213
if (osdetector.os != 'windows') {
214214
task.executable "diff"
@@ -217,11 +217,11 @@ def configureTestTask(Task task, String suffix) {
217217
}
218218
// File isn't found on Windows if last slash is forward-slash
219219
def slash = System.getProperty("file.separator")
220-
task.args "$buildDir/generated/source/proto/test${suffix}/grpc/io/grpc/testing/integration${slash}TestServiceGrpc.java",
220+
task.args "$buildDir/generated/source/proto/test${suffix}/grpc/io/grpc/testing/integration${extraPackage}${slash}TestServiceGrpc.java",
221221
"$projectDir/src/test/golden/TestService${suffix}.java.txt"
222222
}
223223

224224
task testGolden(type: Exec)
225225
task testNanoGolden(type: Exec)
226-
configureTestTask(testGolden, '')
227-
configureTestTask(testNanoGolden, 'Nano')
226+
configureTestTask(testGolden, '', '')
227+
configureTestTask(testNanoGolden, 'Nano', '/nano')

compiler/src/java_plugin/cpp/java_generator.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ static inline string MessageFullJavaName(bool nano, const Descriptor* desc) {
6161
if (nano && !desc->file()->options().javanano_use_deprecated_package()) {
6262
// XXX: Add "nano" to the original package
6363
// (https://github.com/grpc/grpc-java/issues/900)
64+
if (isupper(name[0])) {
65+
// No java package specified.
66+
return "nano." + name;
67+
}
6468
for (int i = 0; i < name.size(); ++i) {
6569
if ((name[i] == '.') && (i < (name.size() - 1)) && isupper(name[i + 1])) {
6670
return name.substr(0, i + 1) + "nano." + name.substr(i + 1);
@@ -624,10 +628,12 @@ void GenerateService(const ServiceDescriptor* service,
624628
vars["ExperimentalApi"] = "io.grpc.ExperimentalApi";
625629

626630
Printer printer(out, '$');
627-
string package_name = ServiceJavaPackage(service->file());
628-
printer.Print(
629-
"package $package_name$;\n\n",
630-
"package_name", package_name);
631+
string package_name = ServiceJavaPackage(service->file(), generate_nano);
632+
if (!package_name.empty()) {
633+
printer.Print(
634+
"package $package_name$;\n\n",
635+
"package_name", package_name);
636+
}
631637
PrintImports(&printer, generate_nano);
632638

633639
// Package string is used to fully qualify method names.
@@ -638,11 +644,19 @@ void GenerateService(const ServiceDescriptor* service,
638644
PrintService(service, &vars, &printer, generate_nano);
639645
}
640646

641-
string ServiceJavaPackage(const FileDescriptor* file) {
647+
string ServiceJavaPackage(const FileDescriptor* file, bool nano) {
642648
string result = google::protobuf::compiler::java::ClassName(file);
643649
size_t last_dot_pos = result.find_last_of('.');
644650
if (last_dot_pos != string::npos) {
645651
result.resize(last_dot_pos);
652+
} else {
653+
result = "";
654+
}
655+
if (nano && !file->options().javanano_use_deprecated_package()) {
656+
if (!result.empty()) {
657+
result += ".";
658+
}
659+
result += "nano";
646660
}
647661
return result;
648662
}

compiler/src/java_plugin/cpp/java_generator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using namespace std;
3939
namespace java_grpc_generator {
4040

4141
// Returns the package name of the gRPC services defined in the given file.
42-
string ServiceJavaPackage(const google::protobuf::FileDescriptor* file);
42+
string ServiceJavaPackage(const google::protobuf::FileDescriptor* file, bool nano);
4343

4444
// Returns the name of the outer class that wraps in all the generated code for
4545
// the given service.

compiler/src/java_plugin/cpp/java_plugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include "java_generator.h"
99
#include <google/protobuf/compiler/code_generator.h>
1010
#include <google/protobuf/compiler/plugin.h>
11-
#include <google/protobuf/io/zero_copy_stream.h>
1211
#include <google/protobuf/descriptor.h>
12+
#include <google/protobuf/io/zero_copy_stream.h>
1313

1414
static string JavaPackageToDir(const string& package_name) {
1515
string package_dir = package_name;
@@ -41,7 +41,7 @@ class JavaGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
4141
}
4242
}
4343

44-
string package_name = java_grpc_generator::ServiceJavaPackage(file);
44+
string package_name = java_grpc_generator::ServiceJavaPackage(file, generate_nano);
4545
string package_filename = JavaPackageToDir(package_name);
4646
for (int i = 0; i < file->service_count(); ++i) {
4747
const google::protobuf::ServiceDescriptor* service = file->service(i);

compiler/src/test/golden/TestServiceNano.java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.grpc.testing.integration;
1+
package io.grpc.testing.integration.nano;
22

33
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
44
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;

0 commit comments

Comments
 (0)