1 /*
2 * Copyright 2019 The gRPC Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // Generates Java gRPC service interface out of Protobuf IDL.
18 //
19 // This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
20 // and net/proto2/compiler/public/plugin.h for more information on plugins.
21
22 #include <memory>
23
24 #include "java_generator.h"
25 #include <google/protobuf/compiler/code_generator.h>
26 #include <google/protobuf/compiler/plugin.h>
27 #include <google/protobuf/descriptor.h>
28 #include <google/protobuf/io/zero_copy_stream.h>
29
30 namespace protobuf = google::protobuf;
31
JavaPackageToDir(const std::string & package_name)32 static std::string JavaPackageToDir(const std::string& package_name) {
33 std::string package_dir = package_name;
34 for (size_t i = 0; i < package_dir.size(); ++i) {
35 if (package_dir[i] == '.') {
36 package_dir[i] = '/';
37 }
38 }
39 if (!package_dir.empty()) package_dir += "/";
40 return package_dir;
41 }
42
43 class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
44 public:
JavaGrpcGenerator()45 JavaGrpcGenerator() {}
~JavaGrpcGenerator()46 virtual ~JavaGrpcGenerator() {}
47
GetSupportedFeatures() const48 uint64_t GetSupportedFeatures() const override {
49 return FEATURE_PROTO3_OPTIONAL;
50 }
51
Generate(const protobuf::FileDescriptor * file,const std::string & parameter,protobuf::compiler::GeneratorContext * context,std::string * error) const52 virtual bool Generate(const protobuf::FileDescriptor* file,
53 const std::string& parameter,
54 protobuf::compiler::GeneratorContext* context,
55 std::string* error) const override {
56 std::vector<std::pair<std::string, std::string> > options;
57 protobuf::compiler::ParseGeneratorParameter(parameter, &options);
58
59 java_grpc_generator::ProtoFlavor flavor =
60 java_grpc_generator::ProtoFlavor::NORMAL;
61
62 bool disable_version = false;
63 for (size_t i = 0; i < options.size(); i++) {
64 if (options[i].first == "lite") {
65 flavor = java_grpc_generator::ProtoFlavor::LITE;
66 } else if (options[i].first == "noversion") {
67 disable_version = true;
68 }
69 }
70
71 std::string package_name = java_grpc_generator::ServiceJavaPackage(file);
72 std::string package_filename = JavaPackageToDir(package_name);
73 for (int i = 0; i < file->service_count(); ++i) {
74 const protobuf::ServiceDescriptor* service = file->service(i);
75 std::string filename = package_filename
76 + java_grpc_generator::ServiceClassName(service) + ".java";
77 std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
78 context->Open(filename));
79 java_grpc_generator::GenerateService(
80 service, output.get(), flavor, disable_version);
81 }
82 return true;
83 }
84 };
85
main(int argc,char * argv[])86 int main(int argc, char* argv[]) {
87 JavaGrpcGenerator generator;
88 return protobuf::compiler::PluginMain(argc, argv, &generator);
89 }
90