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 std::vector<const FieldDescriptor*> fields; | 93 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 |
192 } // namespace | 194 } // namespace |
193 | 195 |
194 FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, | 196 FileGenerator::FileGenerator(const FileDescriptor* file, bool immutable_api, |
195 bool immutable_api) | 197 bool enforce_lite) |
196 : file_(file), | 198 : file_(file), |
197 java_package_(FileJavaPackage(file, immutable_api)), | 199 java_package_(FileJavaPackage(file, immutable_api)), |
198 message_generators_( | 200 message_generators_( |
199 new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_
count()]), | 201 new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_
count()]), |
200 extension_generators_( | 202 extension_generators_( |
201 new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_c
ount()]), | 203 new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_c
ount()]), |
202 context_(new Context(file, options)), | 204 context_(new Context(file)), |
203 name_resolver_(context_->GetNameResolver()), | 205 name_resolver_(context_->GetNameResolver()), |
204 options_(options), | |
205 immutable_api_(immutable_api) { | 206 immutable_api_(immutable_api) { |
206 classname_ = name_resolver_->GetFileClassName(file, immutable_api); | 207 classname_ = name_resolver_->GetFileClassName(file, immutable_api); |
| 208 context_->SetEnforceLite(enforce_lite); |
207 generator_factory_.reset( | 209 generator_factory_.reset( |
208 new ImmutableGeneratorFactory(context_.get())); | 210 new ImmutableGeneratorFactory(context_.get())); |
209 for (int i = 0; i < file_->message_type_count(); ++i) { | 211 for (int i = 0; i < file_->message_type_count(); ++i) { |
210 message_generators_[i].reset( | 212 message_generators_[i].reset( |
211 generator_factory_->NewMessageGenerator(file_->message_type(i))); | 213 generator_factory_->NewMessageGenerator(file_->message_type(i))); |
212 } | 214 } |
213 for (int i = 0; i < file_->extension_count(); ++i) { | 215 for (int i = 0; i < file_->extension_count(); ++i) { |
214 extension_generators_[i].reset( | 216 extension_generators_[i].reset( |
215 generator_factory_->NewExtensionGenerator(file_->extension(i))); | 217 generator_factory_->NewExtensionGenerator(file_->extension(i))); |
216 } | 218 } |
(...skipping 27 matching lines...) Expand all Loading... |
244 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 246 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
245 "// source: $filename$\n" | 247 "// source: $filename$\n" |
246 "\n", | 248 "\n", |
247 "filename", file_->name()); | 249 "filename", file_->name()); |
248 if (!java_package_.empty()) { | 250 if (!java_package_.empty()) { |
249 printer->Print( | 251 printer->Print( |
250 "package $package$;\n" | 252 "package $package$;\n" |
251 "\n", | 253 "\n", |
252 "package", java_package_); | 254 "package", java_package_); |
253 } | 255 } |
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 $ctor$() {}\n", | 258 " private $classname$() {}\n", |
259 "classname", classname_, "ctor", classname_); | 259 "classname", classname_); |
260 printer->Annotate("classname", file_->name()); | |
261 printer->Indent(); | 260 printer->Indent(); |
262 | 261 |
263 // ----------------------------------------------------------------- | 262 // ----------------------------------------------------------------- |
264 | 263 |
265 printer->Print( | 264 printer->Print( |
266 "public static void registerAllExtensions(\n" | 265 "public static void registerAllExtensions(\n" |
267 " com.google.protobuf.ExtensionRegistryLite registry) {\n"); | 266 " com.google.protobuf.ExtensionRegistry$lite$ registry) {\n", |
| 267 "lite", |
| 268 HasDescriptorMethods(file_, context_->EnforceLite()) ? "" : "Lite"); |
268 | 269 |
269 printer->Indent(); | 270 printer->Indent(); |
270 | 271 |
271 for (int i = 0; i < file_->extension_count(); i++) { | 272 for (int i = 0; i < file_->extension_count(); i++) { |
272 extension_generators_[i]->GenerateRegistrationCode(printer); | 273 extension_generators_[i]->GenerateRegistrationCode(printer); |
273 } | 274 } |
274 | 275 |
275 for (int i = 0; i < file_->message_type_count(); i++) { | 276 for (int i = 0; i < file_->message_type_count(); i++) { |
276 message_generators_[i]->GenerateExtensionRegistrationCode(printer); | 277 message_generators_[i]->GenerateExtensionRegistrationCode(printer); |
277 } | 278 } |
278 | 279 |
279 printer->Outdent(); | 280 printer->Outdent(); |
280 printer->Print( | 281 printer->Print( |
281 "}\n"); | 282 "}\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 } | |
296 | 283 |
297 // ----------------------------------------------------------------- | 284 // ----------------------------------------------------------------- |
298 | 285 |
299 if (!MultipleJavaFiles(file_, immutable_api_)) { | 286 if (!MultipleJavaFiles(file_, immutable_api_)) { |
300 for (int i = 0; i < file_->enum_type_count(); i++) { | 287 for (int i = 0; i < file_->enum_type_count(); i++) { |
301 if (HasDescriptorMethods(file_, context_->EnforceLite())) { | 288 if (HasDescriptorMethods(file_, context_->EnforceLite())) { |
302 EnumGenerator(file_->enum_type(i), immutable_api_, context_.get()) | 289 EnumGenerator(file_->enum_type(i), immutable_api_, context_.get()) |
303 .Generate(printer); | 290 .Generate(printer); |
304 } else { | 291 } else { |
305 EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get()) | 292 EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get()) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 " getDescriptor() {\n" | 365 " getDescriptor() {\n" |
379 " return descriptor;\n" | 366 " return descriptor;\n" |
380 "}\n" | 367 "}\n" |
381 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" | 368 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n" |
382 " descriptor;\n" | 369 " descriptor;\n" |
383 "static {\n", | 370 "static {\n", |
384 // TODO(dweis): Mark this as final. | 371 // TODO(dweis): Mark this as final. |
385 "final", ""); | 372 "final", ""); |
386 printer->Indent(); | 373 printer->Indent(); |
387 | 374 |
388 SharedCodeGenerator shared_code_generator(file_, options_); | 375 SharedCodeGenerator shared_code_generator(file_); |
389 shared_code_generator.GenerateDescriptors(printer); | 376 shared_code_generator.GenerateDescriptors(printer); |
390 | 377 |
391 int bytecode_estimate = 0; | 378 int bytecode_estimate = 0; |
392 int method_num = 0; | 379 int method_num = 0; |
393 | 380 |
394 for (int i = 0; i < file_->message_type_count(); i++) { | 381 for (int i = 0; i < file_->message_type_count(); i++) { |
395 bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializ
ers(printer); | 382 bytecode_estimate += message_generators_[i]->GenerateStaticVariableInitializ
ers(printer); |
396 MaybeRestartJavaMethod( | 383 MaybeRestartJavaMethod( |
397 printer, | 384 printer, |
398 &bytecode_estimate, &method_num, | 385 &bytecode_estimate, &method_num, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 "$dependency$.getDescriptor();\n", | 516 "$dependency$.getDescriptor();\n", |
530 "dependency", dependency); | 517 "dependency", dependency); |
531 } | 518 } |
532 } | 519 } |
533 | 520 |
534 printer->Outdent(); | 521 printer->Outdent(); |
535 printer->Print( | 522 printer->Print( |
536 "}\n"); | 523 "}\n"); |
537 } | 524 } |
538 | 525 |
539 template <typename GeneratorClass, typename DescriptorClass> | 526 template<typename GeneratorClass, typename DescriptorClass> |
540 static void GenerateSibling(const string& package_dir, | 527 static void GenerateSibling(const string& package_dir, |
541 const string& java_package, | 528 const string& java_package, |
542 const DescriptorClass* descriptor, | 529 const DescriptorClass* descriptor, |
543 GeneratorContext* context, | 530 GeneratorContext* context, |
544 std::vector<string>* file_list, bool annotate_code, | 531 vector<string>* file_list, |
545 std::vector<string>* annotation_list, | |
546 const string& name_suffix, | 532 const string& name_suffix, |
547 GeneratorClass* generator, | 533 GeneratorClass* generator, |
548 void (GeneratorClass::*pfn)(io::Printer* printer)) { | 534 void (GeneratorClass::*pfn)(io::Printer* printer)) { |
549 string filename = package_dir + descriptor->name() + name_suffix + ".java"; | 535 string filename = package_dir + descriptor->name() + name_suffix + ".java"; |
550 file_list->push_back(filename); | 536 file_list->push_back(filename); |
551 string info_full_path = filename + ".pb.meta"; | |
552 GeneratedCodeInfo annotations; | |
553 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( | |
554 &annotations); | |
555 | 537 |
556 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); | 538 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); |
557 io::Printer printer(output.get(), '$', | 539 io::Printer printer(output.get(), '$'); |
558 annotate_code ? &annotation_collector : NULL); | |
559 | 540 |
560 printer.Print( | 541 printer.Print( |
561 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 542 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
562 "// source: $filename$\n" | 543 "// source: $filename$\n" |
563 "\n", | 544 "\n", |
564 "filename", descriptor->file()->name()); | 545 "filename", descriptor->file()->name()); |
565 if (!java_package.empty()) { | 546 if (!java_package.empty()) { |
566 printer.Print( | 547 printer.Print( |
567 "package $package$;\n" | 548 "package $package$;\n" |
568 "\n", | 549 "\n", |
569 "package", java_package); | 550 "package", java_package); |
570 } | 551 } |
571 | 552 |
572 (generator->*pfn)(&printer); | 553 (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 } | |
580 } | 554 } |
581 | 555 |
582 void FileGenerator::GenerateSiblings(const string& package_dir, | 556 void FileGenerator::GenerateSiblings(const string& package_dir, |
583 GeneratorContext* context, | 557 GeneratorContext* context, |
584 std::vector<string>* file_list, | 558 vector<string>* file_list) { |
585 std::vector<string>* annotation_list) { | |
586 if (MultipleJavaFiles(file_, immutable_api_)) { | 559 if (MultipleJavaFiles(file_, immutable_api_)) { |
587 for (int i = 0; i < file_->enum_type_count(); i++) { | 560 for (int i = 0; i < file_->enum_type_count(); i++) { |
588 if (HasDescriptorMethods(file_, context_->EnforceLite())) { | 561 if (HasDescriptorMethods(file_, context_->EnforceLite())) { |
589 EnumGenerator generator(file_->enum_type(i), immutable_api_, | 562 EnumGenerator generator(file_->enum_type(i), immutable_api_, |
590 context_.get()); | 563 context_.get()); |
591 GenerateSibling<EnumGenerator>( | 564 GenerateSibling<EnumGenerator>(package_dir, java_package_, |
592 package_dir, java_package_, file_->enum_type(i), context, file_list, | 565 file_->enum_type(i), |
593 options_.annotate_code, annotation_list, "", &generator, | 566 context, file_list, "", |
594 &EnumGenerator::Generate); | 567 &generator, |
| 568 &EnumGenerator::Generate); |
595 } else { | 569 } else { |
596 EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, | 570 EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, |
597 context_.get()); | 571 context_.get()); |
598 GenerateSibling<EnumLiteGenerator>( | 572 GenerateSibling<EnumLiteGenerator>(package_dir, java_package_, |
599 package_dir, java_package_, file_->enum_type(i), context, file_list, | 573 file_->enum_type(i), |
600 options_.annotate_code, annotation_list, "", &generator, | 574 context, file_list, "", |
601 &EnumLiteGenerator::Generate); | 575 &generator, |
| 576 &EnumLiteGenerator::Generate); |
602 } | 577 } |
603 } | 578 } |
604 for (int i = 0; i < file_->message_type_count(); i++) { | 579 for (int i = 0; i < file_->message_type_count(); i++) { |
605 if (immutable_api_) { | 580 if (immutable_api_) { |
606 GenerateSibling<MessageGenerator>( | 581 GenerateSibling<MessageGenerator>(package_dir, java_package_, |
607 package_dir, java_package_, file_->message_type(i), context, | 582 file_->message_type(i), |
608 file_list, options_.annotate_code, annotation_list, "OrBuilder", | 583 context, file_list, |
609 message_generators_[i].get(), &MessageGenerator::GenerateInterface); | 584 "OrBuilder", |
| 585 message_generators_[i].get(), |
| 586 &MessageGenerator::GenerateInterface); |
610 } | 587 } |
611 GenerateSibling<MessageGenerator>( | 588 GenerateSibling<MessageGenerator>(package_dir, java_package_, |
612 package_dir, java_package_, file_->message_type(i), context, | 589 file_->message_type(i), |
613 file_list, options_.annotate_code, annotation_list, "", | 590 context, file_list, "", |
614 message_generators_[i].get(), &MessageGenerator::Generate); | 591 message_generators_[i].get(), |
| 592 &MessageGenerator::Generate); |
615 } | 593 } |
616 if (HasGenericServices(file_, context_->EnforceLite())) { | 594 if (HasGenericServices(file_, context_->EnforceLite())) { |
617 for (int i = 0; i < file_->service_count(); i++) { | 595 for (int i = 0; i < file_->service_count(); i++) { |
618 google::protobuf::scoped_ptr<ServiceGenerator> generator( | 596 google::protobuf::scoped_ptr<ServiceGenerator> generator( |
619 generator_factory_->NewServiceGenerator(file_->service(i))); | 597 generator_factory_->NewServiceGenerator(file_->service(i))); |
620 GenerateSibling<ServiceGenerator>( | 598 GenerateSibling<ServiceGenerator>(package_dir, java_package_, |
621 package_dir, java_package_, file_->service(i), context, file_list, | 599 file_->service(i), |
622 options_.annotate_code, annotation_list, "", generator.get(), | 600 context, file_list, "", |
623 &ServiceGenerator::Generate); | 601 generator.get(), |
| 602 &ServiceGenerator::Generate); |
624 } | 603 } |
625 } | 604 } |
626 } | 605 } |
627 } | 606 } |
628 | 607 |
629 bool FileGenerator::ShouldIncludeDependency( | 608 bool FileGenerator::ShouldIncludeDependency( |
630 const FileDescriptor* descriptor, bool immutable_api) { | 609 const FileDescriptor* descriptor, bool immutable_api) { |
631 return true; | 610 return true; |
632 } | 611 } |
633 | 612 |
634 } // namespace java | 613 } // namespace java |
635 } // namespace compiler | 614 } // namespace compiler |
636 } // namespace protobuf | 615 } // namespace protobuf |
637 } // namespace google | 616 } // namespace google |
OLD | NEW |