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
GetSupportedFeatures() const61 uint64_t JavaGenerator::GetSupportedFeatures() const {
62 return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL;
63 }
64
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const65 bool JavaGenerator::Generate(const FileDescriptor* file,
66 const std::string& parameter,
67 GeneratorContext* context,
68 std::string* error) const {
69 // -----------------------------------------------------------------
70 // parse generator options
71
72 std::vector<std::pair<std::string, std::string> > options;
73 ParseGeneratorParameter(parameter, &options);
74 Options file_options;
75
76 for (int i = 0; i < options.size(); i++) {
77 if (options[i].first == "output_list_file") {
78 file_options.output_list_file = options[i].second;
79 } else if (options[i].first == "immutable") {
80 file_options.generate_immutable_code = true;
81 } else if (options[i].first == "mutable") {
82 file_options.generate_mutable_code = true;
83 } else if (options[i].first == "shared") {
84 file_options.generate_shared_code = true;
85 } else if (options[i].first == "lite") {
86 // Note: Java Lite does not guarantee API/ABI stability. We may choose to
87 // break existing API in order to boost performance / reduce code size.
88 file_options.enforce_lite = true;
89 } else if (options[i].first == "annotate_code") {
90 file_options.annotate_code = true;
91 } else if (options[i].first == "annotation_list_file") {
92 file_options.annotation_list_file = options[i].second;
93 } else {
94 *error = "Unknown generator option: " + options[i].first;
95 return false;
96 }
97 }
98
99 if (file_options.enforce_lite && file_options.generate_mutable_code) {
100 *error = "lite runtime generator option cannot be used with mutable API.";
101 return false;
102 }
103
104 // By default we generate immutable code and shared code for immutable API.
105 if (!file_options.generate_immutable_code &&
106 !file_options.generate_mutable_code &&
107 !file_options.generate_shared_code) {
108 file_options.generate_immutable_code = true;
109 file_options.generate_shared_code = true;
110 }
111
112 // -----------------------------------------------------------------
113
114
115 std::vector<std::string> all_files;
116 std::vector<std::string> all_annotations;
117
118
119 std::vector<FileGenerator*> file_generators;
120 if (file_options.generate_immutable_code) {
121 file_generators.push_back(new FileGenerator(file, file_options,
122 /* immutable = */ true));
123 }
124 if (file_options.generate_mutable_code) {
125 file_generators.push_back(new FileGenerator(file, file_options,
126 /* mutable = */ false));
127 }
128
129 for (int i = 0; i < file_generators.size(); ++i) {
130 if (!file_generators[i]->Validate(error)) {
131 for (int j = 0; j < file_generators.size(); ++j) {
132 delete file_generators[j];
133 }
134 return false;
135 }
136 }
137
138 for (int i = 0; i < file_generators.size(); ++i) {
139 FileGenerator* file_generator = file_generators[i];
140
141 std::string package_dir = JavaPackageToDir(file_generator->java_package());
142
143 std::string java_filename = package_dir;
144 java_filename += file_generator->classname();
145 java_filename += ".java";
146 all_files.push_back(java_filename);
147 std::string info_full_path = java_filename + ".pb.meta";
148 if (file_options.annotate_code) {
149 all_annotations.push_back(info_full_path);
150 }
151
152 // Generate main java file.
153 std::unique_ptr<io::ZeroCopyOutputStream> output(
154 context->Open(java_filename));
155 GeneratedCodeInfo annotations;
156 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector(
157 &annotations);
158 io::Printer printer(
159 output.get(), '$',
160 file_options.annotate_code ? &annotation_collector : NULL);
161
162 file_generator->Generate(&printer);
163
164 // Generate sibling files.
165 file_generator->GenerateSiblings(package_dir, context, &all_files,
166 &all_annotations);
167
168 if (file_options.annotate_code) {
169 std::unique_ptr<io::ZeroCopyOutputStream> info_output(
170 context->Open(info_full_path));
171 annotations.SerializeToZeroCopyStream(info_output.get());
172 }
173 }
174
175
176 for (int i = 0; i < file_generators.size(); ++i) {
177 delete file_generators[i];
178 }
179 file_generators.clear();
180
181 // Generate output list if requested.
182 if (!file_options.output_list_file.empty()) {
183 // Generate output list. This is just a simple text file placed in a
184 // deterministic location which lists the .java files being generated.
185 std::unique_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
186 context->Open(file_options.output_list_file));
187 io::Printer srclist_printer(srclist_raw_output.get(), '$');
188 for (int i = 0; i < all_files.size(); i++) {
189 srclist_printer.Print("$filename$\n", "filename", all_files[i]);
190 }
191 }
192
193 if (!file_options.annotation_list_file.empty()) {
194 // Generate output list. This is just a simple text file placed in a
195 // deterministic location which lists the .java files being generated.
196 std::unique_ptr<io::ZeroCopyOutputStream> annotation_list_raw_output(
197 context->Open(file_options.annotation_list_file));
198 io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$');
199 for (int i = 0; i < all_annotations.size(); i++) {
200 annotation_list_printer.Print("$filename$\n", "filename",
201 all_annotations[i]);
202 }
203 }
204
205 return true;
206 }
207
208 } // namespace java
209 } // namespace compiler
210 } // namespace protobuf
211 } // namespace google
212