From 12d8a514c84ee8d60e5d8b8b84aa09dad15197f2 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Sat, 7 Feb 2015 12:51:36 -0800 Subject: [PATCH] Get the map factory per mergeFrom() call for JavaNano. --- .../java/com/google/protobuf/nano/InternalNano.java | 5 ++++- .../protobuf/compiler/javanano/javanano_helpers.cc | 11 +++++++++++ .../protobuf/compiler/javanano/javanano_helpers.h | 2 ++ .../protobuf/compiler/javanano/javanano_map_field.cc | 2 +- .../protobuf/compiler/javanano/javanano_message.cc | 5 +++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java index f3144652b0..c92ec41761 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -30,6 +30,8 @@ package com.google.protobuf.nano; +import com.google.protobuf.nano.MapFactories.MapFactory; + import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Arrays; @@ -406,12 +408,13 @@ public final class InternalNano { public static final Map mergeMapEntry( CodedInputByteBufferNano input, Map map, + MapFactory mapFactory, int keyType, int valueType, V value, int keyTag, int valueTag) throws IOException { - map = MapFactories.getMapFactory().forMap(map); + map = mapFactory.forMap(map); final int length = input.readRawVarint32(); final int oldLimit = input.pushLimit(length); K key = null; diff --git a/src/google/protobuf/compiler/javanano/javanano_helpers.cc b/src/google/protobuf/compiler/javanano/javanano_helpers.cc index 2149418af6..7b23d9f43d 100644 --- a/src/google/protobuf/compiler/javanano/javanano_helpers.cc +++ b/src/google/protobuf/compiler/javanano/javanano_helpers.cc @@ -560,6 +560,17 @@ void SetBitOperationVariables(const string name, (*variables)["different_" + name] = GenerateDifferentBit(bitIndex); } +bool HasMapField(const Descriptor* descriptor) { + for (int i = 0; i < descriptor->field_count(); ++i) { + const FieldDescriptor* field = descriptor->field(i); + if (field->type() == FieldDescriptor::TYPE_MESSAGE && + IsMapEntry(field->message_type())) { + return true; + } + } + return false; +} + } // namespace javanano } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/javanano/javanano_helpers.h b/src/google/protobuf/compiler/javanano/javanano_helpers.h index 5d3532b4ac..b4fee649a3 100644 --- a/src/google/protobuf/compiler/javanano/javanano_helpers.h +++ b/src/google/protobuf/compiler/javanano/javanano_helpers.h @@ -187,6 +187,8 @@ inline bool IsMapEntry(const Descriptor* descriptor) { descriptor->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; } +bool HasMapField(const Descriptor* descriptor); + } // namespace javanano } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/javanano/javanano_map_field.cc b/src/google/protobuf/compiler/javanano/javanano_map_field.cc index 117c4f5c04..83b2b0ce8f 100644 --- a/src/google/protobuf/compiler/javanano/javanano_map_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_map_field.cc @@ -134,7 +134,7 @@ void MapFieldGenerator:: GenerateMergingCode(io::Printer* printer) const { printer->Print(variables_, "this.$name$ = com.google.protobuf.nano.InternalNano.mergeMapEntry(\n" - " input, this.$name$,\n" + " input, this.$name$, mapFactory,\n" " com.google.protobuf.nano.InternalNano.$key_desc_type$,\n" " com.google.protobuf.nano.InternalNano.$value_desc_type$,\n" " $value_default$,\n" diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc index 5454d9be0e..da7c656f38 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message.cc @@ -345,6 +345,11 @@ void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) { "classname", descriptor_->name()); printer->Indent(); + if (HasMapField(descriptor_)) { + printer->Print( + "com.google.protobuf.nano.MapFactories.MapFactory mapFactory =\n" + " com.google.protobuf.nano.MapFactories.getMapFactory();\n"); + } printer->Print( "while (true) {\n");