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