blob: 2eed9d260d1698c9f6b4afb94e0c1773fc28f17f [file] [log] [blame]
Yun Pengfa7299c2020-07-14 10:39:21 +02001/*
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
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020017// 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>
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020028#include <google/protobuf/io/zero_copy_stream.h>
29
Dmitry Ivankov77537622020-10-15 08:50:52 +020030namespace protobuf = google::protobuf;
31
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020032static std::string JavaPackageToDir(const std::string& package_name) {
33 std::string package_dir = package_name;
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020034 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
Dmitry Ivankov77537622020-10-15 08:50:52 +020043class JavaGrpcGenerator : public protobuf::compiler::CodeGenerator {
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020044 public:
45 JavaGrpcGenerator() {}
46 virtual ~JavaGrpcGenerator() {}
47
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020048 uint64_t GetSupportedFeatures() const override {
49 return FEATURE_PROTO3_OPTIONAL;
50 }
51
Dmitry Ivankov77537622020-10-15 08:50:52 +020052 virtual bool Generate(const protobuf::FileDescriptor* file,
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020053 const std::string& parameter,
Dmitry Ivankov77537622020-10-15 08:50:52 +020054 protobuf::compiler::GeneratorContext* context,
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020055 std::string* error) const override {
56 std::vector<std::pair<std::string, std::string> > options;
Dmitry Ivankov77537622020-10-15 08:50:52 +020057 protobuf::compiler::ParseGeneratorParameter(parameter, &options);
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020058
59 java_grpc_generator::ProtoFlavor flavor =
60 java_grpc_generator::ProtoFlavor::NORMAL;
Jakob Buchgraber74cea832017-05-29 09:56:22 +020061
Jakob Buchgrabera4b4f8f2018-03-07 16:13:48 +010062 bool disable_version = false;
63 for (size_t i = 0; i < options.size(); i++) {
Yun Pengfa7299c2020-07-14 10:39:21 +020064 if (options[i].first == "lite") {
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020065 flavor = java_grpc_generator::ProtoFlavor::LITE;
Jakob Buchgrabera4b4f8f2018-03-07 16:13:48 +010066 } else if (options[i].first == "noversion") {
67 disable_version = true;
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020068 }
69 }
70
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020071 std::string package_name = java_grpc_generator::ServiceJavaPackage(file);
72 std::string package_filename = JavaPackageToDir(package_name);
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020073 for (int i = 0; i < file->service_count(); ++i) {
Dmitry Ivankov77537622020-10-15 08:50:52 +020074 const protobuf::ServiceDescriptor* service = file->service(i);
Dmitry Ivankovd4cd4e72020-10-09 15:31:26 +020075 std::string filename = package_filename
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020076 + java_grpc_generator::ServiceClassName(service) + ".java";
Dmitry Ivankov77537622020-10-15 08:50:52 +020077 std::unique_ptr<protobuf::io::ZeroCopyOutputStream> output(
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020078 context->Open(filename));
Jakob Buchgrabera4b4f8f2018-03-07 16:13:48 +010079 java_grpc_generator::GenerateService(
80 service, output.get(), flavor, disable_version);
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020081 }
82 return true;
83 }
84};
85
86int main(int argc, char* argv[]) {
87 JavaGrpcGenerator generator;
Dmitry Ivankov77537622020-10-15 08:50:52 +020088 return protobuf::compiler::PluginMain(argc, argv, &generator);
Klaus Aehlig0b6549f2016-10-24 14:01:59 +020089}