java_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 
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
36 
37 
38 #include <memory>
39 
49 
51 
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace java {
56 
57 
60 
62  const std::string& parameter,
63  GeneratorContext* context,
64  std::string* error) const {
65  // -----------------------------------------------------------------
66  // parse generator options
67 
68  std::vector<std::pair<std::string, std::string> > options;
69  ParseGeneratorParameter(parameter, &options);
70  Options file_options;
71 
72  for (int i = 0; i < options.size(); i++) {
73  if (options[i].first == "output_list_file") {
74  file_options.output_list_file = options[i].second;
75  } else if (options[i].first == "immutable") {
76  file_options.generate_immutable_code = true;
77  } else if (options[i].first == "mutable") {
78  file_options.generate_mutable_code = true;
79  } else if (options[i].first == "shared") {
80  file_options.generate_shared_code = true;
81  } else if (options[i].first == "lite") {
82  // Note: Java Lite does not guarantee API/ABI stability. We may choose to
83  // break existing API in order to boost performance / reduce code size.
84  file_options.enforce_lite = true;
85  } else if (options[i].first == "annotate_code") {
86  file_options.annotate_code = true;
87  } else if (options[i].first == "annotation_list_file") {
88  file_options.annotation_list_file = options[i].second;
89  } else {
90  *error = "Unknown generator option: " + options[i].first;
91  return false;
92  }
93  }
94 
95  if (file_options.enforce_lite && file_options.generate_mutable_code) {
96  *error = "lite runtime generator option cannot be used with mutable API.";
97  return false;
98  }
99 
100  // By default we generate immutable code and shared code for immutable API.
101  if (!file_options.generate_immutable_code &&
102  !file_options.generate_mutable_code &&
103  !file_options.generate_shared_code) {
104  file_options.generate_immutable_code = true;
105  file_options.generate_shared_code = true;
106  }
107 
108  // -----------------------------------------------------------------
109 
110 
111  std::vector<std::string> all_files;
112  std::vector<std::string> all_annotations;
113 
114 
115  std::vector<FileGenerator*> file_generators;
116  if (file_options.generate_immutable_code) {
117  file_generators.push_back(new FileGenerator(file, file_options,
118  /* immutable = */ true));
119  }
120  if (file_options.generate_mutable_code) {
121  file_generators.push_back(new FileGenerator(file, file_options,
122  /* mutable = */ false));
123  }
124 
125  for (int i = 0; i < file_generators.size(); ++i) {
126  if (!file_generators[i]->Validate(error)) {
127  for (int j = 0; j < file_generators.size(); ++j) {
128  delete file_generators[j];
129  }
130  return false;
131  }
132  }
133 
134  for (int i = 0; i < file_generators.size(); ++i) {
135  FileGenerator* file_generator = file_generators[i];
136 
137  std::string package_dir = JavaPackageToDir(file_generator->java_package());
138 
139  std::string java_filename = package_dir;
140  java_filename += file_generator->classname();
141  java_filename += ".java";
142  all_files.push_back(java_filename);
143  std::string info_full_path = java_filename + ".pb.meta";
144  if (file_options.annotate_code) {
145  all_annotations.push_back(info_full_path);
146  }
147 
148  // Generate main java file.
149  std::unique_ptr<io::ZeroCopyOutputStream> output(
150  context->Open(java_filename));
151  GeneratedCodeInfo annotations;
153  &annotations);
154  io::Printer printer(
155  output.get(), '$',
156  file_options.annotate_code ? &annotation_collector : NULL);
157 
158  file_generator->Generate(&printer);
159 
160  // Generate sibling files.
161  file_generator->GenerateSiblings(package_dir, context, &all_files,
162  &all_annotations);
163 
164  if (file_options.annotate_code) {
165  std::unique_ptr<io::ZeroCopyOutputStream> info_output(
166  context->Open(info_full_path));
167  annotations.SerializeToZeroCopyStream(info_output.get());
168  }
169  }
170 
171 
172  for (int i = 0; i < file_generators.size(); ++i) {
173  delete file_generators[i];
174  }
175  file_generators.clear();
176 
177  // Generate output list if requested.
178  if (!file_options.output_list_file.empty()) {
179  // Generate output list. This is just a simple text file placed in a
180  // deterministic location which lists the .java files being generated.
181  std::unique_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
182  context->Open(file_options.output_list_file));
183  io::Printer srclist_printer(srclist_raw_output.get(), '$');
184  for (int i = 0; i < all_files.size(); i++) {
185  srclist_printer.Print("$filename$\n", "filename", all_files[i]);
186  }
187  }
188 
189  if (!file_options.annotation_list_file.empty()) {
190  // Generate output list. This is just a simple text file placed in a
191  // deterministic location which lists the .java files being generated.
192  std::unique_ptr<io::ZeroCopyOutputStream> annotation_list_raw_output(
193  context->Open(file_options.annotation_list_file));
194  io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$');
195  for (int i = 0; i < all_annotations.size(); i++) {
196  annotation_list_printer.Print("$filename$\n", "filename",
197  all_annotations[i]);
198  }
199  }
200 
201  return true;
202 }
203 
204 } // namespace java
205 } // namespace compiler
206 } // namespace protobuf
207 } // namespace google
zero_copy_stream.h
google::protobuf::compiler::java::FileGenerator::GenerateSiblings
void GenerateSiblings(const std::string &package_dir, GeneratorContext *generator_context, std::vector< std::string > *file_list, std::vector< std::string > *annotation_list)
Definition: java_file.cc:628
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::compiler::java::FileGenerator
Definition: java_file.h:68
java_name_resolver.h
java_helpers.h
NULL
NULL
Definition: test_security_zap.cpp:405
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
java_generator.h
google::protobuf::compiler::java::Options
Definition: java_options.h:42
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
google::protobuf::compiler::java::Options::generate_mutable_code
bool generate_mutable_code
Definition: java_options.h:52
google::protobuf::compiler::java::JavaGenerator::JavaGenerator
JavaGenerator()
Definition: java_generator.cc:58
java_options.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
java_shared_code_generator.h
google::protobuf::compiler::java::Options::annotate_code
bool annotate_code
Definition: java_options.h:59
error
Definition: cJSON.c:88
strutil.h
google::protobuf::compiler::java::JavaPackageToDir
std::string JavaPackageToDir(std::string package_name)
Definition: java_helpers.cc:265
java_generator_factory.h
google::protobuf::compiler::java::Options::annotation_list_file
std::string annotation_list_file
Definition: java_options.h:62
google::protobuf::compiler::java::FileGenerator::Generate
void Generate(io::Printer *printer)
Definition: java_file.cc:254
printer.h
google::protobuf::compiler::java::Options::generate_immutable_code
bool generate_immutable_code
Definition: java_options.h:51
google::protobuf::compiler::java::Options::generate_shared_code
bool generate_shared_code
Definition: java_options.h:53
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::compiler::java::FileGenerator::java_package
const std::string & java_package()
Definition: java_file.h:89
java
google::protobuf::compiler::java::JavaGenerator::Generate
bool Generate(const FileDescriptor *file, const std::string &parameter, GeneratorContext *context, std::string *error) const
Definition: java_generator.cc:61
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: code_generator.cc:101
first
GLint first
Definition: glcorearb.h:2830
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::java::Options::enforce_lite
bool enforce_lite
Definition: java_options.h:56
google::protobuf::io::AnnotationProtoCollector
Definition: printer.h:76
google::protobuf::compiler::java::FileGenerator::classname
const std::string & classname()
Definition: java_file.h:90
descriptor.pb.h
java_file.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
GeneratedCodeInfo
Definition: descriptor.pb.h:6355
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
google::protobuf::compiler::java::Options::output_list_file
std::string output_list_file
Definition: java_options.h:65
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::java::JavaGenerator::~JavaGenerator
~JavaGenerator()
Definition: java_generator.cc:59


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54