• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Generates Java gRPC service interface out of Protobuf IDL.
2 //
3 // This is a Proto2 compiler plugin.  See net/proto2/compiler/proto/plugin.proto
4 // and net/proto2/compiler/public/plugin.h for more information on plugins.
5 
6 #include <memory>
7 
8 #include "java_generator.h"
9 #include <google/protobuf/compiler/code_generator.h>
10 #include <google/protobuf/compiler/plugin.h>
11 #include <google/protobuf/descriptor.h>
12 #include <google/protobuf/io/zero_copy_stream.h>
13 
JavaPackageToDir(const string & package_name)14 static string JavaPackageToDir(const string& package_name) {
15   string package_dir = package_name;
16   for (size_t i = 0; i < package_dir.size(); ++i) {
17     if (package_dir[i] == '.') {
18       package_dir[i] = '/';
19     }
20   }
21   if (!package_dir.empty()) package_dir += "/";
22   return package_dir;
23 }
24 
25 class JavaGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
26  public:
JavaGrpcGenerator()27   JavaGrpcGenerator() {}
~JavaGrpcGenerator()28   virtual ~JavaGrpcGenerator() {}
29 
Generate(const google::protobuf::FileDescriptor * file,const string & parameter,google::protobuf::compiler::GeneratorContext * context,string * error) const30   virtual bool Generate(const google::protobuf::FileDescriptor* file,
31                         const string& parameter,
32                         google::protobuf::compiler::GeneratorContext* context,
33                         string* error) const {
34     std::vector<std::pair<string, string> > options;
35     google::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
36 
37     java_grpc_generator::ProtoFlavor flavor =
38         java_grpc_generator::ProtoFlavor::NORMAL;
39 
40     bool disable_version = false;
41     for (size_t i = 0; i < options.size(); i++) {
42       if (options[i].first == "nano") {
43         flavor = java_grpc_generator::ProtoFlavor::NANO;
44       } else if (options[i].first == "lite") {
45         flavor = java_grpc_generator::ProtoFlavor::LITE;
46       } else if (options[i].first == "noversion") {
47         disable_version = true;
48       }
49     }
50 
51     string package_name = java_grpc_generator::ServiceJavaPackage(
52         file, flavor == java_grpc_generator::ProtoFlavor::NANO);
53     string package_filename = JavaPackageToDir(package_name);
54     for (int i = 0; i < file->service_count(); ++i) {
55       const google::protobuf::ServiceDescriptor* service = file->service(i);
56       string filename = package_filename
57           + java_grpc_generator::ServiceClassName(service) + ".java";
58       std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
59           context->Open(filename));
60       java_grpc_generator::GenerateService(
61           service, output.get(), flavor, disable_version);
62     }
63     return true;
64   }
65 };
66 
main(int argc,char * argv[])67 int main(int argc, char* argv[]) {
68   JavaGrpcGenerator generator;
69   return google::protobuf::compiler::PluginMain(argc, argv, &generator);
70 }
71