OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // appended in to the extensions parameter. | 83 // appended in to the extensions parameter. |
84 // Returns false when there are unknown fields, in which case the data in the | 84 // Returns false when there are unknown fields, in which case the data in the |
85 // extensions output parameter is not reliable and should be discarded. | 85 // extensions output parameter is not reliable and should be discarded. |
86 bool CollectExtensions(const Message& message, | 86 bool CollectExtensions(const Message& message, |
87 FieldDescriptorSet* extensions) { | 87 FieldDescriptorSet* extensions) { |
88 const Reflection* reflection = message.GetReflection(); | 88 const Reflection* reflection = message.GetReflection(); |
89 | 89 |
90 // There are unknown fields that could be extensions, thus this call fails. | 90 // There are unknown fields that could be extensions, thus this call fails. |
91 if (reflection->GetUnknownFields(message).field_count() > 0) return false; | 91 if (reflection->GetUnknownFields(message).field_count() > 0) return false; |
92 | 92 |
93 vector<const FieldDescriptor*> fields; | 93 std::vector<const FieldDescriptor*> fields; |
94 reflection->ListFields(message, &fields); | 94 reflection->ListFields(message, &fields); |
95 | 95 |
96 for (int i = 0; i < fields.size(); i++) { | 96 for (int i = 0; i < fields.size(); i++) { |
97 if (fields[i]->is_extension()) extensions->insert(fields[i]); | 97 if (fields[i]->is_extension()) extensions->insert(fields[i]); |
98 | 98 |
99 if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) { | 99 if (GetJavaType(fields[i]) == JAVATYPE_MESSAGE) { |
100 if (fields[i]->is_repeated()) { | 100 if (fields[i]->is_repeated()) { |
101 int size = reflection->FieldSize(message, fields[i]); | 101 int size = reflection->FieldSize(message, fields[i]); |
102 for (int j = 0; j < size; j++) { | 102 for (int j = 0; j < size; j++) { |
103 const Message& sub_message = | 103 const Message& sub_message = |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 if ((*bytecode_estimate) > bytesPerMethod) { | 182 if ((*bytecode_estimate) > bytesPerMethod) { |
183 ++(*method_num); | 183 ++(*method_num); |
184 printer->Print(chain_statement, "method_num", SimpleItoa(*method_num)); | 184 printer->Print(chain_statement, "method_num", SimpleItoa(*method_num)); |
185 printer->Outdent(); | 185 printer->Outdent(); |
186 printer->Print("}\n"); | 186 printer->Print("}\n"); |
187 printer->Print(method_decl, "method_num", SimpleItoa(*method_num)); | 187 printer->Print(method_decl, "method_num", SimpleItoa(*method_num)); |
188 printer->Indent(); | 188 printer->Indent(); |
189 *bytecode_estimate = 0; | 189 *bytecode_estimate = 0; |
190 } | 190 } |
191 } | 191 } |
192 | |
193 | |
194 } // namespace | 192 } // namespace |
195 | 193 |
196 FileGenerator::FileGenerator(const FileDescriptor* file, bool immutable_api, | 194 FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, |
197 bool enforce_lite) | 195 bool immutable_api) |
198 : file_(file), | 196 : file_(file), |
199 java_package_(FileJavaPackage(file, immutable_api)), | 197 java_package_(FileJavaPackage(file, immutable_api)), |
200 message_generators_( | 198 message_generators_( |
201 new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_
count()]), | 199 new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_
count()]), |
202 extension_generators_( | 200 extension_generators_( |
203 new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_c
ount()]), | 201 new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_c
ount()]), |
204 context_(new Context(file)), | 202 context_(new Context(file, options)), |
205 name_resolver_(context_->GetNameResolver()), | 203 name_resolver_(context_->GetNameResolver()), |
| 204 options_(options), |
206 immutable_api_(immutable_api) { | 205 immutable_api_(immutable_api) { |
207 classname_ = name_resolver_->GetFileClassName(file, immutable_api); | 206 classname_ = name_resolver_->GetFileClassName(file, immutable_api); |
208 context_->SetEnforceLite(enforce_lite); | |
209 generator_factory_.reset( | 207 generator_factory_.reset( |
210 new ImmutableGeneratorFactory(context_.get())); | 208 new ImmutableGeneratorFactory(context_.get())); |
211 for (int i = 0; i < file_->message_type_count(); ++i) { | 209 for (int i = 0; i < file_->message_type_count(); ++i) { |
212 message_generators_[i].reset( | 210 message_generators_[i].reset( |
213 generator_factory_->NewMessageGenerator(file_->message_type(i))); | 211 generator_factory_->NewMessageGenerator(file_->message_type(i))); |
214 } | 212 } |
215 for (int i = 0; i < file_->extension_count(); ++i) { | 213 for (int i = 0; i < file_->extension_count(); ++i) { |
216 extension_generators_[i].reset( | 214 extension_generators_[i].reset( |
217 generator_factory_->NewExtensionGenerator(file_->extension(i))); | 215 generator_factory_->NewExtensionGenerator(file_->extension(i))); |
218 } | 216 } |
(...skipping 27 matching lines...) Expand all Loading... |
246 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 244 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
247 "// source: $filename$\n" | 245 "// source: $filename$\n" |
248 "\n", | 246 "\n", |
249 "filename", file_->name()); | 247 "filename", file_->name()); |
250 if (!java_package_.empty()) { | 248 if (!java_package_.empty()) { |
251 printer->Print( | 249 printer->Print( |
252 "package $package$;\n" | 250 "package $package$;\n" |
253 "\n", | 251 "\n", |
254 "package", java_package_); | 252 "package", java_package_); |
255 } | 253 } |
| 254 PrintGeneratedAnnotation( |
| 255 printer, '$', options_.annotate_code ? classname_ + ".java.pb.meta" : ""); |
256 printer->Print( | 256 printer->Print( |
257 "public final class $classname$ {\n" | 257 "public final class $classname$ {\n" |
258 " private $classname$() {}\n", | 258 " private $ctor$() {}\n", |
259 "classname", classname_); | 259 "classname", classname_, "ctor", classname_); |
| 260 printer->Annotate("classname", file_->name()); |
260 printer->Indent(); | 261 printer->Indent(); |
261 | 262 |
262 // ----------------------------------------------------------------- | 263 // ----------------------------------------------------------------- |
263 | 264 |
264 printer->Print( | 265 printer->Print( |
265 "public static void registerAllExtensions(\n" | 266 "public static void registerAllExtensions(\n" |
266 " com.google.protobuf.ExtensionRegistry$lite$ registry) {\n", | 267 " com.google.protobuf.ExtensionRegistryLite registry) {\n"); |
267 "lite", | |
268 HasDescriptorMethods(file_, context_->EnforceLite()) ? "" : "Lite"); | |
269 | 268 |
270 printer->Indent(); | 269 printer->Indent(); |
271 | 270 |
272 for (int i = 0; i < file_->extension_count(); i++) { | 271 for (int i = 0; i < file_->extension_count(); i++) { |
273 extension_generators_[i]->GenerateRegistrationCode(printer); | 272 extension_generators_[i]->GenerateRegistrationCode(printer); |
274 } | 273 } |
275 | 274 |
276 for (int i = 0; i < file_->message_type_count(); i++) { | 275 for (int i = 0; i < file_->message_type_count(); i++) { |
277 message_generators_[i]->GenerateExtensionRegistrationCode(printer); | 276 message_generators_[i]->GenerateExtensionRegistrationCode(printer); |
278 } | 277 } |
279 | 278 |
280 printer->Outdent(); | 279 printer->Outdent(); |
281 printer->Print( | 280 printer->Print( |
282 "}\n"); | 281 "}\n"); |
| 282 if (HasDescriptorMethods(file_, context_->EnforceLite())) { |
| 283 // Overload registerAllExtensions for the non-lite usage to |
| 284 // redundantly maintain the original signature (this is |
| 285 // redundant because ExtensionRegistryLite now invokes |
| 286 // ExtensionRegistry in the non-lite usage). Intent is |
| 287 // to remove this in the future. |
| 288 printer->Print( |
| 289 "\n" |
| 290 "public static void registerAllExtensions(\n" |
| 291 " com.google.protobuf.ExtensionRegistry registry) {\n" |
| 292 " registerAllExtensions(\n" |
| 293 " (com.google.protobuf.ExtensionRegistryLite) registry);\n" |
| 294 "}\n"); |
| 295 } |
283 | 296 |
284 // ----------------------------------------------------------------- | 297 // ----------------------------------------------------------------- |
285 | 298 |
286 if (!MultipleJavaFiles(file_, immutable_api_)) { | 299 if (!MultipleJavaFiles(file_, immutable_api_)) { |
287 for (int i = 0; i < file_->enum_type_count(); i++) { | 300 for (int i = 0; i < file_->enum_type_count(); i++) { |
288 if (HasDescriptorMethods(file_, context_->EnforceLite())) { | 301 if (HasDescriptorMethods(file_, context_->EnforceLite())) { |
289 EnumGenerator(file_->enum_type(i), immutable_api_, context_.get()) | 302 EnumGenerator(file_->enum_type(i), immutable_api_, context_.get()) |
290 .Generate(printer); | 303 .Generate(printer); |
291 } else { | 304 } else { |
292 EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get()) | 305 EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get()) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 " getDescriptor() {\n" | 378 " getDescriptor() {\n" |
366 " return descriptor;\n" | 379 " return descriptor;\n" |
367 "}\n" | 380 "}\n" |
368 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" | 381 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" |
369 " descriptor;\n" | 382 " descriptor;\n" |
370 "static {\n", | 383 "static {\n", |
371 // TODO(dweis): Mark this as final. | 384 // TODO(dweis): Mark this as final. |
372 "final", ""); | 385 "final", ""); |
373 printer->Indent(); | 386 printer->Indent(); |
374 | 387 |
375 SharedCodeGenerator shared_code_generator(file_); | 388 SharedCodeGenerator shared_code_generator(file_, options_); |
376 shared_code_generator.GenerateDescriptors(printer); | 389 shared_code_generator.GenerateDescriptors(printer); |
377 | 390 |
378 int bytecode_estimate = 0; | 391 int bytecode_estimate = 0; |
379 int method_num = 0; | 392 int method_num = 0; |
380 | 393 |
381 for (int i = 0; i < file_->message_type_count(); i++) { | 394 for (int i = 0; i < file_->message_type_count(); i++) { |
382 bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializ
ers(printer); | 395 bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializ
ers(printer); |
383 MaybeRestartJavaMethod( | 396 MaybeRestartJavaMethod( |
384 printer, | 397 printer, |
385 &bytecode_estimate, &method_num, | 398 &bytecode_estimate, &method_num, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 "$dependency$.getDescriptor();\n", | 529 "$dependency$.getDescriptor();\n", |
517 "dependency", dependency); | 530 "dependency", dependency); |
518 } | 531 } |
519 } | 532 } |
520 | 533 |
521 printer->Outdent(); | 534 printer->Outdent(); |
522 printer->Print( | 535 printer->Print( |
523 "}\n"); | 536 "}\n"); |
524 } | 537 } |
525 | 538 |
526 template<typename GeneratorClass, typename DescriptorClass> | 539 template <typename GeneratorClass, typename DescriptorClass> |
527 static void GenerateSibling(const string& package_dir, | 540 static void GenerateSibling(const string& package_dir, |
528 const string& java_package, | 541 const string& java_package, |
529 const DescriptorClass* descriptor, | 542 const DescriptorClass* descriptor, |
530 GeneratorContext* context, | 543 GeneratorContext* context, |
531 vector<string>* file_list, | 544 std::vector<string>* file_list, bool annotate_code, |
| 545 std::vector<string>* annotation_list, |
532 const string& name_suffix, | 546 const string& name_suffix, |
533 GeneratorClass* generator, | 547 GeneratorClass* generator, |
534 void (GeneratorClass::*pfn)(io::Printer* printer)) { | 548 void (GeneratorClass::*pfn)(io::Printer* printer)) { |
535 string filename = package_dir + descriptor->name() + name_suffix + ".java"; | 549 string filename = package_dir + descriptor->name() + name_suffix + ".java"; |
536 file_list->push_back(filename); | 550 file_list->push_back(filename); |
| 551 string info_full_path = filename + ".pb.meta"; |
| 552 GeneratedCodeInfo annotations; |
| 553 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( |
| 554 &annotations); |
537 | 555 |
538 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); | 556 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); |
539 io::Printer printer(output.get(), '$'); | 557 io::Printer printer(output.get(), '$', |
| 558 annotate_code ? &annotation_collector : NULL); |
540 | 559 |
541 printer.Print( | 560 printer.Print( |
542 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 561 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
543 "// source: $filename$\n" | 562 "// source: $filename$\n" |
544 "\n", | 563 "\n", |
545 "filename", descriptor->file()->name()); | 564 "filename", descriptor->file()->name()); |
546 if (!java_package.empty()) { | 565 if (!java_package.empty()) { |
547 printer.Print( | 566 printer.Print( |
548 "package $package$;\n" | 567 "package $package$;\n" |
549 "\n", | 568 "\n", |
550 "package", java_package); | 569 "package", java_package); |
551 } | 570 } |
552 | 571 |
553 (generator->*pfn)(&printer); | 572 (generator->*pfn)(&printer); |
| 573 |
| 574 if (annotate_code) { |
| 575 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> info_output( |
| 576 context->Open(info_full_path)); |
| 577 annotations.SerializeToZeroCopyStream(info_output.get()); |
| 578 annotation_list->push_back(info_full_path); |
| 579 } |
554 } | 580 } |
555 | 581 |
556 void FileGenerator::GenerateSiblings(const string& package_dir, | 582 void FileGenerator::GenerateSiblings(const string& package_dir, |
557 GeneratorContext* context, | 583 GeneratorContext* context, |
558 vector<string>* file_list) { | 584 std::vector<string>* file_list, |
| 585 std::vector<string>* annotation_list) { |
559 if (MultipleJavaFiles(file_, immutable_api_)) { | 586 if (MultipleJavaFiles(file_, immutable_api_)) { |
560 for (int i = 0; i < file_->enum_type_count(); i++) { | 587 for (int i = 0; i < file_->enum_type_count(); i++) { |
561 if (HasDescriptorMethods(file_, context_->EnforceLite())) { | 588 if (HasDescriptorMethods(file_, context_->EnforceLite())) { |
562 EnumGenerator generator(file_->enum_type(i), immutable_api_, | 589 EnumGenerator generator(file_->enum_type(i), immutable_api_, |
563 context_.get()); | 590 context_.get()); |
564 GenerateSibling<EnumGenerator>(package_dir, java_package_, | 591 GenerateSibling<EnumGenerator>( |
565 file_->enum_type(i), | 592 package_dir, java_package_, file_->enum_type(i), context, file_list, |
566 context, file_list, "", | 593 options_.annotate_code, annotation_list, "", &generator, |
567 &generator, | 594 &EnumGenerator::Generate); |
568 &EnumGenerator::Generate); | |
569 } else { | 595 } else { |
570 EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, | 596 EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, |
571 context_.get()); | 597 context_.get()); |
572 GenerateSibling<EnumLiteGenerator>(package_dir, java_package_, | 598 GenerateSibling<EnumLiteGenerator>( |
573 file_->enum_type(i), | 599 package_dir, java_package_, file_->enum_type(i), context, file_list, |
574 context, file_list, "", | 600 options_.annotate_code, annotation_list, "", &generator, |
575 &generator, | 601 &EnumLiteGenerator::Generate); |
576 &EnumLiteGenerator::Generate); | |
577 } | 602 } |
578 } | 603 } |
579 for (int i = 0; i < file_->message_type_count(); i++) { | 604 for (int i = 0; i < file_->message_type_count(); i++) { |
580 if (immutable_api_) { | 605 if (immutable_api_) { |
581 GenerateSibling<MessageGenerator>(package_dir, java_package_, | 606 GenerateSibling<MessageGenerator>( |
582 file_->message_type(i), | 607 package_dir, java_package_, file_->message_type(i), context, |
583 context, file_list, | 608 file_list, options_.annotate_code, annotation_list, "OrBuilder", |
584 "OrBuilder", | 609 message_generators_[i].get(), &MessageGenerator::GenerateInterface); |
585 message_generators_[i].get(), | |
586 &MessageGenerator::GenerateInterface); | |
587 } | 610 } |
588 GenerateSibling<MessageGenerator>(package_dir, java_package_, | 611 GenerateSibling<MessageGenerator>( |
589 file_->message_type(i), | 612 package_dir, java_package_, file_->message_type(i), context, |
590 context, file_list, "", | 613 file_list, options_.annotate_code, annotation_list, "", |
591 message_generators_[i].get(), | 614 message_generators_[i].get(), &MessageGenerator::Generate); |
592 &MessageGenerator::Generate); | |
593 } | 615 } |
594 if (HasGenericServices(file_, context_->EnforceLite())) { | 616 if (HasGenericServices(file_, context_->EnforceLite())) { |
595 for (int i = 0; i < file_->service_count(); i++) { | 617 for (int i = 0; i < file_->service_count(); i++) { |
596 google::protobuf::scoped_ptr<ServiceGenerator> generator( | 618 google::protobuf::scoped_ptr<ServiceGenerator> generator( |
597 generator_factory_->NewServiceGenerator(file_->service(i))); | 619 generator_factory_->NewServiceGenerator(file_->service(i))); |
598 GenerateSibling<ServiceGenerator>(package_dir, java_package_, | 620 GenerateSibling<ServiceGenerator>( |
599 file_->service(i), | 621 package_dir, java_package_, file_->service(i), context, file_list, |
600 context, file_list, "", | 622 options_.annotate_code, annotation_list, "", generator.get(), |
601 generator.get(), | 623 &ServiceGenerator::Generate); |
602 &ServiceGenerator::Generate); | |
603 } | 624 } |
604 } | 625 } |
605 } | 626 } |
606 } | 627 } |
607 | 628 |
608 bool FileGenerator::ShouldIncludeDependency( | 629 bool FileGenerator::ShouldIncludeDependency( |
609 const FileDescriptor* descriptor, bool immutable_api) { | 630 const FileDescriptor* descriptor, bool immutable_api) { |
610 return true; | 631 return true; |
611 } | 632 } |
612 | 633 |
613 } // namespace java | 634 } // namespace java |
614 } // namespace compiler | 635 } // namespace compiler |
615 } // namespace protobuf | 636 } // namespace protobuf |
616 } // namespace google | 637 } // namespace google |
OLD | NEW |