blob: a33eba1b0641cbaa1877dce5115fcb7309d013e1 [file] [log] [blame]
Ulas Kirazci23370232013-03-14 16:44:33 -07001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// http://code.google.com/p/protobuf/
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/javanano/javanano_params.h>
36#include <google/protobuf/compiler/javanano/javanano_generator.h>
37#include <google/protobuf/compiler/javanano/javanano_file.h>
38#include <google/protobuf/compiler/javanano/javanano_helpers.h>
39#include <google/protobuf/io/printer.h>
40#include <google/protobuf/io/zero_copy_stream.h>
41#include <google/protobuf/descriptor.pb.h>
42#include <google/protobuf/stubs/strutil.h>
43
44namespace google {
45namespace protobuf {
46namespace compiler {
47namespace javanano {
48
Max Caibc8eec32014-01-14 14:54:48 +000049namespace {
50
51string TrimString(const string& s) {
52 string::size_type start = s.find_first_not_of(" \n\r\t");
53 if (start == string::npos) {
54 return "";
55 }
56 string::size_type end = s.find_last_not_of(" \n\r\t") + 1;
57 return s.substr(start, end - start);
58}
59
60} // namespace
61
Ulas Kirazci23370232013-03-14 16:44:33 -070062void UpdateParamsRecursively(Params& params,
63 const FileDescriptor* file) {
64 // Add any parameters for this file
65 if (file->options().has_java_outer_classname()) {
66 params.set_java_outer_classname(
67 file->name(), file->options().java_outer_classname());
68 }
69 if (file->options().has_java_package()) {
Xiao Hang801e9202015-07-27 17:46:22 -070070 string result = file->options().java_package();
71 if (!file->options().javanano_use_deprecated_package()) {
72 if (!result.empty()) {
73 result += ".";
74 }
75 result += "nano";
76 }
Ulas Kirazci23370232013-03-14 16:44:33 -070077 params.set_java_package(
Xiao Hang801e9202015-07-27 17:46:22 -070078 file->name(), result);
Ulas Kirazci23370232013-03-14 16:44:33 -070079 }
Max Cai4c4d6392013-07-24 14:16:38 +010080 if (file->options().has_java_multiple_files()) {
81 params.set_java_multiple_files(
82 file->name(), file->options().java_multiple_files());
83 }
Ulas Kirazci23370232013-03-14 16:44:33 -070084
85 // Loop through all dependent files recursively
86 // adding dep
87 for (int i = 0; i < file->dependency_count(); i++) {
88 UpdateParamsRecursively(params, file->dependency(i));
89 }
90}
91
92JavaNanoGenerator::JavaNanoGenerator() {}
93JavaNanoGenerator::~JavaNanoGenerator() {}
94
95bool JavaNanoGenerator::Generate(const FileDescriptor* file,
96 const string& parameter,
Feng Xiaocd980d12014-11-19 15:58:54 -080097 GeneratorContext* output_directory,
Ulas Kirazci23370232013-03-14 16:44:33 -070098 string* error) const {
99 vector<pair<string, string> > options;
100
101 ParseGeneratorParameter(parameter, &options);
102
103 // -----------------------------------------------------------------
104 // parse generator options
105
106 // Name a file where we will write a list of generated file names, one
107 // per line.
108 string output_list_file;
109 Params params(file->name());
110
Ulas Kirazci23370232013-03-14 16:44:33 -0700111 // Update per file params
112 UpdateParamsRecursively(params, file);
113
114 // Replace any existing options with ones from command line
115 for (int i = 0; i < options.size(); i++) {
Max Caibc8eec32014-01-14 14:54:48 +0000116 string option_name = TrimString(options[i].first);
117 string option_value = TrimString(options[i].second);
118 if (option_name == "output_list_file") {
119 output_list_file = option_value;
120 } else if (option_name == "java_package") {
Ulas Kirazci23370232013-03-14 16:44:33 -0700121 vector<string> parts;
Max Caibc8eec32014-01-14 14:54:48 +0000122 SplitStringUsing(option_value, "|", &parts);
Ulas Kirazci23370232013-03-14 16:44:33 -0700123 if (parts.size() != 2) {
124 *error = "Bad java_package, expecting filename|PackageName found '"
Max Caibc8eec32014-01-14 14:54:48 +0000125 + option_value + "'";
Ulas Kirazci23370232013-03-14 16:44:33 -0700126 return false;
127 }
128 params.set_java_package(parts[0], parts[1]);
Max Caibc8eec32014-01-14 14:54:48 +0000129 } else if (option_name == "java_outer_classname") {
Ulas Kirazci23370232013-03-14 16:44:33 -0700130 vector<string> parts;
Max Caibc8eec32014-01-14 14:54:48 +0000131 SplitStringUsing(option_value, "|", &parts);
Ulas Kirazci23370232013-03-14 16:44:33 -0700132 if (parts.size() != 2) {
133 *error = "Bad java_outer_classname, "
134 "expecting filename|ClassName found '"
Max Caibc8eec32014-01-14 14:54:48 +0000135 + option_value + "'";
Ulas Kirazci23370232013-03-14 16:44:33 -0700136 return false;
137 }
138 params.set_java_outer_classname(parts[0], parts[1]);
Max Caibc8eec32014-01-14 14:54:48 +0000139 } else if (option_name == "store_unknown_fields") {
140 params.set_store_unknown_fields(option_value == "true");
141 } else if (option_name == "java_multiple_files") {
142 params.set_override_java_multiple_files(option_value == "true");
143 } else if (option_name == "java_nano_generate_has") {
144 params.set_generate_has(option_value == "true");
145 } else if (option_name == "enum_style") {
146 params.set_java_enum_style(option_value == "java");
147 } else if (option_name == "optional_field_style") {
148 params.set_optional_field_accessors(option_value == "accessors");
Brian Duffa8920682014-09-30 10:57:50 -0700149 params.set_use_reference_types_for_primitives(option_value == "reftypes"
Brian Duff7fbf6542014-10-01 12:55:00 -0700150 || option_value == "reftypes_compat_mode");
Brian Duffa8920682014-09-30 10:57:50 -0700151 params.set_reftypes_primitive_enums(
Brian Duff7fbf6542014-10-01 12:55:00 -0700152 option_value == "reftypes_compat_mode");
153 if (option_value == "reftypes_compat_mode") {
154 params.set_generate_clear(false);
155 }
Max Caibc8eec32014-01-14 14:54:48 +0000156 } else if (option_name == "generate_equals") {
157 params.set_generate_equals(option_value == "true");
Jie Daid9425a62014-04-21 14:29:03 -0700158 } else if (option_name == "ignore_services") {
159 params.set_ignore_services(option_value == "true");
Jeff Davidsonec4b1ce2014-04-22 23:25:53 -0700160 } else if (option_name == "parcelable_messages") {
161 params.set_parcelable_messages(option_value == "true");
Brian Duffd099a882014-10-01 13:33:40 -0700162 } else if (option_name == "generate_clone") {
163 params.set_generate_clone(option_value == "true");
Jeff Davidsonec19be22015-02-11 13:12:14 -0800164 } else if (option_name == "generate_intdefs") {
165 params.set_generate_intdefs(option_value == "true");
Brian Duffdac7e022015-02-21 15:22:43 -0800166 } else if (option_name == "generate_clear") {
167 params.set_generate_clear(option_value == "true");
Ulas Kirazci23370232013-03-14 16:44:33 -0700168 } else {
Max Caibc8eec32014-01-14 14:54:48 +0000169 *error = "Ignore unknown javanano generator option: " + option_name;
Ulas Kirazci23370232013-03-14 16:44:33 -0700170 }
171 }
172
Max Cai71766122013-09-18 17:10:31 +0100173 // Check illegal parameter combinations
Brian Duff10107cb2013-09-30 20:49:13 -0700174 // Note: the enum-like optional_field_style generator param ensures
175 // that we can never have illegal combinations of field styles
176 // (e.g. reftypes and accessors can't be on at the same time).
177 if (params.generate_has()
178 && (params.optional_field_accessors()
179 || params.use_reference_types_for_primitives())) {
Max Cai71766122013-09-18 17:10:31 +0100180 error->assign("java_nano_generate_has=true cannot be used in conjunction"
Brian Duff10107cb2013-09-30 20:49:13 -0700181 " with optional_field_style=accessors or optional_field_style=reftypes");
Max Cai71766122013-09-18 17:10:31 +0100182 return false;
183 }
184
Ulas Kirazci23370232013-03-14 16:44:33 -0700185 // -----------------------------------------------------------------
186
187 FileGenerator file_generator(file, params);
188 if (!file_generator.Validate(error)) {
189 return false;
190 }
191
192 string package_dir =
193 StringReplace(file_generator.java_package(), ".", "/", true);
194 if (!package_dir.empty()) package_dir += "/";
195
196 vector<string> all_files;
197
Max Cai06eed372013-07-29 17:20:50 +0100198 if (IsOuterClassNeeded(params, file)) {
199 string java_filename = package_dir;
200 java_filename += file_generator.classname();
201 java_filename += ".java";
202 all_files.push_back(java_filename);
Ulas Kirazci23370232013-03-14 16:44:33 -0700203
Max Cai06eed372013-07-29 17:20:50 +0100204 // Generate main java file.
205 scoped_ptr<io::ZeroCopyOutputStream> output(
206 output_directory->Open(java_filename));
207 io::Printer printer(output.get(), '$');
208 file_generator.Generate(&printer);
209 }
Ulas Kirazci23370232013-03-14 16:44:33 -0700210
211 // Generate sibling files.
212 file_generator.GenerateSiblings(package_dir, output_directory, &all_files);
213
214 // Generate output list if requested.
215 if (!output_list_file.empty()) {
216 // Generate output list. This is just a simple text file placed in a
217 // deterministic location which lists the .java files being generated.
218 scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
219 output_directory->Open(output_list_file));
220 io::Printer srclist_printer(srclist_raw_output.get(), '$');
221 for (int i = 0; i < all_files.size(); i++) {
222 srclist_printer.Print("$filename$\n", "filename", all_files[i]);
223 }
224 }
225
226 return true;
227}
228
229} // namespace java
230} // namespace compiler
231} // namespace protobuf
232} // namespace google