blob: 8968069f4f0c00a9275a29f51e04221dff25a901 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00003// http://code.google.com/p/protobuf/
4//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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_file.h>
36#include <google/protobuf/compiler/java/java_enum.h>
37#include <google/protobuf/compiler/java/java_service.h>
38#include <google/protobuf/compiler/java/java_extension.h>
39#include <google/protobuf/compiler/java/java_helpers.h>
40#include <google/protobuf/compiler/java/java_message.h>
41#include <google/protobuf/compiler/code_generator.h>
42#include <google/protobuf/io/printer.h>
43#include <google/protobuf/io/zero_copy_stream.h>
44#include <google/protobuf/descriptor.pb.h>
45#include <google/protobuf/stubs/strutil.h>
46
47namespace google {
48namespace protobuf {
49namespace compiler {
50namespace java {
51
kenton@google.com24bf56f2008-09-24 20:31:01 +000052namespace {
53
54// Recursively searches the given message to see if it contains any extensions.
55bool UsesExtensions(const Message& message) {
56 const Reflection* reflection = message.GetReflection();
57
58 // We conservatively assume that unknown fields are extensions.
59 if (reflection->GetUnknownFields(message).field_count() > 0) return true;
60
61 vector<const FieldDescriptor*> fields;
62 reflection->ListFields(message, &fields);
63
64 for (int i = 0; i < fields.size(); i++) {
65 if (fields[i]->is_extension()) return true;
66
kenton@google.comfccb1462009-12-18 02:11:36 +000067 if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) {
kenton@google.com24bf56f2008-09-24 20:31:01 +000068 if (fields[i]->is_repeated()) {
69 int size = reflection->FieldSize(message, fields[i]);
70 for (int j = 0; j < size; j++) {
71 const Message& sub_message =
72 reflection->GetRepeatedMessage(message, fields[i], j);
73 if (UsesExtensions(sub_message)) return true;
74 }
75 } else {
76 const Message& sub_message = reflection->GetMessage(message, fields[i]);
77 if (UsesExtensions(sub_message)) return true;
78 }
79 }
80 }
81
82 return false;
83}
84
kenton@google.comfccb1462009-12-18 02:11:36 +000085
kenton@google.com24bf56f2008-09-24 20:31:01 +000086} // namespace
87
temporal40ee5512008-07-10 02:12:20 +000088FileGenerator::FileGenerator(const FileDescriptor* file)
89 : file_(file),
90 java_package_(FileJavaPackage(file)),
liujisi@google.com33165fe2010-11-02 13:14:58 +000091 classname_(FileClassName(file)) {
92}
temporal40ee5512008-07-10 02:12:20 +000093
94FileGenerator::~FileGenerator() {}
95
96bool FileGenerator::Validate(string* error) {
97 // Check that no class name matches the file's class name. This is a common
98 // problem that leads to Java compile errors that can be hard to understand.
99 // It's especially bad when using the java_multiple_files, since we would
100 // end up overwriting the outer class with one of the inner ones.
101
102 bool found_conflict = false;
103 for (int i = 0; i < file_->enum_type_count() && !found_conflict; i++) {
104 if (file_->enum_type(i)->name() == classname_) {
105 found_conflict = true;
106 }
107 }
108 for (int i = 0; i < file_->message_type_count() && !found_conflict; i++) {
109 if (file_->message_type(i)->name() == classname_) {
110 found_conflict = true;
111 }
112 }
113 for (int i = 0; i < file_->service_count() && !found_conflict; i++) {
114 if (file_->service(i)->name() == classname_) {
115 found_conflict = true;
116 }
117 }
118
119 if (found_conflict) {
120 error->assign(file_->name());
121 error->append(
122 ": Cannot generate Java output because the file's outer class name, \"");
123 error->append(classname_);
124 error->append(
125 "\", matches the name of one of the types declared inside it. "
126 "Please either rename the type or use the java_outer_classname "
127 "option to specify a different outer class name for the .proto file.");
128 return false;
129 }
130
131 return true;
132}
133
134void FileGenerator::Generate(io::Printer* printer) {
135 // We don't import anything because we refer to all classes by their
136 // fully-qualified names in the generated source.
137 printer->Print(
138 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
kenton@google.comfccb1462009-12-18 02:11:36 +0000139 "// source: $filename$\n"
140 "\n",
141 "filename", file_->name());
temporal40ee5512008-07-10 02:12:20 +0000142 if (!java_package_.empty()) {
143 printer->Print(
144 "package $package$;\n"
145 "\n",
146 "package", java_package_);
147 }
148 printer->Print(
149 "public final class $classname$ {\n"
150 " private $classname$() {}\n",
151 "classname", classname_);
152 printer->Indent();
153
154 // -----------------------------------------------------------------
155
temporal40ee5512008-07-10 02:12:20 +0000156 printer->Print(
kenton@google.com24bf56f2008-09-24 20:31:01 +0000157 "public static void registerAllExtensions(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000158 " com.google.protobuf.ExtensionRegistry$lite$ registry) {\n",
159 "lite", HasDescriptorMethods(file_) ? "" : "Lite");
160
temporal40ee5512008-07-10 02:12:20 +0000161 printer->Indent();
162
kenton@google.com24bf56f2008-09-24 20:31:01 +0000163 for (int i = 0; i < file_->extension_count(); i++) {
164 ExtensionGenerator(file_->extension(i)).GenerateRegistrationCode(printer);
temporal40ee5512008-07-10 02:12:20 +0000165 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000166
167 for (int i = 0; i < file_->message_type_count(); i++) {
168 MessageGenerator(file_->message_type(i))
169 .GenerateExtensionRegistrationCode(printer);
170 }
temporal40ee5512008-07-10 02:12:20 +0000171
172 printer->Outdent();
173 printer->Print(
kenton@google.com24bf56f2008-09-24 20:31:01 +0000174 "}\n");
temporal40ee5512008-07-10 02:12:20 +0000175
176 // -----------------------------------------------------------------
177
178 if (!file_->options().java_multiple_files()) {
179 for (int i = 0; i < file_->enum_type_count(); i++) {
180 EnumGenerator(file_->enum_type(i)).Generate(printer);
181 }
182 for (int i = 0; i < file_->message_type_count(); i++) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000183 MessageGenerator messageGenerator(file_->message_type(i));
184 messageGenerator.GenerateInterface(printer);
185 messageGenerator.Generate(printer);
temporal40ee5512008-07-10 02:12:20 +0000186 }
kenton@google.comfccb1462009-12-18 02:11:36 +0000187 if (HasGenericServices(file_)) {
188 for (int i = 0; i < file_->service_count(); i++) {
189 ServiceGenerator(file_->service(i)).Generate(printer);
190 }
temporal40ee5512008-07-10 02:12:20 +0000191 }
192 }
193
194 // Extensions must be generated in the outer class since they are values,
195 // not classes.
196 for (int i = 0; i < file_->extension_count(); i++) {
197 ExtensionGenerator(file_->extension(i)).Generate(printer);
198 }
199
200 // Static variables.
201 for (int i = 0; i < file_->message_type_count(); i++) {
202 // TODO(kenton): Reuse MessageGenerator objects?
203 MessageGenerator(file_->message_type(i)).GenerateStaticVariables(printer);
204 }
205
kenton@google.com24bf56f2008-09-24 20:31:01 +0000206 printer->Print("\n");
207
kenton@google.com80b1d622009-07-29 01:13:20 +0000208 if (HasDescriptorMethods(file_)) {
209 GenerateEmbeddedDescriptor(printer);
210 } else {
211 printer->Print(
212 "static {\n");
213 printer->Indent();
kenton@google.com24bf56f2008-09-24 20:31:01 +0000214
kenton@google.com80b1d622009-07-29 01:13:20 +0000215 for (int i = 0; i < file_->message_type_count(); i++) {
216 // TODO(kenton): Reuse MessageGenerator objects?
217 MessageGenerator(file_->message_type(i))
218 .GenerateStaticVariableInitializers(printer);
219 }
220
kenton@google.com80b1d622009-07-29 01:13:20 +0000221 printer->Outdent();
222 printer->Print(
223 "}\n");
224 }
225
kenton@google.comfccb1462009-12-18 02:11:36 +0000226 printer->Print(
227 "\n"
228 "// @@protoc_insertion_point(outer_class_scope)\n");
229
kenton@google.com80b1d622009-07-29 01:13:20 +0000230 printer->Outdent();
231 printer->Print("}\n");
232}
233
234void FileGenerator::GenerateEmbeddedDescriptor(io::Printer* printer) {
kenton@google.com24bf56f2008-09-24 20:31:01 +0000235 // Embed the descriptor. We simply serialize the entire FileDescriptorProto
236 // and embed it as a string literal, which is parsed and built into real
237 // descriptors at initialization time. We unfortunately have to put it in
238 // a string literal, not a byte array, because apparently using a literal
239 // byte array causes the Java compiler to generate *instructions* to
240 // initialize each and every byte of the array, e.g. as if you typed:
241 // b[0] = 123; b[1] = 456; b[2] = 789;
242 // This makes huge bytecode files and can easily hit the compiler's internal
243 // code size limits (error "code to large"). String literals are apparently
244 // embedded raw, which is what we want.
245 FileDescriptorProto file_proto;
246 file_->CopyTo(&file_proto);
kenton@google.comfccb1462009-12-18 02:11:36 +0000247
kenton@google.com24bf56f2008-09-24 20:31:01 +0000248 string file_data;
249 file_proto.SerializeToString(&file_data);
250
251 printer->Print(
252 "public static com.google.protobuf.Descriptors.FileDescriptor\n"
253 " getDescriptor() {\n"
254 " return descriptor;\n"
255 "}\n"
256 "private static com.google.protobuf.Descriptors.FileDescriptor\n"
257 " descriptor;\n"
258 "static {\n"
kenton@google.com68996fc2009-08-07 21:02:02 +0000259 " java.lang.String[] descriptorData = {\n");
kenton@google.com24bf56f2008-09-24 20:31:01 +0000260 printer->Indent();
261 printer->Indent();
262
263 // Only write 40 bytes per line.
264 static const int kBytesPerLine = 40;
265 for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
kenton@google.com68996fc2009-08-07 21:02:02 +0000266 if (i > 0) {
267 // Every 400 lines, start a new string literal, in order to avoid the
268 // 64k length limit.
269 if (i % 400 == 0) {
270 printer->Print(",\n");
271 } else {
272 printer->Print(" +\n");
273 }
274 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000275 printer->Print("\"$data$\"",
276 "data", CEscape(file_data.substr(i, kBytesPerLine)));
277 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000278
279 printer->Outdent();
kenton@google.com68996fc2009-08-07 21:02:02 +0000280 printer->Print("\n};\n");
kenton@google.com24bf56f2008-09-24 20:31:01 +0000281
282 // -----------------------------------------------------------------
283 // Create the InternalDescriptorAssigner.
284
285 printer->Print(
286 "com.google.protobuf.Descriptors.FileDescriptor."
287 "InternalDescriptorAssigner assigner =\n"
288 " new com.google.protobuf.Descriptors.FileDescriptor."
289 "InternalDescriptorAssigner() {\n"
290 " public com.google.protobuf.ExtensionRegistry assignDescriptors(\n"
291 " com.google.protobuf.Descriptors.FileDescriptor root) {\n"
292 " descriptor = root;\n");
293
294 printer->Indent();
295 printer->Indent();
296 printer->Indent();
297
298 for (int i = 0; i < file_->message_type_count(); i++) {
299 // TODO(kenton): Reuse MessageGenerator objects?
300 MessageGenerator(file_->message_type(i))
301 .GenerateStaticVariableInitializers(printer);
302 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000303 for (int i = 0; i < file_->extension_count(); i++) {
304 // TODO(kenton): Reuse ExtensionGenerator objects?
305 ExtensionGenerator(file_->extension(i))
liujisi@google.com33165fe2010-11-02 13:14:58 +0000306 .GenerateNonNestedInitializationCode(printer);
kenton@google.com24bf56f2008-09-24 20:31:01 +0000307 }
308
309 if (UsesExtensions(file_proto)) {
310 // Must construct an ExtensionRegistry containing all possible extensions
311 // and return it.
312 printer->Print(
313 "com.google.protobuf.ExtensionRegistry registry =\n"
314 " com.google.protobuf.ExtensionRegistry.newInstance();\n"
315 "registerAllExtensions(registry);\n");
316 for (int i = 0; i < file_->dependency_count(); i++) {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000317 if (ShouldIncludeDependency(file_->dependency(i))) {
318 printer->Print(
319 "$dependency$.registerAllExtensions(registry);\n",
320 "dependency", ClassName(file_->dependency(i)));
321 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000322 }
323 printer->Print(
324 "return registry;\n");
325 } else {
326 printer->Print(
327 "return null;\n");
328 }
329
330 printer->Outdent();
331 printer->Outdent();
332 printer->Outdent();
333
334 printer->Print(
335 " }\n"
336 " };\n");
337
338 // -----------------------------------------------------------------
339 // Invoke internalBuildGeneratedFileFrom() to build the file.
340
341 printer->Print(
342 "com.google.protobuf.Descriptors.FileDescriptor\n"
343 " .internalBuildGeneratedFileFrom(descriptorData,\n"
344 " new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
345
346 for (int i = 0; i < file_->dependency_count(); i++) {
kenton@google.comfccb1462009-12-18 02:11:36 +0000347 if (ShouldIncludeDependency(file_->dependency(i))) {
348 printer->Print(
349 " $dependency$.getDescriptor(),\n",
350 "dependency", ClassName(file_->dependency(i)));
351 }
kenton@google.com24bf56f2008-09-24 20:31:01 +0000352 }
353
354 printer->Print(
355 " }, assigner);\n");
356
357 printer->Outdent();
358 printer->Print(
359 "}\n");
temporal40ee5512008-07-10 02:12:20 +0000360}
361
362template<typename GeneratorClass, typename DescriptorClass>
363static void GenerateSibling(const string& package_dir,
364 const string& java_package,
365 const DescriptorClass* descriptor,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000366 GeneratorContext* context,
367 vector<string>* file_list,
368 const string& name_suffix,
369 void (GeneratorClass::*pfn)(io::Printer* printer)) {
370 string filename = package_dir + descriptor->name() + name_suffix + ".java";
temporal40ee5512008-07-10 02:12:20 +0000371 file_list->push_back(filename);
372
liujisi@google.com33165fe2010-11-02 13:14:58 +0000373 scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
temporal40ee5512008-07-10 02:12:20 +0000374 io::Printer printer(output.get(), '$');
375
376 printer.Print(
377 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
378 "\n");
379 if (!java_package.empty()) {
380 printer.Print(
381 "package $package$;\n"
382 "\n",
383 "package", java_package);
384 }
385
liujisi@google.com33165fe2010-11-02 13:14:58 +0000386 GeneratorClass generator(descriptor);
387 (generator.*pfn)(&printer);
temporal40ee5512008-07-10 02:12:20 +0000388}
389
390void FileGenerator::GenerateSiblings(const string& package_dir,
liujisi@google.com33165fe2010-11-02 13:14:58 +0000391 GeneratorContext* context,
temporal40ee5512008-07-10 02:12:20 +0000392 vector<string>* file_list) {
393 if (file_->options().java_multiple_files()) {
394 for (int i = 0; i < file_->enum_type_count(); i++) {
395 GenerateSibling<EnumGenerator>(package_dir, java_package_,
396 file_->enum_type(i),
liujisi@google.com33165fe2010-11-02 13:14:58 +0000397 context, file_list, "",
398 &EnumGenerator::Generate);
temporal40ee5512008-07-10 02:12:20 +0000399 }
400 for (int i = 0; i < file_->message_type_count(); i++) {
401 GenerateSibling<MessageGenerator>(package_dir, java_package_,
402 file_->message_type(i),
liujisi@google.com33165fe2010-11-02 13:14:58 +0000403 context, file_list, "OrBuilder",
404 &MessageGenerator::GenerateInterface);
405 GenerateSibling<MessageGenerator>(package_dir, java_package_,
406 file_->message_type(i),
407 context, file_list, "",
408 &MessageGenerator::Generate);
temporal40ee5512008-07-10 02:12:20 +0000409 }
kenton@google.comfccb1462009-12-18 02:11:36 +0000410 if (HasGenericServices(file_)) {
411 for (int i = 0; i < file_->service_count(); i++) {
412 GenerateSibling<ServiceGenerator>(package_dir, java_package_,
413 file_->service(i),
liujisi@google.com33165fe2010-11-02 13:14:58 +0000414 context, file_list, "",
415 &ServiceGenerator::Generate);
kenton@google.comfccb1462009-12-18 02:11:36 +0000416 }
temporal40ee5512008-07-10 02:12:20 +0000417 }
418 }
419}
420
kenton@google.comfccb1462009-12-18 02:11:36 +0000421bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor) {
422 return true;
423}
424
temporal40ee5512008-07-10 02:12:20 +0000425} // namespace java
426} // namespace compiler
427} // namespace protobuf
428} // namespace google