65 struct FieldDescriptorCompare {
73 return f1->full_name() < f2->full_name();
77 typedef std::set<const FieldDescriptor*, FieldDescriptorCompare>
85 bool CollectExtensions(
const Message&
message, FieldDescriptorSet* extensions) {
86 const Reflection* reflection =
message.GetReflection();
89 if (reflection->GetUnknownFields(
message).field_count() > 0)
return false;
91 std::vector<const FieldDescriptor*>
fields;
100 for (
int j = 0; j <
size; j++) {
101 const Message& sub_message =
103 if (!CollectExtensions(sub_message, extensions))
return false;
106 const Message& sub_message = reflection->GetMessage(
message,
fields[
i]);
107 if (!CollectExtensions(sub_message, extensions))
return false;
122 FieldDescriptorSet* extensions,
124 if (!CollectExtensions(file_proto, extensions)) {
128 const Descriptor* file_proto_desc = alternate_pool.FindMessageTypeByName(
131 <<
"Find unknown fields in FileDescriptorProto when building "
133 <<
". It's likely that those fields are custom options, however, "
134 "descriptor.proto is not in the transitive dependencies. "
135 "This normally should not happen. Please report a bug.";
136 DynamicMessageFactory factory;
137 std::unique_ptr<Message> dynamic_file_proto(
138 factory.GetPrototype(file_proto_desc)->New());
146 GOOGLE_CHECK(CollectExtensions(*dynamic_file_proto, extensions))
147 <<
"Find unknown fields in FileDescriptorProto when building "
149 <<
". It's likely that those fields are custom options, however, "
150 "those options cannot be recognized in the builder pool. "
151 "This normally should not happen. Please report a bug.";
163 void MaybeRestartJavaMethod(io::Printer* printer,
int* bytecode_estimate,
164 int* method_num,
const char* chain_statement,
165 const char* method_decl) {
172 if ((*bytecode_estimate) > bytesPerMethod) {
174 printer->Print(chain_statement,
"method_num",
StrCat(*method_num));
176 printer->Print(
"}\n");
177 printer->Print(method_decl,
"method_num",
StrCat(*method_num));
179 *bytecode_estimate = 0;
188 message_generators_(file->message_type_count()),
189 extension_generators_(file->extension_count()),
191 name_resolver_(
context_->GetNameResolver()),
193 immutable_api_(immutable_api) {
217 ": Cannot generate Java output because the file's outer class name, "
221 "\", matches the name of one of the types declared inside it. "
222 "Please either rename the type or use the java_outer_classname "
223 "option to specify a different outer class name for the .proto file.");
233 <<
"\", matches the name of one of the types declared inside it when "
234 <<
"case is ignored. This can cause compilation issues on Windows / "
235 <<
"MacOS. Please either rename the type or use the "
236 <<
"java_outer_classname option to specify a different outer class "
237 <<
"name for the .proto file to be safe.";
243 <<
"The optimize_for = LITE_RUNTIME option is no longer supported by "
244 <<
"protobuf Java code generator and is ignored--protoc will always "
245 <<
"generate full runtime code for Java. To use Java Lite runtime, "
246 <<
"users should use the Java Lite plugin instead. See:\n"
248 "https://github.com/protocolbuffers/protobuf/blob/master/java/"
258 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
259 "// source: $filename$\n"
264 "package $package$;\n"
271 "$deprecation$public final class $classname$ {\n"
272 " private $ctor$() {}\n",
282 "public static void registerAllExtensions(\n"
283 " com.google.protobuf.ExtensionRegistryLite registry) {\n");
296 printer->
Print(
"}\n");
305 "public static void registerAllExtensions(\n"
306 " com.google.protobuf.ExtensionRegistry registry) {\n"
307 " registerAllExtensions(\n"
308 " (com.google.protobuf.ExtensionRegistryLite) registry);\n"
330 std::unique_ptr<ServiceGenerator> generator(
332 generator->Generate(printer);
346 int static_block_bytecode_estimate = 0;
349 printer, &static_block_bytecode_estimate);
352 printer->
Print(
"\n");
361 printer->
Print(
"static {\n");
363 int bytecode_estimate = 0;
369 MaybeRestartJavaMethod(
370 printer, &bytecode_estimate, &method_num,
371 "_clinit_autosplit_$method_num$();\n",
372 "private static void _clinit_autosplit_$method_num$() {\n");
376 printer->
Print(
"}\n");
381 "// @@protoc_insertion_point(outer_class_scope)\n");
384 printer->
Print(
"}\n");
390 "public static com.google.protobuf.Descriptors.FileDescriptor\n"
391 " getDescriptor() {\n"
392 " return descriptor;\n"
394 "private static $final$ com.google.protobuf.Descriptors.FileDescriptor\n"
404 int bytecode_estimate = 0;
410 MaybeRestartJavaMethod(
411 printer, &bytecode_estimate, &method_num,
412 "_clinit_autosplit_dinit_$method_num$();\n",
413 "private static void _clinit_autosplit_dinit_$method_num$() {\n");
418 MaybeRestartJavaMethod(
419 printer, &bytecode_estimate, &method_num,
420 "_clinit_autosplit_dinit_$method_num$();\n",
421 "private static void _clinit_autosplit_dinit_$method_num$() {\n");
442 file_proto.SerializeToString(&
file_data);
443 FieldDescriptorSet extensions;
446 if (extensions.size() > 0) {
450 "com.google.protobuf.ExtensionRegistry registry =\n"
451 " com.google.protobuf.ExtensionRegistry.newInstance();\n");
452 FieldDescriptorSet::iterator
it;
453 for (
it = extensions.begin();
it != extensions.end();
it++) {
454 std::unique_ptr<ExtensionGenerator> generator(
456 bytecode_estimate += generator->GenerateRegistrationCode(printer);
457 MaybeRestartJavaMethod(
458 printer, &bytecode_estimate, &method_num,
459 "_clinit_autosplit_dinit_$method_num$(registry);\n",
460 "private static void _clinit_autosplit_dinit_$method_num$(\n"
461 " com.google.protobuf.ExtensionRegistry registry) {\n");
464 "com.google.protobuf.Descriptors.FileDescriptor\n"
465 " .internalUpdateFileDescriptor(descriptor, registry);\n");
473 printer->
Print(
"$dependency$.getDescriptor();\n",
"dependency",
479 printer->
Print(
"}\n");
485 "public static com.google.protobuf.Descriptors.FileDescriptor\n"
486 " getDescriptor() {\n"
487 " return descriptor;\n"
489 "private static final com.google.protobuf.Descriptors.FileDescriptor\n"
495 "descriptor = $immutable_package$.$descriptor_classname$.descriptor;\n",
511 file_proto.SerializeToString(&
file_data);
512 FieldDescriptorSet extensions;
515 if (extensions.size() > 0) {
524 " java.lang.Class immutableClass = java.lang.Class.forName(\n"
525 " \"$immutable_classname$\");\n"
526 "} catch (java.lang.ClassNotFoundException e) {\n",
533 "com.google.protobuf.ExtensionRegistry registry =\n"
534 " com.google.protobuf.ExtensionRegistry.newInstance();\n"
535 "com.google.protobuf.MessageLite defaultExtensionInstance = null;\n");
536 FieldDescriptorSet::iterator
it;
537 for (
it = extensions.begin();
it != extensions.end();
it++) {
550 "defaultExtensionInstance = com.google.protobuf.Internal\n"
551 " .getDefaultInstance(\"$class$\");\n"
552 "if (defaultExtensionInstance != null) {\n"
554 " $scope$.getExtensions().get($index$),\n"
555 " (com.google.protobuf.Message) defaultExtensionInstance);\n"
557 "scope", scope,
"index",
StrCat(
field->index()),
"class",
560 printer->
Print(
"registry.add($scope$.getExtensions().get($index$));\n",
565 "com.google.protobuf.Descriptors.FileDescriptor\n"
566 " .internalUpdateFileDescriptor(descriptor, registry);\n");
569 printer->
Print(
"}\n");
577 printer->
Print(
"$dependency$.getDescriptor();\n",
"dependency",
583 printer->
Print(
"}\n");
586 template <
typename GeneratorClass,
typename DescriptorClass>
590 std::vector<std::string>* file_list,
bool annotate_code,
591 std::vector<std::string>* annotation_list,
const std::string& name_suffix,
592 GeneratorClass* generator,
593 void (GeneratorClass::*pfn)(
io::Printer* printer)) {
595 package_dir +
descriptor->name() + name_suffix +
".java";
596 file_list->push_back(filename);
597 std::string info_full_path = filename +
".pb.meta";
602 std::unique_ptr<io::ZeroCopyOutputStream>
output(context->
Open(filename));
604 annotate_code ? &annotation_collector :
NULL);
607 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
608 "// source: $filename$\n"
611 if (!java_package.empty()) {
613 "package $package$;\n"
615 "package", java_package);
618 (generator->*pfn)(&printer);
621 std::unique_ptr<io::ZeroCopyOutputStream> info_output(
622 context->
Open(info_full_path));
623 annotations.SerializeToZeroCopyStream(info_output.get());
624 annotation_list->push_back(info_full_path);
630 std::vector<std::string>* file_list,
631 std::vector<std::string>* annotation_list) {
637 GenerateSibling<EnumGenerator>(
644 GenerateSibling<EnumLiteGenerator>(
652 GenerateSibling<MessageGenerator>(
657 GenerateSibling<MessageGenerator>(
664 std::unique_ptr<ServiceGenerator> generator(
666 GenerateSibling<ServiceGenerator>(
676 bool immutable_api) {