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
35 #include <google/protobuf/compiler/java/java_generator.h>
36
37
38 #include <memory>
39
40 #include <google/protobuf/compiler/java/java_file.h>
41 #include <google/protobuf/compiler/java/java_generator_factory.h>
42 #include <google/protobuf/compiler/java/java_helpers.h>
43 #include <google/protobuf/compiler/java/java_name_resolver.h>
44 #include <google/protobuf/compiler/java/java_options.h>
45 #include <google/protobuf/compiler/java/java_shared_code_generator.h>
46 #include <google/protobuf/descriptor.pb.h>
47 #include <google/protobuf/io/printer.h>
48 #include <google/protobuf/io/zero_copy_stream.h>
49
50 #include <google/protobuf/stubs/strutil.h>
51
52 namespace google {
53 namespace protobuf {
54 namespace compiler {
55 namespace java {
56
57
JavaGenerator()58 JavaGenerator::JavaGenerator() {}
~JavaGenerator()59 JavaGenerator::~JavaGenerator() {}
60
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const61 bool JavaGenerator::Generate(const FileDescriptor* file,
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;
152 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
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
208