PageRenderTime 112ms CodeModel.GetById 47ms app.highlight 61ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/java/java_file.cc

http://github.com/tomahawk-player/tomahawk
C++ | 428 lines | 301 code | 59 blank | 68 comment | 55 complexity | 0ed69056d26484abdb3057efd857af2e MD5 | raw file
  1// 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/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
 52namespace {
 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
 67    if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) {
 68      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
 85
 86}  // namespace
 87
 88FileGenerator::FileGenerator(const FileDescriptor* file)
 89  : file_(file),
 90    java_package_(FileJavaPackage(file)),
 91    classname_(FileClassName(file)) {
 92}
 93
 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"
139    "// source: $filename$\n"
140    "\n",
141    "filename", file_->name());
142  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
156  printer->Print(
157    "public static void registerAllExtensions(\n"
158    "    com.google.protobuf.ExtensionRegistry$lite$ registry) {\n",
159    "lite", HasDescriptorMethods(file_) ? "" : "Lite");
160
161  printer->Indent();
162
163  for (int i = 0; i < file_->extension_count(); i++) {
164    ExtensionGenerator(file_->extension(i)).GenerateRegistrationCode(printer);
165  }
166
167  for (int i = 0; i < file_->message_type_count(); i++) {
168    MessageGenerator(file_->message_type(i))
169      .GenerateExtensionRegistrationCode(printer);
170  }
171
172  printer->Outdent();
173  printer->Print(
174    "}\n");
175
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++) {
183      MessageGenerator messageGenerator(file_->message_type(i));
184      messageGenerator.GenerateInterface(printer);
185      messageGenerator.Generate(printer);
186    }
187    if (HasGenericServices(file_)) {
188      for (int i = 0; i < file_->service_count(); i++) {
189        ServiceGenerator(file_->service(i)).Generate(printer);
190      }
191    }
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
206  printer->Print("\n");
207
208  if (HasDescriptorMethods(file_)) {
209    GenerateEmbeddedDescriptor(printer);
210  } else {
211    printer->Print(
212      "static {\n");
213    printer->Indent();
214
215    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
221    printer->Outdent();
222    printer->Print(
223      "}\n");
224  }
225
226  printer->Print(
227    "\n"
228    "// @@protoc_insertion_point(outer_class_scope)\n");
229
230  printer->Outdent();
231  printer->Print("}\n");
232}
233
234void FileGenerator::GenerateEmbeddedDescriptor(io::Printer* printer) {
235  // 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);
247
248  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"
259    "  java.lang.String[] descriptorData = {\n");
260  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) {
266    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    }
275    printer->Print("\"$data$\"",
276      "data", CEscape(file_data.substr(i, kBytesPerLine)));
277  }
278
279  printer->Outdent();
280  printer->Print("\n};\n");
281
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  }
303  for (int i = 0; i < file_->extension_count(); i++) {
304    // TODO(kenton):  Reuse ExtensionGenerator objects?
305    ExtensionGenerator(file_->extension(i))
306        .GenerateNonNestedInitializationCode(printer);
307  }
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++) {
317      if (ShouldIncludeDependency(file_->dependency(i))) {
318        printer->Print(
319            "$dependency$.registerAllExtensions(registry);\n",
320            "dependency", ClassName(file_->dependency(i)));
321      }
322    }
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++) {
347    if (ShouldIncludeDependency(file_->dependency(i))) {
348      printer->Print(
349        "      $dependency$.getDescriptor(),\n",
350        "dependency", ClassName(file_->dependency(i)));
351    }
352  }
353
354  printer->Print(
355    "    }, assigner);\n");
356
357  printer->Outdent();
358  printer->Print(
359    "}\n");
360}
361
362template<typename GeneratorClass, typename DescriptorClass>
363static void GenerateSibling(const string& package_dir,
364                            const string& java_package,
365                            const DescriptorClass* descriptor,
366                            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";
371  file_list->push_back(filename);
372
373  scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
374  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
386  GeneratorClass generator(descriptor);
387  (generator.*pfn)(&printer);
388}
389
390void FileGenerator::GenerateSiblings(const string& package_dir,
391                                     GeneratorContext* context,
392                                     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),
397                                     context, file_list, "",
398                                     &EnumGenerator::Generate);
399    }
400    for (int i = 0; i < file_->message_type_count(); i++) {
401      GenerateSibling<MessageGenerator>(package_dir, java_package_,
402                                        file_->message_type(i),
403                                        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);
409    }
410    if (HasGenericServices(file_)) {
411      for (int i = 0; i < file_->service_count(); i++) {
412        GenerateSibling<ServiceGenerator>(package_dir, java_package_,
413                                          file_->service(i),
414                                          context, file_list, "",
415                                          &ServiceGenerator::Generate);
416      }
417    }
418  }
419}
420
421bool FileGenerator::ShouldIncludeDependency(const FileDescriptor* descriptor) {
422  return true;
423}
424
425}  // namespace java
426}  // namespace compiler
427}  // namespace protobuf
428}  // namespace google