| Index: third_party/protobuf/src/google/protobuf/compiler/java/java_message.cc
|
| diff --git a/third_party/protobuf/src/google/protobuf/compiler/java/java_message.cc b/third_party/protobuf/src/google/protobuf/compiler/java/java_message.cc
|
| index 9322e242badaff23d4f8cb0ba192e08e4514fdef..09b0fd945c86e52fc53a7d23188094bdedd004b9 100644
|
| --- a/third_party/protobuf/src/google/protobuf/compiler/java/java_message.cc
|
| +++ b/third_party/protobuf/src/google/protobuf/compiler/java/java_message.cc
|
| @@ -1,6 +1,6 @@
|
| // Protocol Buffers - Google's data interchange format
|
| // Copyright 2008 Google Inc. All rights reserved.
|
| -// http://code.google.com/p/protobuf/
|
| +// https://developers.google.com/protocol-buffers/
|
| //
|
| // Redistribution and use in source and binary forms, with or without
|
| // modification, are permitted provided that the following conditions are
|
| @@ -37,12 +37,21 @@
|
| #include <algorithm>
|
| #include <google/protobuf/stubs/hash.h>
|
| #include <map>
|
| +#include <memory>
|
| +#ifndef _SHARED_PTR_H
|
| +#include <google/protobuf/stubs/shared_ptr.h>
|
| +#endif
|
| #include <vector>
|
|
|
| +#include <google/protobuf/compiler/java/java_context.h>
|
| #include <google/protobuf/compiler/java/java_doc_comment.h>
|
| #include <google/protobuf/compiler/java/java_enum.h>
|
| #include <google/protobuf/compiler/java/java_extension.h>
|
| +#include <google/protobuf/compiler/java/java_generator_factory.h>
|
| #include <google/protobuf/compiler/java/java_helpers.h>
|
| +#include <google/protobuf/compiler/java/java_message_builder.h>
|
| +#include <google/protobuf/compiler/java/java_message_builder_lite.h>
|
| +#include <google/protobuf/compiler/java/java_name_resolver.h>
|
| #include <google/protobuf/io/coded_stream.h>
|
| #include <google/protobuf/io/printer.h>
|
| #include <google/protobuf/descriptor.pb.h>
|
| @@ -59,233 +68,190 @@ using internal::WireFormat;
|
| using internal::WireFormatLite;
|
|
|
| namespace {
|
| -
|
| -void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) {
|
| - // Print the field's proto-syntax definition as a comment. We don't want to
|
| - // print group bodies so we cut off after the first line.
|
| - string def = field->DebugString();
|
| - printer->Print("// $def$\n",
|
| - "def", def.substr(0, def.find_first_of('\n')));
|
| -}
|
| -
|
| -struct FieldOrderingByNumber {
|
| - inline bool operator()(const FieldDescriptor* a,
|
| - const FieldDescriptor* b) const {
|
| - return a->number() < b->number();
|
| - }
|
| -};
|
| -
|
| -struct ExtensionRangeOrdering {
|
| - bool operator()(const Descriptor::ExtensionRange* a,
|
| - const Descriptor::ExtensionRange* b) const {
|
| - return a->start < b->start;
|
| - }
|
| -};
|
| -
|
| -// Sort the fields of the given Descriptor by number into a new[]'d array
|
| -// and return it.
|
| -const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
|
| - const FieldDescriptor** fields =
|
| - new const FieldDescriptor*[descriptor->field_count()];
|
| - for (int i = 0; i < descriptor->field_count(); i++) {
|
| - fields[i] = descriptor->field(i);
|
| - }
|
| - sort(fields, fields + descriptor->field_count(),
|
| - FieldOrderingByNumber());
|
| - return fields;
|
| -}
|
| -
|
| -// Get an identifier that uniquely identifies this type within the file.
|
| -// This is used to declare static variables related to this type at the
|
| -// outermost file scope.
|
| -string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
|
| - return "static_" + StringReplace(descriptor->full_name(), ".", "_", true);
|
| -}
|
| -
|
| -// Returns true if the message type has any required fields. If it doesn't,
|
| -// we can optimize out calls to its isInitialized() method.
|
| -//
|
| -// already_seen is used to avoid checking the same type multiple times
|
| -// (and also to protect against recursion).
|
| -static bool HasRequiredFields(
|
| - const Descriptor* type,
|
| - hash_set<const Descriptor*>* already_seen) {
|
| - if (already_seen->count(type) > 0) {
|
| - // The type is already in cache. This means that either:
|
| - // a. The type has no required fields.
|
| - // b. We are in the midst of checking if the type has required fields,
|
| - // somewhere up the stack. In this case, we know that if the type
|
| - // has any required fields, they'll be found when we return to it,
|
| - // and the whole call to HasRequiredFields() will return true.
|
| - // Therefore, we don't have to check if this type has required fields
|
| - // here.
|
| - return false;
|
| - }
|
| - already_seen->insert(type);
|
| -
|
| - // If the type has extensions, an extension with message type could contain
|
| - // required fields, so we have to be conservative and assume such an
|
| - // extension exists.
|
| - if (type->extension_range_count() > 0) return true;
|
| -
|
| - for (int i = 0; i < type->field_count(); i++) {
|
| - const FieldDescriptor* field = type->field(i);
|
| - if (field->is_required()) {
|
| - return true;
|
| - }
|
| - if (GetJavaType(field) == JAVATYPE_MESSAGE) {
|
| - if (HasRequiredFields(field->message_type(), already_seen)) {
|
| - return true;
|
| - }
|
| - }
|
| - }
|
| -
|
| - return false;
|
| +bool GenerateHasBits(const Descriptor* descriptor) {
|
| + return SupportFieldPresence(descriptor->file()) ||
|
| + HasRepeatedFields(descriptor);
|
| }
|
|
|
| -static bool HasRequiredFields(const Descriptor* type) {
|
| - hash_set<const Descriptor*> already_seen;
|
| - return HasRequiredFields(type, &already_seen);
|
| +string MapValueImmutableClassdName(const Descriptor* descriptor,
|
| + ClassNameResolver* name_resolver) {
|
| + const FieldDescriptor* value_field = descriptor->FindFieldByName("value");
|
| + GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, value_field->type());
|
| + return name_resolver->GetImmutableClassName(value_field->message_type());
|
| }
|
| -
|
| } // namespace
|
|
|
| // ===================================================================
|
|
|
| MessageGenerator::MessageGenerator(const Descriptor* descriptor)
|
| - : descriptor_(descriptor),
|
| - field_generators_(descriptor) {
|
| -}
|
| + : descriptor_(descriptor) {}
|
|
|
| MessageGenerator::~MessageGenerator() {}
|
|
|
| -void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - // Because descriptor.proto (com.google.protobuf.DescriptorProtos) is
|
| - // used in the construction of descriptors, we have a tricky bootstrapping
|
| - // problem. To help control static initialization order, we make sure all
|
| - // descriptors and other static data that depends on them are members of
|
| - // the outermost class in the file. This way, they will be initialized in
|
| - // a deterministic order.
|
| -
|
| - map<string, string> vars;
|
| - vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
|
| - vars["index"] = SimpleItoa(descriptor_->index());
|
| - vars["classname"] = ClassName(descriptor_);
|
| - if (descriptor_->containing_type() != NULL) {
|
| - vars["parent"] = UniqueFileScopeIdentifier(
|
| - descriptor_->containing_type());
|
| - }
|
| - if (descriptor_->file()->options().java_multiple_files()) {
|
| - // We can only make these package-private since the classes that use them
|
| - // are in separate files.
|
| - vars["private"] = "";
|
| - } else {
|
| - vars["private"] = "private ";
|
| - }
|
| -
|
| - // The descriptor for this type.
|
| - printer->Print(vars,
|
| - "$private$static com.google.protobuf.Descriptors.Descriptor\n"
|
| - " internal_$identifier$_descriptor;\n");
|
| +// ===================================================================
|
| +// TODO(api): Move this class to a separate immutable_message.cc file.
|
| +ImmutableMessageGenerator::ImmutableMessageGenerator(
|
| + const Descriptor* descriptor, Context* context)
|
| + : MessageGenerator(descriptor), context_(context),
|
| + name_resolver_(context->GetNameResolver()),
|
| + field_generators_(descriptor, context_) {
|
| + GOOGLE_CHECK_NE(
|
| + FileOptions::LITE_RUNTIME, descriptor->file()->options().optimize_for());
|
| +}
|
|
|
| - // And the FieldAccessorTable.
|
| - printer->Print(vars,
|
| - "$private$static\n"
|
| - " com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
|
| - " internal_$identifier$_fieldAccessorTable;\n");
|
| +ImmutableMessageGenerator::~ImmutableMessageGenerator() {}
|
| +
|
| +void ImmutableMessageGenerator::GenerateStaticVariables(io::Printer* printer) {
|
| + // Because descriptor.proto (com.google.protobuf.DescriptorProtos) is
|
| + // used in the construction of descriptors, we have a tricky bootstrapping
|
| + // problem. To help control static initialization order, we make sure all
|
| + // descriptors and other static data that depends on them are members of
|
| + // the outermost class in the file. This way, they will be initialized in
|
| + // a deterministic order.
|
| +
|
| + map<string, string> vars;
|
| + vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
|
| + vars["index"] = SimpleItoa(descriptor_->index());
|
| + vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_);
|
| + if (descriptor_->containing_type() != NULL) {
|
| + vars["parent"] = UniqueFileScopeIdentifier(
|
| + descriptor_->containing_type());
|
| + }
|
| + if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) {
|
| + // We can only make these package-private since the classes that use them
|
| + // are in separate files.
|
| + vars["private"] = "";
|
| + } else {
|
| + vars["private"] = "private ";
|
| }
|
|
|
| + // The descriptor for this type.
|
| + printer->Print(vars,
|
| + // TODO(teboring): final needs to be added back. The way to fix it is to
|
| + // generate methods that can construct the types, and then still declare the
|
| + // types, and then init them in clinit with the new method calls.
|
| + "$private$static com.google.protobuf.Descriptors.Descriptor\n"
|
| + " internal_$identifier$_descriptor;\n");
|
| +
|
| + // And the FieldAccessorTable.
|
| + GenerateFieldAccessorTable(printer);
|
| +
|
| // Generate static members for all nested types.
|
| for (int i = 0; i < descriptor_->nested_type_count(); i++) {
|
| // TODO(kenton): Reuse MessageGenerator objects?
|
| - MessageGenerator(descriptor_->nested_type(i))
|
| + ImmutableMessageGenerator(descriptor_->nested_type(i), context_)
|
| .GenerateStaticVariables(printer);
|
| }
|
| }
|
|
|
| -void MessageGenerator::GenerateStaticVariableInitializers(
|
| +int ImmutableMessageGenerator::GenerateStaticVariableInitializers(
|
| io::Printer* printer) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - map<string, string> vars;
|
| - vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
|
| - vars["index"] = SimpleItoa(descriptor_->index());
|
| - vars["classname"] = ClassName(descriptor_);
|
| - if (descriptor_->containing_type() != NULL) {
|
| - vars["parent"] = UniqueFileScopeIdentifier(
|
| - descriptor_->containing_type());
|
| - }
|
| -
|
| - // The descriptor for this type.
|
| - if (descriptor_->containing_type() == NULL) {
|
| - printer->Print(vars,
|
| - "internal_$identifier$_descriptor =\n"
|
| - " getDescriptor().getMessageTypes().get($index$);\n");
|
| - } else {
|
| - printer->Print(vars,
|
| - "internal_$identifier$_descriptor =\n"
|
| - " internal_$parent$_descriptor.getNestedTypes().get($index$);\n");
|
| - }
|
| -
|
| - // And the FieldAccessorTable.
|
| + int bytecode_estimate = 0;
|
| + map<string, string> vars;
|
| + vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
|
| + vars["index"] = SimpleItoa(descriptor_->index());
|
| + vars["classname"] = name_resolver_->GetImmutableClassName(descriptor_);
|
| + if (descriptor_->containing_type() != NULL) {
|
| + vars["parent"] = UniqueFileScopeIdentifier(
|
| + descriptor_->containing_type());
|
| + }
|
| +
|
| + // The descriptor for this type.
|
| + if (descriptor_->containing_type() == NULL) {
|
| printer->Print(vars,
|
| - "internal_$identifier$_fieldAccessorTable = new\n"
|
| - " com.google.protobuf.GeneratedMessage.FieldAccessorTable(\n"
|
| - " internal_$identifier$_descriptor,\n"
|
| - " new java.lang.String[] { ");
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - printer->Print(
|
| - "\"$field_name$\", ",
|
| - "field_name",
|
| - UnderscoresToCapitalizedCamelCase(descriptor_->field(i)));
|
| - }
|
| - printer->Print(
|
| - "});\n");
|
| + "internal_$identifier$_descriptor =\n"
|
| + " getDescriptor().getMessageTypes().get($index$);\n");
|
| + bytecode_estimate += 30;
|
| + } else {
|
| + printer->Print(vars,
|
| + "internal_$identifier$_descriptor =\n"
|
| + " internal_$parent$_descriptor.getNestedTypes().get($index$);\n");
|
| + bytecode_estimate += 30;
|
| }
|
|
|
| + // And the FieldAccessorTable.
|
| + bytecode_estimate += GenerateFieldAccessorTableInitializer(printer);
|
| +
|
| // Generate static member initializers for all nested types.
|
| for (int i = 0; i < descriptor_->nested_type_count(); i++) {
|
| // TODO(kenton): Reuse MessageGenerator objects?
|
| - MessageGenerator(descriptor_->nested_type(i))
|
| - .GenerateStaticVariableInitializers(printer);
|
| + bytecode_estimate +=
|
| + ImmutableMessageGenerator(descriptor_->nested_type(i), context_)
|
| + .GenerateStaticVariableInitializers(printer);
|
| }
|
| + return bytecode_estimate;
|
| +}
|
| +
|
| +void ImmutableMessageGenerator::
|
| +GenerateFieldAccessorTable(io::Printer* printer) {
|
| + map<string, string> vars;
|
| + vars["identifier"] = UniqueFileScopeIdentifier(descriptor_);
|
| + if (MultipleJavaFiles(descriptor_->file(), /* immutable = */ true)) {
|
| + // We can only make these package-private since the classes that use them
|
| + // are in separate files.
|
| + vars["private"] = "";
|
| + } else {
|
| + vars["private"] = "private ";
|
| + }
|
| + printer->Print(vars,
|
| + "$private$static\n"
|
| + " com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
|
| + " internal_$identifier$_fieldAccessorTable;\n");
|
| +}
|
| +
|
| +int ImmutableMessageGenerator::
|
| +GenerateFieldAccessorTableInitializer(io::Printer* printer) {
|
| + int bytecode_estimate = 10;
|
| + printer->Print(
|
| + "internal_$identifier$_fieldAccessorTable = new\n"
|
| + " com.google.protobuf.GeneratedMessage.FieldAccessorTable(\n"
|
| + " internal_$identifier$_descriptor,\n"
|
| + " new java.lang.String[] { ",
|
| + "identifier",
|
| + UniqueFileScopeIdentifier(descriptor_));
|
| + for (int i = 0; i < descriptor_->field_count(); i++) {
|
| + const FieldDescriptor* field = descriptor_->field(i);
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
| + bytecode_estimate += 6;
|
| + printer->Print(
|
| + "\"$field_name$\", ",
|
| + "field_name", info->capitalized_name);
|
| + }
|
| + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
|
| + const OneofDescriptor* oneof = descriptor_->oneof_decl(i);
|
| + const OneofGeneratorInfo* info = context_->GetOneofGeneratorInfo(oneof);
|
| + bytecode_estimate += 6;
|
| + printer->Print(
|
| + "\"$oneof_name$\", ",
|
| + "oneof_name", info->capitalized_name);
|
| + }
|
| + printer->Print("});\n");
|
| + return bytecode_estimate;
|
| }
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::GenerateInterface(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) {
|
| if (descriptor_->extension_range_count() > 0) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public interface $classname$OrBuilder extends\n"
|
| - " com.google.protobuf.GeneratedMessage.\n"
|
| - " ExtendableMessageOrBuilder<$classname$> {\n",
|
| - "classname", descriptor_->name());
|
| - } else {
|
| - printer->Print(
|
| - "public interface $classname$OrBuilder extends \n"
|
| - " com.google.protobuf.GeneratedMessageLite.\n"
|
| - " ExtendableMessageOrBuilder<$classname$> {\n",
|
| - "classname", descriptor_->name());
|
| - }
|
| + printer->Print(
|
| + "public interface $classname$OrBuilder extends\n"
|
| + " $extra_interfaces$\n"
|
| + " com.google.protobuf.GeneratedMessage.\n"
|
| + " ExtendableMessageOrBuilder<$classname$> {\n",
|
| + "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_),
|
| + "classname", descriptor_->name());
|
| } else {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public interface $classname$OrBuilder\n"
|
| - " extends com.google.protobuf.MessageOrBuilder {\n",
|
| - "classname", descriptor_->name());
|
| - } else {
|
| - printer->Print(
|
| - "public interface $classname$OrBuilder\n"
|
| - " extends com.google.protobuf.MessageLiteOrBuilder {\n",
|
| - "classname", descriptor_->name());
|
| - }
|
| + printer->Print(
|
| + "public interface $classname$OrBuilder extends\n"
|
| + " $extra_interfaces$\n"
|
| + " com.google.protobuf.MessageOrBuilder {\n",
|
| + "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_),
|
| + "classname", descriptor_->name());
|
| }
|
|
|
| printer->Indent();
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| printer->Print("\n");
|
| - PrintFieldComment(printer, descriptor_->field(i));
|
| field_generators_.get(descriptor_->field(i))
|
| .GenerateInterfaceMembers(printer);
|
| }
|
| @@ -296,55 +262,38 @@ void MessageGenerator::GenerateInterface(io::Printer* printer) {
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::Generate(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::Generate(io::Printer* printer) {
|
| bool is_own_file =
|
| descriptor_->containing_type() == NULL &&
|
| - descriptor_->file()->options().java_multiple_files();
|
| + MultipleJavaFiles(descriptor_->file(), /* immutable = */ true);
|
| +
|
| + map<string, string> variables;
|
| + variables["static"] = is_own_file ? " " : " static ";
|
| + variables["classname"] = descriptor_->name();
|
| + variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
|
|
|
| WriteMessageDocComment(printer, descriptor_);
|
|
|
| // The builder_type stores the super type name of the nested Builder class.
|
| string builder_type;
|
| if (descriptor_->extension_range_count() > 0) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public $static$ final class $classname$ extends\n"
|
| - " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n"
|
| - " $classname$> implements $classname$OrBuilder {\n",
|
| - "static", is_own_file ? "" : "static",
|
| - "classname", descriptor_->name());
|
| - builder_type = strings::Substitute(
|
| - "com.google.protobuf.GeneratedMessage.ExtendableBuilder<$0, ?>",
|
| - ClassName(descriptor_));
|
| - } else {
|
| - printer->Print(
|
| - "public $static$ final class $classname$ extends\n"
|
| - " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n"
|
| - " $classname$> implements $classname$OrBuilder {\n",
|
| - "static", is_own_file ? "" : "static",
|
| - "classname", descriptor_->name());
|
| - builder_type = strings::Substitute(
|
| - "com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>",
|
| - ClassName(descriptor_));
|
| - }
|
| + printer->Print(variables,
|
| + "public $static$final class $classname$ extends\n"
|
| + " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n"
|
| + " $classname$> implements\n"
|
| + " $extra_interfaces$\n"
|
| + " $classname$OrBuilder {\n");
|
| + builder_type = strings::Substitute(
|
| + "com.google.protobuf.GeneratedMessage.ExtendableBuilder<$0, ?>",
|
| + name_resolver_->GetImmutableClassName(descriptor_));
|
| } else {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public $static$ final class $classname$ extends\n"
|
| - " com.google.protobuf.GeneratedMessage\n"
|
| - " implements $classname$OrBuilder {\n",
|
| - "static", is_own_file ? "" : "static",
|
| - "classname", descriptor_->name());
|
| - builder_type = "com.google.protobuf.GeneratedMessage.Builder<?>";
|
| - } else {
|
| - printer->Print(
|
| - "public $static$ final class $classname$ extends\n"
|
| - " com.google.protobuf.GeneratedMessageLite\n"
|
| - " implements $classname$OrBuilder {\n",
|
| - "static", is_own_file ? "" : "static",
|
| - "classname", descriptor_->name());
|
| - builder_type = "com.google.protobuf.GeneratedMessageLite.Builder";
|
| - }
|
| + printer->Print(variables,
|
| + "public $static$final class $classname$ extends\n"
|
| + " com.google.protobuf.GeneratedMessage implements\n"
|
| + " $extra_interfaces$\n"
|
| + " $classname$OrBuilder {\n");
|
| +
|
| + builder_type = "com.google.protobuf.GeneratedMessage.Builder";
|
| }
|
| printer->Indent();
|
| // Using builder_type, instead of Builder, prevents the Builder class from
|
| @@ -355,75 +304,139 @@ void MessageGenerator::Generate(io::Printer* printer) {
|
| "// Use $classname$.newBuilder() to construct.\n"
|
| "private $classname$($buildertype$ builder) {\n"
|
| " super(builder);\n"
|
| - "$set_unknown_fields$\n"
|
| "}\n",
|
| "classname", descriptor_->name(),
|
| - "buildertype", builder_type,
|
| - "set_unknown_fields", HasUnknownFields(descriptor_)
|
| - ? " this.unknownFields = builder.getUnknownFields();" : "");
|
| + "buildertype", builder_type);
|
| + printer->Print(
|
| + "private $classname$() {\n",
|
| + "classname", descriptor_->name());
|
| + printer->Indent();
|
| + GenerateInitializers(printer);
|
| + printer->Outdent();
|
| printer->Print(
|
| - // Used when constructing the default instance, which cannot be initialized
|
| - // immediately because it may cyclically refer to other default instances.
|
| - "private $classname$(boolean noInit) {$set_default_unknown_fields$}\n"
|
| - "\n"
|
| - "private static final $classname$ defaultInstance;\n"
|
| - "public static $classname$ getDefaultInstance() {\n"
|
| - " return defaultInstance;\n"
|
| - "}\n"
|
| - "\n"
|
| - "public $classname$ getDefaultInstanceForType() {\n"
|
| - " return defaultInstance;\n"
|
| "}\n"
|
| - "\n",
|
| - "classname", descriptor_->name(),
|
| - "set_default_unknown_fields", HasUnknownFields(descriptor_)
|
| - ? " this.unknownFields ="
|
| - " com.google.protobuf.UnknownFieldSet.getDefaultInstance(); " : "");
|
| + "\n");
|
|
|
| - if (HasUnknownFields(descriptor_)) {
|
| + printer->Print(
|
| + "@java.lang.Override\n"
|
| + "public final com.google.protobuf.UnknownFieldSet\n"
|
| + "getUnknownFields() {\n");
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| printer->Print(
|
| - "private final com.google.protobuf.UnknownFieldSet unknownFields;\n"
|
| - ""
|
| - "@java.lang.Override\n"
|
| - "public final com.google.protobuf.UnknownFieldSet\n"
|
| - " getUnknownFields() {\n"
|
| - " return this.unknownFields;\n"
|
| - "}\n");
|
| + " return this.unknownFields;\n");
|
| + } else {
|
| + printer->Print(
|
| + " return com.google.protobuf.UnknownFieldSet.getDefaultInstance();\n");
|
| }
|
| + printer->Print(
|
| + "}\n");
|
|
|
| if (HasGeneratedMethods(descriptor_)) {
|
| GenerateParsingConstructor(printer);
|
| }
|
|
|
| GenerateDescriptorMethods(printer);
|
| - GenerateParser(printer);
|
|
|
| // Nested types
|
| for (int i = 0; i < descriptor_->enum_type_count(); i++) {
|
| - EnumGenerator(descriptor_->enum_type(i)).Generate(printer);
|
| + EnumGenerator(descriptor_->enum_type(i), true, context_)
|
| + .Generate(printer);
|
| }
|
|
|
| for (int i = 0; i < descriptor_->nested_type_count(); i++) {
|
| - MessageGenerator messageGenerator(descriptor_->nested_type(i));
|
| + // Don't generate Java classes for map entry messages.
|
| + if (IsMapEntry(descriptor_->nested_type(i))) continue;
|
| + ImmutableMessageGenerator messageGenerator(
|
| + descriptor_->nested_type(i), context_);
|
| messageGenerator.GenerateInterface(printer);
|
| messageGenerator.Generate(printer);
|
| }
|
|
|
| - // Integers for bit fields.
|
| - int totalBits = 0;
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - totalBits += field_generators_.get(descriptor_->field(i))
|
| - .GetNumBitsForMessage();
|
| + if (GenerateHasBits(descriptor_)) {
|
| + // Integers for bit fields.
|
| + int totalBits = 0;
|
| + for (int i = 0; i < descriptor_->field_count(); i++) {
|
| + totalBits += field_generators_.get(descriptor_->field(i))
|
| + .GetNumBitsForMessage();
|
| + }
|
| + int totalInts = (totalBits + 31) / 32;
|
| + for (int i = 0; i < totalInts; i++) {
|
| + printer->Print("private int $bit_field_name$;\n",
|
| + "bit_field_name", GetBitFieldName(i));
|
| + }
|
| }
|
| - int totalInts = (totalBits + 31) / 32;
|
| - for (int i = 0; i < totalInts; i++) {
|
| - printer->Print("private int $bit_field_name$;\n",
|
| - "bit_field_name", GetBitFieldName(i));
|
| +
|
| + // oneof
|
| + map<string, string> vars;
|
| + for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
|
| + vars["oneof_name"] = context_->GetOneofGeneratorInfo(
|
| + descriptor_->oneof_decl(i))->name;
|
| + vars["oneof_capitalized_name"] = context_->GetOneofGeneratorInfo(
|
| + descriptor_->oneof_decl(i))->capitalized_name;
|
| + vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index());
|
| + // oneofCase_ and oneof_
|
| + printer->Print(vars,
|
| + "private int $oneof_name$Case_ = 0;\n"
|
| + "private java.lang.Object $oneof_name$_;\n");
|
| + // OneofCase enum
|
| + printer->Print(vars,
|
| + "public enum $oneof_capitalized_name$Case\n"
|
| + " implements com.google.protobuf.Internal.EnumLite {\n");
|
| + printer->Indent();
|
| + for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
|
| + const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
|
| + printer->Print(
|
| + "$field_name$($field_number$),\n",
|
| + "field_name",
|
| + ToUpper(field->name()),
|
| + "field_number",
|
| + SimpleItoa(field->number()));
|
| + }
|
| + printer->Print(
|
| + "$cap_oneof_name$_NOT_SET(0);\n",
|
| + "cap_oneof_name",
|
| + ToUpper(vars["oneof_name"]));
|
| + printer->Print(vars,
|
| + "private int value = 0;\n"
|
| + "private $oneof_capitalized_name$Case(int value) {\n"
|
| + " this.value = value;\n"
|
| + "}\n");
|
| + printer->Print(vars,
|
| + "public static $oneof_capitalized_name$Case valueOf(int value) {\n"
|
| + " switch (value) {\n");
|
| + for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
|
| + const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
|
| + printer->Print(
|
| + " case $field_number$: return $field_name$;\n",
|
| + "field_number",
|
| + SimpleItoa(field->number()),
|
| + "field_name",
|
| + ToUpper(field->name()));
|
| + }
|
| + printer->Print(
|
| + " case 0: return $cap_oneof_name$_NOT_SET;\n"
|
| + " default: throw new java.lang.IllegalArgumentException(\n"
|
| + " \"Value is undefined for this oneof enum.\");\n"
|
| + " }\n"
|
| + "}\n"
|
| + "public int getNumber() {\n"
|
| + " return this.value;\n"
|
| + "}\n",
|
| + "cap_oneof_name", ToUpper(vars["oneof_name"]));
|
| + printer->Outdent();
|
| + printer->Print("};\n\n");
|
| + // oneofCase()
|
| + printer->Print(vars,
|
| + "public $oneof_capitalized_name$Case\n"
|
| + "get$oneof_capitalized_name$Case() {\n"
|
| + " return $oneof_capitalized_name$Case.valueOf(\n"
|
| + " $oneof_name$Case_);\n"
|
| + "}\n"
|
| + "\n");
|
| }
|
|
|
| // Fields
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - PrintFieldComment(printer, descriptor_->field(i));
|
| printer->Print("public static final int $constant_name$ = $number$;\n",
|
| "constant_name", FieldConstantName(descriptor_->field(i)),
|
| "number", SimpleItoa(descriptor_->field(i)->number()));
|
| @@ -431,19 +444,8 @@ void MessageGenerator::Generate(io::Printer* printer) {
|
| printer->Print("\n");
|
| }
|
|
|
| - // Called by the constructor, except in the case of the default instance,
|
| - // in which case this is called by static init code later on.
|
| - printer->Print("private void initFields() {\n");
|
| - printer->Indent();
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - field_generators_.get(descriptor_->field(i))
|
| - .GenerateInitializationCode(printer);
|
| - }
|
| - printer->Outdent();
|
| - printer->Print("}\n");
|
| -
|
| if (HasGeneratedMethods(descriptor_)) {
|
| - GenerateIsInitialized(printer, MEMOIZE);
|
| + GenerateIsInitialized(printer);
|
| GenerateMessageSerializationMethods(printer);
|
| }
|
|
|
| @@ -451,27 +453,50 @@ void MessageGenerator::Generate(io::Printer* printer) {
|
| GenerateEqualsAndHashCode(printer);
|
| }
|
|
|
| +
|
| GenerateParseFromMethods(printer);
|
| GenerateBuilder(printer);
|
|
|
| + printer->Print(
|
| + "\n"
|
| + "// @@protoc_insertion_point(class_scope:$full_name$)\n",
|
| + "full_name", descriptor_->full_name());
|
| +
|
| +
|
| // Carefully initialize the default instance in such a way that it doesn't
|
| // conflict with other initialization.
|
| printer->Print(
|
| - "\n"
|
| + "private static final $classname$ DEFAULT_INSTANCE;\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| + printer->Print(
|
| "static {\n"
|
| - " defaultInstance = new $classname$(true);\n"
|
| - " defaultInstance.initFields();\n"
|
| + " DEFAULT_INSTANCE = new $classname$();\n"
|
| "}\n"
|
| - "\n"
|
| - "// @@protoc_insertion_point(class_scope:$full_name$)\n",
|
| - "classname", descriptor_->name(),
|
| - "full_name", descriptor_->full_name());
|
| + "\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
|
|
| - // Extensions must be declared after the defaultInstance is initialized
|
| - // because the defaultInstance is used by the extension to lazily retrieve
|
| + printer->Print(
|
| + "public static $classname$ getDefaultInstance() {\n"
|
| + " return DEFAULT_INSTANCE;\n"
|
| + "}\n"
|
| + "\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| +
|
| + GenerateParser(printer);
|
| +
|
| + printer->Print(
|
| + "public $classname$ getDefaultInstanceForType() {\n"
|
| + " return DEFAULT_INSTANCE;\n"
|
| + "}\n"
|
| + "\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| +
|
| + // Extensions must be declared after the DEFAULT_INSTANCE is initialized
|
| + // because the DEFAULT_INSTANCE is used by the extension to lazily retrieve
|
| // the outer class's FileDescriptor.
|
| for (int i = 0; i < descriptor_->extension_count(); i++) {
|
| - ExtensionGenerator(descriptor_->extension(i)).Generate(printer);
|
| + ImmutableExtensionGenerator(descriptor_->extension(i), context_)
|
| + .Generate(printer);
|
| }
|
|
|
| printer->Outdent();
|
| @@ -481,46 +506,46 @@ void MessageGenerator::Generate(io::Printer* printer) {
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::
|
| +void ImmutableMessageGenerator::
|
| GenerateMessageSerializationMethods(io::Printer* printer) {
|
| - scoped_array<const FieldDescriptor*> sorted_fields(
|
| - SortFieldsByNumber(descriptor_));
|
| + google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields(
|
| + SortFieldsByNumber(descriptor_));
|
|
|
| vector<const Descriptor::ExtensionRange*> sorted_extensions;
|
| for (int i = 0; i < descriptor_->extension_range_count(); ++i) {
|
| sorted_extensions.push_back(descriptor_->extension_range(i));
|
| }
|
| - sort(sorted_extensions.begin(), sorted_extensions.end(),
|
| - ExtensionRangeOrdering());
|
| + std::sort(sorted_extensions.begin(), sorted_extensions.end(),
|
| + ExtensionRangeOrdering());
|
|
|
| printer->Print(
|
| "public void writeTo(com.google.protobuf.CodedOutputStream output)\n"
|
| " throws java.io.IOException {\n");
|
| printer->Indent();
|
| - // writeTo(CodedOutputStream output) might be invoked without
|
| - // getSerializedSize() ever being called, but we need the memoized
|
| - // sizes in case this message has packed fields. Rather than emit checks for
|
| - // each packed field, just call getSerializedSize() up front for all messages.
|
| - // In most cases, getSerializedSize() will have already been called anyway by
|
| - // one of the wrapper writeTo() methods, making this call cheap.
|
| - printer->Print(
|
| - "getSerializedSize();\n");
|
| + if (HasPackedFields(descriptor_)) {
|
| + // writeTo(CodedOutputStream output) might be invoked without
|
| + // getSerializedSize() ever being called, but we need the memoized
|
| + // sizes in case this message has packed fields. Rather than emit checks for
|
| + // each packed field, just call getSerializedSize() up front.
|
| + // In most cases, getSerializedSize() will have already been called anyway
|
| + // by one of the wrapper writeTo() methods, making this call cheap.
|
| + printer->Print(
|
| + "getSerializedSize();\n");
|
| + }
|
|
|
| if (descriptor_->extension_range_count() > 0) {
|
| if (descriptor_->options().message_set_wire_format()) {
|
| printer->Print(
|
| - "com.google.protobuf.GeneratedMessage$lite$\n"
|
| - " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n"
|
| - " newMessageSetExtensionWriter();\n",
|
| - "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite",
|
| - "classname", ClassName(descriptor_));
|
| + "com.google.protobuf.GeneratedMessage\n"
|
| + " .ExtendableMessage<$classname$>.ExtensionWriter\n"
|
| + " extensionWriter = newMessageSetExtensionWriter();\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| } else {
|
| printer->Print(
|
| - "com.google.protobuf.GeneratedMessage$lite$\n"
|
| - " .ExtendableMessage<$classname$>.ExtensionWriter extensionWriter =\n"
|
| - " newExtensionWriter();\n",
|
| - "lite", HasDescriptorMethods(descriptor_) ? "" : "Lite",
|
| - "classname", ClassName(descriptor_));
|
| + "com.google.protobuf.GeneratedMessage\n"
|
| + " .ExtendableMessage<$classname$>.ExtensionWriter\n"
|
| + " extensionWriter = newExtensionWriter();\n",
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| }
|
| }
|
|
|
| @@ -539,13 +564,13 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
|
| }
|
| }
|
|
|
| - if (HasUnknownFields(descriptor_)) {
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| if (descriptor_->options().message_set_wire_format()) {
|
| printer->Print(
|
| - "getUnknownFields().writeAsMessageSetTo(output);\n");
|
| + "unknownFields.writeAsMessageSetTo(output);\n");
|
| } else {
|
| printer->Print(
|
| - "getUnknownFields().writeTo(output);\n");
|
| + "unknownFields.writeTo(output);\n");
|
| }
|
| }
|
|
|
| @@ -575,13 +600,13 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
|
| }
|
| }
|
|
|
| - if (HasUnknownFields(descriptor_)) {
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| if (descriptor_->options().message_set_wire_format()) {
|
| printer->Print(
|
| - "size += getUnknownFields().getSerializedSizeAsMessageSet();\n");
|
| + "size += unknownFields.getSerializedSizeAsMessageSet();\n");
|
| } else {
|
| printer->Print(
|
| - "size += getUnknownFields().getSerializedSize();\n");
|
| + "size += unknownFields.getSerializedSize();\n");
|
| }
|
| }
|
|
|
| @@ -593,16 +618,10 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
|
| "\n");
|
|
|
| printer->Print(
|
| - "private static final long serialVersionUID = 0L;\n"
|
| - "@java.lang.Override\n"
|
| - "protected java.lang.Object writeReplace()\n"
|
| - " throws java.io.ObjectStreamException {\n"
|
| - " return super.writeReplace();\n"
|
| - "}\n"
|
| - "\n");
|
| + "private static final long serialVersionUID = 0L;\n");
|
| }
|
|
|
| -void MessageGenerator::
|
| +void ImmutableMessageGenerator::
|
| GenerateParseFromMethods(io::Printer* printer) {
|
| // Note: These are separate from GenerateMessageSerializationMethods()
|
| // because they need to be generated even for messages that are optimized
|
| @@ -661,15 +680,15 @@ GenerateParseFromMethods(io::Printer* printer) {
|
| " return PARSER.parseFrom(input, extensionRegistry);\n"
|
| "}\n"
|
| "\n",
|
| - "classname", ClassName(descriptor_));
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
| }
|
|
|
| -void MessageGenerator::GenerateSerializeOneField(
|
| +void ImmutableMessageGenerator::GenerateSerializeOneField(
|
| io::Printer* printer, const FieldDescriptor* field) {
|
| field_generators_.get(field).GenerateSerializationCode(printer);
|
| }
|
|
|
| -void MessageGenerator::GenerateSerializeOneExtensionRange(
|
| +void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange(
|
| io::Printer* printer, const Descriptor::ExtensionRange* range) {
|
| printer->Print(
|
| "extensionWriter.writeUntil($end$, output);\n",
|
| @@ -678,429 +697,198 @@ void MessageGenerator::GenerateSerializeOneExtensionRange(
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::GenerateBuilder(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) {
|
| + // LITE_RUNTIME implements this at the GeneratedMessageLite level.
|
| printer->Print(
|
| - "public static Builder newBuilder() { return Builder.create(); }\n"
|
| - "public Builder newBuilderForType() { return newBuilder(); }\n"
|
| + "public Builder newBuilderForType() { return newBuilder(); }\n");
|
| +
|
| + printer->Print(
|
| + "public static Builder newBuilder() {\n"
|
| + " return DEFAULT_INSTANCE.toBuilder();\n"
|
| + "}\n"
|
| "public static Builder newBuilder($classname$ prototype) {\n"
|
| - " return newBuilder().mergeFrom(prototype);\n"
|
| + " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n"
|
| + "}\n"
|
| + "public Builder toBuilder() {\n"
|
| + " return this == DEFAULT_INSTANCE\n"
|
| + " ? new Builder() : new Builder().mergeFrom(this);\n"
|
| "}\n"
|
| - "public Builder toBuilder() { return newBuilder(this); }\n"
|
| "\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - if (HasNestedBuilders(descriptor_)) {
|
| - printer->Print(
|
| - "@java.lang.Override\n"
|
| - "protected Builder newBuilderForType(\n"
|
| - " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
|
| - " Builder builder = new Builder(parent);\n"
|
| - " return builder;\n"
|
| - "}\n");
|
| - }
|
| -
|
| - WriteMessageDocComment(printer, descriptor_);
|
| -
|
| - if (descriptor_->extension_range_count() > 0) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public static final class Builder extends\n"
|
| - " com.google.protobuf.GeneratedMessage.ExtendableBuilder<\n"
|
| - " $classname$, Builder> implements $classname$OrBuilder {\n",
|
| - "classname", ClassName(descriptor_));
|
| - } else {
|
| - printer->Print(
|
| - "public static final class Builder extends\n"
|
| - " com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<\n"
|
| - " $classname$, Builder> implements $classname$OrBuilder {\n",
|
| - "classname", ClassName(descriptor_));
|
| - }
|
| - } else {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public static final class Builder extends\n"
|
| - " com.google.protobuf.GeneratedMessage.Builder<Builder>\n"
|
| - " implements $classname$OrBuilder {\n",
|
| - "classname", ClassName(descriptor_));
|
| - } else {
|
| - printer->Print(
|
| - "public static final class Builder extends\n"
|
| - " com.google.protobuf.GeneratedMessageLite.Builder<\n"
|
| - " $classname$, Builder>\n"
|
| - " implements $classname$OrBuilder {\n",
|
| - "classname", ClassName(descriptor_));
|
| - }
|
| - }
|
| - printer->Indent();
|
| -
|
| - GenerateDescriptorMethods(printer);
|
| - GenerateCommonBuilderMethods(printer);
|
| -
|
| - if (HasGeneratedMethods(descriptor_)) {
|
| - GenerateIsInitialized(printer, DONT_MEMOIZE);
|
| - GenerateBuilderParsingMethods(printer);
|
| - }
|
| -
|
| - // Integers for bit fields.
|
| - int totalBits = 0;
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - totalBits += field_generators_.get(descriptor_->field(i))
|
| - .GetNumBitsForBuilder();
|
| - }
|
| - int totalInts = (totalBits + 31) / 32;
|
| - for (int i = 0; i < totalInts; i++) {
|
| - printer->Print("private int $bit_field_name$;\n",
|
| - "bit_field_name", GetBitFieldName(i));
|
| - }
|
| -
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - printer->Print("\n");
|
| - PrintFieldComment(printer, descriptor_->field(i));
|
| - field_generators_.get(descriptor_->field(i))
|
| - .GenerateBuilderMembers(printer);
|
| - }
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
|
|
| printer->Print(
|
| - "\n"
|
| - "// @@protoc_insertion_point(builder_scope:$full_name$)\n",
|
| - "full_name", descriptor_->full_name());
|
| -
|
| - printer->Outdent();
|
| - printer->Print("}\n");
|
| + "@java.lang.Override\n"
|
| + "protected Builder newBuilderForType(\n"
|
| + " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
|
| + " Builder builder = new Builder(parent);\n"
|
| + " return builder;\n"
|
| + "}\n");
|
| +
|
| + MessageBuilderGenerator builderGenerator(descriptor_, context_);
|
| + builderGenerator.Generate(printer);
|
| }
|
|
|
| -void MessageGenerator::GenerateDescriptorMethods(io::Printer* printer) {
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - if (!descriptor_->options().no_standard_descriptor_accessor()) {
|
| - printer->Print(
|
| - "public static final com.google.protobuf.Descriptors.Descriptor\n"
|
| - " getDescriptor() {\n"
|
| - " return $fileclass$.internal_$identifier$_descriptor;\n"
|
| - "}\n"
|
| - "\n",
|
| - "fileclass", ClassName(descriptor_->file()),
|
| - "identifier", UniqueFileScopeIdentifier(descriptor_));
|
| - }
|
| +void ImmutableMessageGenerator::
|
| +GenerateDescriptorMethods(io::Printer* printer) {
|
| + if (!descriptor_->options().no_standard_descriptor_accessor()) {
|
| printer->Print(
|
| - "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
|
| - " internalGetFieldAccessorTable() {\n"
|
| - " return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
|
| - " .ensureFieldAccessorsInitialized(\n"
|
| - " $classname$.class, $classname$.Builder.class);\n"
|
| + "public static final com.google.protobuf.Descriptors.Descriptor\n"
|
| + " getDescriptor() {\n"
|
| + " return $fileclass$.internal_$identifier$_descriptor;\n"
|
| "}\n"
|
| "\n",
|
| - "classname", ClassName(descriptor_),
|
| - "fileclass", ClassName(descriptor_->file()),
|
| + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
|
| "identifier", UniqueFileScopeIdentifier(descriptor_));
|
| }
|
| -}
|
| -
|
| -// ===================================================================
|
| -
|
| -void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
|
| - printer->Print(
|
| - "// Construct using $classname$.newBuilder()\n"
|
| - "private Builder() {\n"
|
| - " maybeForceBuilderInitialization();\n"
|
| - "}\n"
|
| - "\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "private Builder(\n"
|
| - " com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
|
| - " super(parent);\n"
|
| - " maybeForceBuilderInitialization();\n"
|
| - "}\n",
|
| - "classname", ClassName(descriptor_));
|
| + vector<const FieldDescriptor*> map_fields;
|
| + for (int i = 0; i < descriptor_->field_count(); i++) {
|
| + const FieldDescriptor* field = descriptor_->field(i);
|
| + if (GetJavaType(field) == JAVATYPE_MESSAGE &&
|
| + IsMapEntry(field->message_type())) {
|
| + map_fields.push_back(field);
|
| + }
|
| }
|
| -
|
| -
|
| - if (HasNestedBuilders(descriptor_)) {
|
| + if (!map_fields.empty()) {
|
| printer->Print(
|
| - "private void maybeForceBuilderInitialization() {\n"
|
| - " if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {\n");
|
| -
|
| + "@SuppressWarnings({\"rawtypes\"})\n"
|
| + "protected com.google.protobuf.MapField internalGetMapField(\n"
|
| + " int number) {\n"
|
| + " switch (number) {\n");
|
| printer->Indent();
|
| printer->Indent();
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - field_generators_.get(descriptor_->field(i))
|
| - .GenerateFieldBuilderInitializationCode(printer);
|
| + for (int i = 0; i < map_fields.size(); ++i) {
|
| + const FieldDescriptor* field = map_fields[i];
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
| + printer->Print(
|
| + "case $number$:\n"
|
| + " return internalGet$capitalized_name$();\n",
|
| + "number", SimpleItoa(field->number()),
|
| + "capitalized_name", info->capitalized_name);
|
| }
|
| + printer->Print(
|
| + "default:\n"
|
| + " throw new RuntimeException(\n"
|
| + " \"Invalid map field number: \" + number);\n");
|
| printer->Outdent();
|
| printer->Outdent();
|
| -
|
| - printer->Print(
|
| - " }\n"
|
| - "}\n");
|
| - } else {
|
| - printer->Print(
|
| - "private void maybeForceBuilderInitialization() {\n"
|
| - "}\n");
|
| - }
|
| -
|
| - printer->Print(
|
| - "private static Builder create() {\n"
|
| - " return new Builder();\n"
|
| - "}\n"
|
| - "\n"
|
| - "public Builder clear() {\n"
|
| - " super.clear();\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - printer->Indent();
|
| -
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - field_generators_.get(descriptor_->field(i))
|
| - .GenerateBuilderClearCode(printer);
|
| - }
|
| -
|
| - printer->Outdent();
|
| -
|
| - printer->Print(
|
| - " return this;\n"
|
| - "}\n"
|
| - "\n"
|
| - "public Builder clone() {\n"
|
| - " return create().mergeFrom(buildPartial());\n"
|
| - "}\n"
|
| - "\n",
|
| - "classname", ClassName(descriptor_));
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public com.google.protobuf.Descriptors.Descriptor\n"
|
| - " getDescriptorForType() {\n"
|
| - " return $fileclass$.internal_$identifier$_descriptor;\n"
|
| - "}\n"
|
| - "\n",
|
| - "fileclass", ClassName(descriptor_->file()),
|
| - "identifier", UniqueFileScopeIdentifier(descriptor_));
|
| - }
|
| - printer->Print(
|
| - "public $classname$ getDefaultInstanceForType() {\n"
|
| - " return $classname$.getDefaultInstance();\n"
|
| - "}\n"
|
| - "\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - // -----------------------------------------------------------------
|
| -
|
| - printer->Print(
|
| - "public $classname$ build() {\n"
|
| - " $classname$ result = buildPartial();\n"
|
| - " if (!result.isInitialized()) {\n"
|
| - " throw newUninitializedMessageException(result);\n"
|
| - " }\n"
|
| - " return result;\n"
|
| - "}\n"
|
| - "\n"
|
| - "public $classname$ buildPartial() {\n"
|
| - " $classname$ result = new $classname$(this);\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - printer->Indent();
|
| -
|
| - // Local vars for from and to bit fields to avoid accessing the builder and
|
| - // message over and over for these fields. Seems to provide a slight
|
| - // perforamance improvement in micro benchmark and this is also what proto1
|
| - // code does.
|
| - int totalBuilderBits = 0;
|
| - int totalMessageBits = 0;
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - const FieldGenerator& field = field_generators_.get(descriptor_->field(i));
|
| - totalBuilderBits += field.GetNumBitsForBuilder();
|
| - totalMessageBits += field.GetNumBitsForMessage();
|
| - }
|
| - int totalBuilderInts = (totalBuilderBits + 31) / 32;
|
| - int totalMessageInts = (totalMessageBits + 31) / 32;
|
| - for (int i = 0; i < totalBuilderInts; i++) {
|
| - printer->Print("int from_$bit_field_name$ = $bit_field_name$;\n",
|
| - "bit_field_name", GetBitFieldName(i));
|
| - }
|
| - for (int i = 0; i < totalMessageInts; i++) {
|
| - printer->Print("int to_$bit_field_name$ = 0;\n",
|
| - "bit_field_name", GetBitFieldName(i));
|
| - }
|
| -
|
| - // Output generation code for each field.
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - field_generators_.get(descriptor_->field(i)).GenerateBuildingCode(printer);
|
| - }
|
| -
|
| - // Copy the bit field results to the generated message
|
| - for (int i = 0; i < totalMessageInts; i++) {
|
| - printer->Print("result.$bit_field_name$ = to_$bit_field_name$;\n",
|
| - "bit_field_name", GetBitFieldName(i));
|
| - }
|
| -
|
| - printer->Outdent();
|
| -
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| printer->Print(
|
| - " onBuilt();\n");
|
| + " }\n"
|
| + "}\n");
|
| }
|
| -
|
| printer->Print(
|
| - " return result;\n"
|
| + "protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
|
| + " internalGetFieldAccessorTable() {\n"
|
| + " return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
|
| + " .ensureFieldAccessorsInitialized(\n"
|
| + " $classname$.class, $classname$.Builder.class);\n"
|
| "}\n"
|
| "\n",
|
| - "classname", ClassName(descriptor_));
|
| -
|
| - // -----------------------------------------------------------------
|
| -
|
| - if (HasGeneratedMethods(descriptor_)) {
|
| - // MergeFrom(Message other) requires the ability to distinguish the other
|
| - // messages type by its descriptor.
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - printer->Print(
|
| - "public Builder mergeFrom(com.google.protobuf.Message other) {\n"
|
| - " if (other instanceof $classname$) {\n"
|
| - " return mergeFrom(($classname$)other);\n"
|
| - " } else {\n"
|
| - " super.mergeFrom(other);\n"
|
| - " return this;\n"
|
| - " }\n"
|
| - "}\n"
|
| - "\n",
|
| - "classname", ClassName(descriptor_));
|
| - }
|
| -
|
| - printer->Print(
|
| - "public Builder mergeFrom($classname$ other) {\n"
|
| - // Optimization: If other is the default instance, we know none of its
|
| - // fields are set so we can skip the merge.
|
| - " if (other == $classname$.getDefaultInstance()) return this;\n",
|
| - "classname", ClassName(descriptor_));
|
| - printer->Indent();
|
| -
|
| - for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - field_generators_.get(descriptor_->field(i)).GenerateMergingCode(printer);
|
| - }
|
| -
|
| - printer->Outdent();
|
| -
|
| - // if message type has extensions
|
| - if (descriptor_->extension_range_count() > 0) {
|
| - printer->Print(
|
| - " this.mergeExtensionFields(other);\n");
|
| - }
|
| -
|
| - if (HasUnknownFields(descriptor_)) {
|
| - printer->Print(
|
| - " this.mergeUnknownFields(other.getUnknownFields());\n");
|
| - }
|
| -
|
| - printer->Print(
|
| - " return this;\n"
|
| - "}\n"
|
| - "\n");
|
| - }
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_),
|
| + "fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
|
| + "identifier", UniqueFileScopeIdentifier(descriptor_));
|
| }
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::GenerateBuilderParsingMethods(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::GenerateIsInitialized(
|
| + io::Printer* printer) {
|
| + // Memoizes whether the protocol buffer is fully initialized (has all
|
| + // required fields). -1 means not yet computed. 0 means false and 1 means
|
| + // true.
|
| printer->Print(
|
| - "public Builder mergeFrom(\n"
|
| - " com.google.protobuf.CodedInputStream input,\n"
|
| - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
|
| - " throws java.io.IOException {\n"
|
| - " $classname$ parsedMessage = null;\n"
|
| - " try {\n"
|
| - " parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);\n"
|
| - " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
|
| - " parsedMessage = ($classname$) e.getUnfinishedMessage();\n"
|
| - " throw e;\n"
|
| - " } finally {\n"
|
| - " if (parsedMessage != null) {\n"
|
| - " mergeFrom(parsedMessage);\n"
|
| - " }\n"
|
| - " }\n"
|
| - " return this;\n"
|
| - "}\n",
|
| - "classname", ClassName(descriptor_));
|
| -}
|
| -
|
| -// ===================================================================
|
| -
|
| -void MessageGenerator::GenerateIsInitialized(
|
| - io::Printer* printer, UseMemoization useMemoization) {
|
| - bool memoization = useMemoization == MEMOIZE;
|
| - if (memoization) {
|
| - // Memoizes whether the protocol buffer is fully initialized (has all
|
| - // required fields). -1 means not yet computed. 0 means false and 1 means
|
| - // true.
|
| - printer->Print(
|
| - "private byte memoizedIsInitialized = -1;\n");
|
| - }
|
| + "private byte memoizedIsInitialized = -1;\n");
|
| printer->Print(
|
| "public final boolean isInitialized() {\n");
|
| printer->Indent();
|
|
|
| - if (memoization) {
|
| - printer->Print(
|
| - "byte isInitialized = memoizedIsInitialized;\n"
|
| - "if (isInitialized != -1) return isInitialized == 1;\n"
|
| - "\n");
|
| - }
|
| + // Don't directly compare to -1 to avoid an Android x86 JIT bug.
|
| + printer->Print(
|
| + "byte isInitialized = memoizedIsInitialized;\n"
|
| + "if (isInitialized == 1) return true;\n"
|
| + "if (isInitialized == 0) return false;\n"
|
| + "\n");
|
|
|
| // Check that all required fields in this message are set.
|
| // TODO(kenton): We can optimize this when we switch to putting all the
|
| // "has" fields into a single bitfield.
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| const FieldDescriptor* field = descriptor_->field(i);
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
|
|
| if (field->is_required()) {
|
| printer->Print(
|
| "if (!has$name$()) {\n"
|
| - " $memoize$\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| " return false;\n"
|
| "}\n",
|
| - "name", UnderscoresToCapitalizedCamelCase(field),
|
| - "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
|
| + "name", info->capitalized_name);
|
| }
|
| }
|
|
|
| // Now check that all embedded messages are initialized.
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| const FieldDescriptor* field = descriptor_->field(i);
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
| if (GetJavaType(field) == JAVATYPE_MESSAGE &&
|
| HasRequiredFields(field->message_type())) {
|
| switch (field->label()) {
|
| case FieldDescriptor::LABEL_REQUIRED:
|
| printer->Print(
|
| "if (!get$name$().isInitialized()) {\n"
|
| - " $memoize$\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| " return false;\n"
|
| "}\n",
|
| - "type", ClassName(field->message_type()),
|
| - "name", UnderscoresToCapitalizedCamelCase(field),
|
| - "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
|
| + "type", name_resolver_->GetImmutableClassName(
|
| + field->message_type()),
|
| + "name", info->capitalized_name);
|
| break;
|
| case FieldDescriptor::LABEL_OPTIONAL:
|
| + if (!SupportFieldPresence(descriptor_->file()) &&
|
| + field->containing_oneof() != NULL) {
|
| + const OneofDescriptor* oneof = field->containing_oneof();
|
| + const OneofGeneratorInfo* oneof_info =
|
| + context_->GetOneofGeneratorInfo(oneof);
|
| + printer->Print(
|
| + "if ($oneof_name$Case_ == $field_number$) {\n",
|
| + "oneof_name", oneof_info->name,
|
| + "field_number", SimpleItoa(field->number()));
|
| + } else {
|
| + printer->Print(
|
| + "if (has$name$()) {\n",
|
| + "name", info->capitalized_name);
|
| + }
|
| printer->Print(
|
| - "if (has$name$()) {\n"
|
| " if (!get$name$().isInitialized()) {\n"
|
| - " $memoize$\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| " return false;\n"
|
| " }\n"
|
| "}\n",
|
| - "type", ClassName(field->message_type()),
|
| - "name", UnderscoresToCapitalizedCamelCase(field),
|
| - "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
|
| + "name", info->capitalized_name);
|
| break;
|
| case FieldDescriptor::LABEL_REPEATED:
|
| - printer->Print(
|
| - "for (int i = 0; i < get$name$Count(); i++) {\n"
|
| - " if (!get$name$(i).isInitialized()) {\n"
|
| - " $memoize$\n"
|
| - " return false;\n"
|
| - " }\n"
|
| - "}\n",
|
| - "type", ClassName(field->message_type()),
|
| - "name", UnderscoresToCapitalizedCamelCase(field),
|
| - "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
|
| + if (IsMapEntry(field->message_type())) {
|
| + printer->Print(
|
| + "for ($type$ item : get$name$().values()) {\n"
|
| + " if (!item.isInitialized()) {\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| + " return false;\n"
|
| + " }\n"
|
| + "}\n",
|
| + "type", MapValueImmutableClassdName(field->message_type(),
|
| + name_resolver_),
|
| + "name", info->capitalized_name);
|
| + } else {
|
| + printer->Print(
|
| + "for (int i = 0; i < get$name$Count(); i++) {\n"
|
| + " if (!get$name$(i).isInitialized()) {\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| + " return false;\n"
|
| + " }\n"
|
| + "}\n",
|
| + "type", name_resolver_->GetImmutableClassName(
|
| + field->message_type()),
|
| + "name", info->capitalized_name);
|
| + }
|
| break;
|
| }
|
| }
|
| @@ -1109,18 +897,15 @@ void MessageGenerator::GenerateIsInitialized(
|
| if (descriptor_->extension_range_count() > 0) {
|
| printer->Print(
|
| "if (!extensionsAreInitialized()) {\n"
|
| - " $memoize$\n"
|
| + " memoizedIsInitialized = 0;\n"
|
| " return false;\n"
|
| - "}\n",
|
| - "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
|
| + "}\n");
|
| }
|
|
|
| printer->Outdent();
|
|
|
| - if (memoization) {
|
| - printer->Print(
|
| - " memoizedIsInitialized = 1;\n");
|
| - }
|
| + printer->Print(
|
| + " memoizedIsInitialized = 1;\n");
|
|
|
| printer->Print(
|
| " return true;\n"
|
| @@ -1130,7 +915,21 @@ void MessageGenerator::GenerateIsInitialized(
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
|
| +namespace {
|
| +bool CheckHasBitsForEqualsAndHashCode(const FieldDescriptor* field) {
|
| + if (field->is_repeated()) {
|
| + return false;
|
| + }
|
| + if (SupportFieldPresence(field->file())) {
|
| + return true;
|
| + }
|
| + return GetJavaType(field) == JAVATYPE_MESSAGE &&
|
| + field->containing_oneof() == NULL;
|
| +}
|
| +} // namespace
|
| +
|
| +void ImmutableMessageGenerator::
|
| +GenerateEqualsAndHashCode(io::Printer* printer) {
|
| printer->Print(
|
| "@java.lang.Override\n"
|
| "public boolean equals(final java.lang.Object obj) {\n");
|
| @@ -1144,34 +943,38 @@ void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
|
| "}\n"
|
| "$classname$ other = ($classname$) obj;\n"
|
| "\n",
|
| - "classname", ClassName(descriptor_));
|
| + "classname", name_resolver_->GetImmutableClassName(descriptor_));
|
|
|
| printer->Print("boolean result = true;\n");
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| const FieldDescriptor* field = descriptor_->field(i);
|
| - if (!field->is_repeated()) {
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
| + bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field);
|
| + if (check_has_bits) {
|
| printer->Print(
|
| "result = result && (has$name$() == other.has$name$());\n"
|
| "if (has$name$()) {\n",
|
| - "name", UnderscoresToCapitalizedCamelCase(field));
|
| + "name", info->capitalized_name);
|
| printer->Indent();
|
| }
|
| field_generators_.get(field).GenerateEqualsCode(printer);
|
| - if (!field->is_repeated()) {
|
| + if (check_has_bits) {
|
| printer->Outdent();
|
| printer->Print(
|
| "}\n");
|
| }
|
| }
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| + // Always consider unknown fields for equality. This will sometimes return
|
| + // false for non-canonical ordering when running in LITE_RUNTIME but it's
|
| + // the best we can do.
|
| + printer->Print(
|
| + "result = result && unknownFields.equals(other.unknownFields);\n");
|
| + }
|
| + if (descriptor_->extension_range_count() > 0) {
|
| printer->Print(
|
| "result = result &&\n"
|
| - " getUnknownFields().equals(other.getUnknownFields());\n");
|
| - if (descriptor_->extension_range_count() > 0) {
|
| - printer->Print(
|
| - "result = result &&\n"
|
| - " getExtensionFields().equals(other.getExtensionFields());\n");
|
| - }
|
| + " getExtensionFields().equals(other.getExtensionFields());\n");
|
| }
|
| printer->Print(
|
| "return result;\n");
|
| @@ -1181,8 +984,6 @@ void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
|
| "\n");
|
|
|
| printer->Print(
|
| - "private int memoizedHashCode = 0;\n");
|
| - printer->Print(
|
| "@java.lang.Override\n"
|
| "public int hashCode() {\n");
|
| printer->Indent();
|
| @@ -1194,30 +995,34 @@ void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
|
| printer->Outdent();
|
| printer->Print(
|
| "}\n"
|
| - "int hash = 41;\n"
|
| - "hash = (19 * hash) + getDescriptorForType().hashCode();\n");
|
| + "int hash = 41;\n");
|
| +
|
| + printer->Print("hash = (19 * hash) + getDescriptorForType().hashCode();\n");
|
| +
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| const FieldDescriptor* field = descriptor_->field(i);
|
| - if (!field->is_repeated()) {
|
| + const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
|
| + bool check_has_bits = CheckHasBitsForEqualsAndHashCode(field);
|
| + if (check_has_bits) {
|
| printer->Print(
|
| "if (has$name$()) {\n",
|
| - "name", UnderscoresToCapitalizedCamelCase(field));
|
| + "name", info->capitalized_name);
|
| printer->Indent();
|
| }
|
| field_generators_.get(field).GenerateHashCode(printer);
|
| - if (!field->is_repeated()) {
|
| + if (check_has_bits) {
|
| printer->Outdent();
|
| printer->Print("}\n");
|
| }
|
| }
|
| - if (HasDescriptorMethods(descriptor_)) {
|
| - if (descriptor_->extension_range_count() > 0) {
|
| - printer->Print(
|
| - "hash = hashFields(hash, getExtensionFields());\n");
|
| - }
|
| + if (descriptor_->extension_range_count() > 0) {
|
| + printer->Print(
|
| + "hash = hashFields(hash, getExtensionFields());\n");
|
| }
|
| +
|
| + printer->Print(
|
| + "hash = (29 * hash) + unknownFields.hashCode();\n");
|
| printer->Print(
|
| - "hash = (29 * hash) + getUnknownFields().hashCode();\n"
|
| "memoizedHashCode = hash;\n"
|
| "return hash;\n");
|
| printer->Outdent();
|
| @@ -1228,39 +1033,41 @@ void MessageGenerator::GenerateEqualsAndHashCode(io::Printer* printer) {
|
|
|
| // ===================================================================
|
|
|
| -void MessageGenerator::GenerateExtensionRegistrationCode(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::
|
| +GenerateExtensionRegistrationCode(io::Printer* printer) {
|
| for (int i = 0; i < descriptor_->extension_count(); i++) {
|
| - ExtensionGenerator(descriptor_->extension(i))
|
| + ImmutableExtensionGenerator(descriptor_->extension(i), context_)
|
| .GenerateRegistrationCode(printer);
|
| }
|
|
|
| for (int i = 0; i < descriptor_->nested_type_count(); i++) {
|
| - MessageGenerator(descriptor_->nested_type(i))
|
| + ImmutableMessageGenerator(descriptor_->nested_type(i), context_)
|
| .GenerateExtensionRegistrationCode(printer);
|
| }
|
| }
|
|
|
| // ===================================================================
|
| -void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| - scoped_array<const FieldDescriptor*> sorted_fields(
|
| +void ImmutableMessageGenerator::
|
| +GenerateParsingConstructor(io::Printer* printer) {
|
| + google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields(
|
| SortFieldsByNumber(descriptor_));
|
|
|
| printer->Print(
|
| "private $classname$(\n"
|
| " com.google.protobuf.CodedInputStream input,\n"
|
| - " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
|
| - " throws com.google.protobuf.InvalidProtocolBufferException {\n",
|
| + " com.google.protobuf.ExtensionRegistryLite extensionRegistry) {\n",
|
| "classname", descriptor_->name());
|
| printer->Indent();
|
|
|
| // Initialize all fields to default.
|
| printer->Print(
|
| - "initFields();\n");
|
| + "this();\n");
|
|
|
| // Use builder bits to track mutable repeated fields.
|
| int totalBuilderBits = 0;
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| - const FieldGenerator& field = field_generators_.get(descriptor_->field(i));
|
| + const ImmutableFieldGenerator& field =
|
| + field_generators_.get(descriptor_->field(i));
|
| totalBuilderBits += field.GetNumBitsForBuilder();
|
| }
|
| int totalBuilderInts = (totalBuilderBits + 31) / 32;
|
| @@ -1269,7 +1076,7 @@ void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| "bit_field_name", GetBitFieldName(i));
|
| }
|
|
|
| - if (HasUnknownFields(descriptor_)) {
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| printer->Print(
|
| "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n"
|
| " com.google.protobuf.UnknownFieldSet.newBuilder();\n");
|
| @@ -1292,16 +1099,26 @@ void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| printer->Print(
|
| "case 0:\n" // zero signals EOF / limit reached
|
| " done = true;\n"
|
| - " break;\n"
|
| - "default: {\n"
|
| - " if (!parseUnknownField(input,$unknown_fields$\n"
|
| - " extensionRegistry, tag)) {\n"
|
| - " done = true;\n" // it's an endgroup tag
|
| - " }\n"
|
| - " break;\n"
|
| - "}\n",
|
| - "unknown_fields", HasUnknownFields(descriptor_)
|
| - ? " unknownFields," : "");
|
| + " break;\n");
|
| +
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| + printer->Print(
|
| + "default: {\n"
|
| + " if (!parseUnknownField(input, unknownFields,\n"
|
| + " extensionRegistry, tag)) {\n"
|
| + " done = true;\n" // it's an endgroup tag
|
| + " }\n"
|
| + " break;\n"
|
| + "}\n");
|
| + } else {
|
| + printer->Print(
|
| + "default: {\n"
|
| + " if (!input.skipField(tag)) {\n"
|
| + " done = true;\n" // it's an endgroup tag
|
| + " }\n"
|
| + " break;\n"
|
| + "}\n");
|
| + }
|
|
|
| for (int i = 0; i < descriptor_->field_count(); i++) {
|
| const FieldDescriptor* field = sorted_fields[i];
|
| @@ -1348,10 +1165,11 @@ void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| printer->Outdent();
|
| printer->Print(
|
| "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
|
| - " throw e.setUnfinishedMessage(this);\n"
|
| + " throw new RuntimeException(e.setUnfinishedMessage(this));\n"
|
| "} catch (java.io.IOException e) {\n"
|
| - " throw new com.google.protobuf.InvalidProtocolBufferException(\n"
|
| - " e.getMessage()).setUnfinishedMessage(this);\n"
|
| + " throw new RuntimeException(\n"
|
| + " new com.google.protobuf.InvalidProtocolBufferException(\n"
|
| + " e.getMessage()).setUnfinishedMessage(this));\n"
|
| "} finally {\n");
|
| printer->Indent();
|
|
|
| @@ -1361,10 +1179,9 @@ void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| field_generators_.get(field).GenerateParsingDoneCode(printer);
|
| }
|
|
|
| - // Make unknown fields immutable.
|
| - if (HasUnknownFields(descriptor_)) {
|
| - printer->Print(
|
| - "this.unknownFields = unknownFields.build();\n");
|
| + if (PreserveUnknownFields(descriptor_)) {
|
| + // Make unknown fields immutable.
|
| + printer->Print("this.unknownFields = unknownFields.build();\n");
|
| }
|
|
|
| // Make extensions immutable.
|
| @@ -1379,9 +1196,9 @@ void MessageGenerator::GenerateParsingConstructor(io::Printer* printer) {
|
| }
|
|
|
| // ===================================================================
|
| -void MessageGenerator::GenerateParser(io::Printer* printer) {
|
| +void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) {
|
| printer->Print(
|
| - "public static com.google.protobuf.Parser<$classname$> PARSER =\n"
|
| + "public static final com.google.protobuf.Parser<$classname$> PARSER =\n"
|
| " new com.google.protobuf.AbstractParser<$classname$>() {\n",
|
| "classname", descriptor_->name());
|
| printer->Indent();
|
| @@ -1392,13 +1209,24 @@ void MessageGenerator::GenerateParser(io::Printer* printer) {
|
| " throws com.google.protobuf.InvalidProtocolBufferException {\n",
|
| "classname", descriptor_->name());
|
| if (HasGeneratedMethods(descriptor_)) {
|
| + // The parsing constructor throws an InvalidProtocolBufferException via a
|
| + // RuntimeException to aid in method pruning. We unwrap it here.
|
| printer->Print(
|
| - " return new $classname$(input, extensionRegistry);\n",
|
| + " try {\n"
|
| + " return new $classname$(input, extensionRegistry);\n"
|
| + " } catch (RuntimeException e) {\n"
|
| + " if (e.getCause() instanceof\n"
|
| + " com.google.protobuf.InvalidProtocolBufferException) {\n"
|
| + " throw (com.google.protobuf.InvalidProtocolBufferException)\n"
|
| + " e.getCause();\n"
|
| + " }\n"
|
| + " throw e;\n"
|
| + " }\n",
|
| "classname", descriptor_->name());
|
| } else {
|
| - // When parsing constructor isn't generated, use builder to parse messages.
|
| - // Note, will fallback to use reflection based mergeFieldFrom() in
|
| - // AbstractMessage.Builder.
|
| + // When parsing constructor isn't generated, use builder to parse
|
| + // messages. Note, will fallback to use reflection based mergeFieldFrom()
|
| + // in AbstractMessage.Builder.
|
| printer->Indent();
|
| printer->Print(
|
| "Builder builder = newBuilder();\n"
|
| @@ -1408,7 +1236,8 @@ void MessageGenerator::GenerateParser(io::Printer* printer) {
|
| " throw e.setUnfinishedMessage(builder.buildPartial());\n"
|
| "} catch (java.io.IOException e) {\n"
|
| " throw new com.google.protobuf.InvalidProtocolBufferException(\n"
|
| - " e.getMessage()).setUnfinishedMessage(builder.buildPartial());\n"
|
| + " e.getMessage()).setUnfinishedMessage(\n"
|
| + " builder.buildPartial());\n"
|
| "}\n"
|
| "return builder.buildPartial();\n");
|
| printer->Outdent();
|
| @@ -1429,6 +1258,17 @@ void MessageGenerator::GenerateParser(io::Printer* printer) {
|
| "classname", descriptor_->name());
|
| }
|
|
|
| +// ===================================================================
|
| +void ImmutableMessageGenerator::GenerateInitializers(io::Printer* printer) {
|
| + for (int i = 0; i < descriptor_->field_count(); i++) {
|
| + if (!descriptor_->field(i)->containing_oneof()) {
|
| + field_generators_.get(descriptor_->field(i))
|
| + .GenerateInitializationCode(printer);
|
| + }
|
| + }
|
| +}
|
| +
|
| +
|
| } // namespace java
|
| } // namespace compiler
|
| } // namespace protobuf
|
|
|