Get the map factory per mergeFrom() call for JavaNano.

This commit is contained in:
Jisi Liu 2015-02-07 12:51:36 -08:00
parent d9a6f27bc3
commit 12d8a514c8
5 changed files with 23 additions and 2 deletions

View File

@ -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 <K, V> Map<K, V> mergeMapEntry(
CodedInputByteBufferNano input,
Map<K, V> 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;

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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");