java_kotlin_generator.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
32 
33 #include <google/protobuf/compiler/java/java_file.h>
34 #include <google/protobuf/compiler/java/java_helpers.h>
35 #include <google/protobuf/compiler/java/java_options.h>
36 #include <google/protobuf/compiler/java/java_generator.h>
37 #include <google/protobuf/compiler/code_generator.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace java {
43 
46 
48  return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL;
49 }
50 
52  const std::string& parameter,
54  std::string* error) const {
55  // -----------------------------------------------------------------
56  // parse generator options
57 
58  std::vector<std::pair<std::string, std::string> > options;
59  ParseGeneratorParameter(parameter, &options);
60  Options file_options;
61 
62  for (auto& option : options) {
63  if (option.first == "output_list_file") {
64  file_options.output_list_file = option.second;
65  } else if (option.first == "immutable") {
66  file_options.generate_immutable_code = true;
67  } else if (option.first == "mutable") {
68  *error = "Mutable not supported by Kotlin generator";
69  return false;
70  } else if (option.first == "shared") {
71  file_options.generate_shared_code = true;
72  } else if (option.first == "lite") {
73  file_options.enforce_lite = true;
74  } else if (option.first == "annotate_code") {
75  file_options.annotate_code = true;
76  } else if (option.first == "annotation_list_file") {
77  file_options.annotation_list_file = option.second;
78  } else {
79  *error = "Unknown generator option: " + option.first;
80  return false;
81  }
82  }
83 
84  // By default we generate immutable code and shared code for immutable API.
85  if (!file_options.generate_immutable_code &&
86  !file_options.generate_shared_code) {
87  file_options.generate_immutable_code = true;
88  file_options.generate_shared_code = true;
89  }
90 
91  std::vector<std::string> all_files;
92  std::vector<std::string> all_annotations;
93 
94  std::unique_ptr<FileGenerator> file_generator;
95  if (file_options.generate_immutable_code) {
96  file_generator.reset(
97  new FileGenerator(file, file_options, /* immutable_api = */ true));
98  }
99 
100  if (!file_generator->Validate(error)) {
101  return false;
102  }
103 
104  auto open_file = [context](const std::string& filename) {
105  return std::unique_ptr<io::ZeroCopyOutputStream>(context->Open(filename));
106  };
107  std::string package_dir = JavaPackageToDir(file_generator->java_package());
108  std::string kotlin_filename = package_dir;
109  kotlin_filename += file_generator->GetKotlinClassname();
110  kotlin_filename += ".kt";
111  all_files.push_back(kotlin_filename);
112  std::string info_full_path = kotlin_filename + ".pb.meta";
113  if (file_options.annotate_code) {
114  all_annotations.push_back(info_full_path);
115  }
116 
117  // Generate main kotlin file.
118  auto output = open_file(kotlin_filename);
119  GeneratedCodeInfo annotations;
121  &annotations);
122  io::Printer printer(
123  output.get(), '$',
124  file_options.annotate_code ? &annotation_collector : nullptr);
125 
126  file_generator->GenerateKotlinSiblings(package_dir, context, &all_files,
127  &all_annotations);
128 
129  if (file_options.annotate_code) {
130  auto info_output = open_file(info_full_path);
131  annotations.SerializeToZeroCopyStream(info_output.get());
132  }
133 
134  // Generate output list if requested.
135  if (!file_options.output_list_file.empty()) {
136  // Generate output list. This is just a simple text file placed in a
137  // deterministic location which lists the .kt files being generated.
138  auto srclist_raw_output = open_file(file_options.output_list_file);
139  io::Printer srclist_printer(srclist_raw_output.get(), '$');
140  for (auto& all_file : all_files) {
141  srclist_printer.Print("$filename$\n", "filename", all_file);
142  }
143  }
144 
145  if (!file_options.annotation_list_file.empty()) {
146  // Generate output list. This is just a simple text file placed in a
147  // deterministic location which lists the .kt files being generated.
148  auto annotation_list_raw_output =
149  open_file(file_options.annotation_list_file);
150  io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$');
151  for (auto& all_annotation : all_annotations) {
152  annotation_list_printer.Print("$filename$\n", "filename", all_annotation);
153  }
154  }
155 
156  return true;
157 }
158 
159 } // namespace java
160 } // namespace compiler
161 } // namespace protobuf
162 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.cc:113
google::protobuf::compiler::java::FileGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_file.h:68
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
java_kotlin_generator.h
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::compiler::java::Options
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:42
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::java::KotlinGenerator::KotlinGenerator
KotlinGenerator()
Definition: java_kotlin_generator.cc:44
google::protobuf::compiler::java::Options::annotate_code
bool annotate_code
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:59
google::protobuf::compiler::java::KotlinGenerator::~KotlinGenerator
~KotlinGenerator() override
Definition: java_kotlin_generator.cc:45
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::compiler::java::JavaPackageToDir
std::string JavaPackageToDir(std::string package_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_helpers.cc:263
google::protobuf::compiler::java::Options::annotation_list_file
std::string annotation_list_file
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:62
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::compiler::java::Options::generate_immutable_code
bool generate_immutable_code
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:51
google::protobuf::compiler::java::Options::generate_shared_code
bool generate_shared_code
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:53
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
setup.package_dir
package_dir
Definition: setup.py:553
google::protobuf::compiler::java::KotlinGenerator::GetSupportedFeatures
uint64_t GetSupportedFeatures() const override
Definition: java_kotlin_generator.cc:47
java
google::protobuf::compiler::java::KotlinGenerator::Generate
bool Generate(const FileDescriptor *file, const std::string &parameter, GeneratorContext *context, std::string *error) const override
Definition: java_kotlin_generator.cc:51
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.cc:101
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::java::Options::enforce_lite
bool enforce_lite
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:56
google::protobuf::io::AnnotationProtoCollector
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:76
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
GeneratedCodeInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.h:7087
google::protobuf::compiler::GeneratorContext
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/code_generator.h:119
google::protobuf::compiler::java::Options::output_list_file
std::string output_list_file
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/java/java_options.h:65
google_benchmark.option
option
Definition: third_party/benchmark/bindings/python/google_benchmark/__init__.py:115
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:23