Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/java/java_map_field.cc

Issue 1291903002: Pull new version of protobuf sources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <google/protobuf/compiler/java/java_map_field.h>
32
33 #include <google/protobuf/compiler/java/java_context.h>
34 #include <google/protobuf/compiler/java/java_doc_comment.h>
35 #include <google/protobuf/compiler/java/java_helpers.h>
36 #include <google/protobuf/compiler/java/java_name_resolver.h>
37 #include <google/protobuf/io/printer.h>
38
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace java {
43
44 namespace {
45
46 const FieldDescriptor* KeyField(const FieldDescriptor* descriptor) {
47 GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
48 const Descriptor* message = descriptor->message_type();
49 GOOGLE_CHECK(message->options().map_entry());
50 return message->FindFieldByName("key");
51 }
52
53 const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) {
54 GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
55 const Descriptor* message = descriptor->message_type();
56 GOOGLE_CHECK(message->options().map_entry());
57 return message->FindFieldByName("value");
58 }
59
60 string TypeName(const FieldDescriptor* field,
61 ClassNameResolver* name_resolver,
62 bool boxed) {
63 if (GetJavaType(field) == JAVATYPE_MESSAGE) {
64 return name_resolver->GetImmutableClassName(field->message_type());
65 } else if (GetJavaType(field) == JAVATYPE_ENUM) {
66 return name_resolver->GetImmutableClassName(field->enum_type());
67 } else {
68 return boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
69 : PrimitiveTypeName(GetJavaType(field));
70 }
71 }
72
73 string WireType(const FieldDescriptor* field) {
74 return "com.google.protobuf.WireFormat.FieldType." +
75 string(FieldTypeName(field->type()));
76 }
77
78 void SetMessageVariables(const FieldDescriptor* descriptor,
79 int messageBitIndex,
80 int builderBitIndex,
81 const FieldGeneratorInfo* info,
82 ClassNameResolver* name_resolver,
83 map<string, string>* variables) {
84 SetCommonFieldVariables(descriptor, info, variables);
85
86 (*variables)["type"] =
87 name_resolver->GetImmutableClassName(descriptor->message_type());
88 const FieldDescriptor* key = KeyField(descriptor);
89 const FieldDescriptor* value = ValueField(descriptor);
90 (*variables)["key_type"] = TypeName(key, name_resolver, false);
91 (*variables)["boxed_key_type"] = TypeName(key, name_resolver, true);
92 (*variables)["key_wire_type"] = WireType(key);
93 (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver);
94 if (GetJavaType(value) == JAVATYPE_ENUM) {
95 // We store enums as Integers internally.
96 (*variables)["value_type"] = "int";
97 (*variables)["boxed_value_type"] = "java.lang.Integer";
98 (*variables)["value_wire_type"] = WireType(value);
99 (*variables)["value_default_value"] =
100 DefaultValue(value, true, name_resolver) + ".getNumber()";
101
102 (*variables)["value_enum_type"] = TypeName(value, name_resolver, false);
103
104 if (SupportUnknownEnumValue(descriptor->file())) {
105 // Map unknown values to a special UNRECOGNIZED value if supported.
106 (*variables)["unrecognized_value"] =
107 (*variables)["value_enum_type"] + ".UNRECOGNIZED";
108 } else {
109 // Map unknown values to the default value if we don't have UNRECOGNIZED.
110 (*variables)["unrecognized_value"] =
111 DefaultValue(value, true, name_resolver);
112 }
113 } else {
114 (*variables)["value_type"] = TypeName(value, name_resolver, false);
115 (*variables)["boxed_value_type"] = TypeName(value, name_resolver, true);
116 (*variables)["value_wire_type"] = WireType(value);
117 (*variables)["value_default_value"] =
118 DefaultValue(value, true, name_resolver);
119 }
120 (*variables)["type_parameters"] =
121 (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
122 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
123 // by the proto compiler
124 (*variables)["deprecation"] = descriptor->options().deprecated()
125 ? "@java.lang.Deprecated " : "";
126 (*variables)["on_changed"] =
127 HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
128
129 // For repeated fields, one bit is used for whether the array is immutable
130 // in the parsing constructor.
131 (*variables)["get_mutable_bit_parser"] =
132 GenerateGetBitMutableLocal(builderBitIndex);
133 (*variables)["set_mutable_bit_parser"] =
134 GenerateSetBitMutableLocal(builderBitIndex);
135
136 (*variables)["default_entry"] = (*variables)["capitalized_name"] +
137 "DefaultEntryHolder.defaultEntry";
138 if (HasDescriptorMethods(descriptor->file())) {
139 (*variables)["lite"] = "";
140 (*variables)["map_field_parameter"] = (*variables)["default_entry"];
141 (*variables)["descriptor"] =
142 name_resolver->GetImmutableClassName(descriptor->file()) +
143 ".internal_" + UniqueFileScopeIdentifier(descriptor->message_type()) +
144 "_descriptor, ";
145 } else {
146 (*variables)["lite"] = "Lite";
147 (*variables)["map_field_parameter"] = "";
148 (*variables)["descriptor"] = "";
149 }
150 }
151
152 } // namespace
153
154 ImmutableMapFieldGenerator::
155 ImmutableMapFieldGenerator(const FieldDescriptor* descriptor,
156 int messageBitIndex,
157 int builderBitIndex,
158 Context* context)
159 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
160 SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
161 context->GetFieldGeneratorInfo(descriptor),
162 name_resolver_, &variables_);
163 }
164
165 ImmutableMapFieldGenerator::
166 ~ImmutableMapFieldGenerator() {}
167
168 int ImmutableMapFieldGenerator::GetNumBitsForMessage() const {
169 return 0;
170 }
171
172 int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const {
173 return 1;
174 }
175
176 void ImmutableMapFieldGenerator::
177 GenerateInterfaceMembers(io::Printer* printer) const {
178 if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
179 WriteFieldDocComment(printer, descriptor_);
180 printer->Print(
181 variables_,
182 "$deprecation$java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
183 "get$capitalized_name$();\n");
184 if (SupportUnknownEnumValue(descriptor_->file())) {
185 WriteFieldDocComment(printer, descriptor_);
186 printer->Print(
187 variables_,
188 "$deprecation$java.util.Map<$type_parameters$>\n"
189 "get$capitalized_name$Value();\n");
190 }
191 } else {
192 WriteFieldDocComment(printer, descriptor_);
193 printer->Print(
194 variables_,
195 "$deprecation$java.util.Map<$type_parameters$>\n"
196 "get$capitalized_name$();\n");
197 }
198 }
199
200 void ImmutableMapFieldGenerator::
201 GenerateMembers(io::Printer* printer) const {
202 printer->Print(
203 variables_,
204 "private static final class $capitalized_name$DefaultEntryHolder {\n"
205 " static final com.google.protobuf.MapEntry$lite$<\n"
206 " $type_parameters$> defaultEntry =\n"
207 " com.google.protobuf.MapEntry$lite$\n"
208 " .<$type_parameters$>newDefaultInstance(\n"
209 " $descriptor$\n"
210 " $key_wire_type$,\n"
211 " $key_default_value$,\n"
212 " $value_wire_type$,\n"
213 " $value_default_value$);\n"
214 "}\n");
215 printer->Print(
216 variables_,
217 "private com.google.protobuf.MapField$lite$<\n"
218 " $type_parameters$> $name$_;\n"
219 "private com.google.protobuf.MapField$lite$<$type_parameters$>\n"
220 "internalGet$capitalized_name$() {\n"
221 " if ($name$_ == null) {\n"
222 " return com.google.protobuf.MapField$lite$.emptyMapField(\n"
223 " $map_field_parameter$);\n"
224 " }\n"
225 " return $name$_;\n"
226 "}\n");
227 if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
228 printer->Print(
229 variables_,
230 "private static final\n"
231 "com.google.protobuf.Internal.MapAdapter.Converter<\n"
232 " java.lang.Integer, $value_enum_type$> $name$ValueConverter =\n"
233 " com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n"
234 " $value_enum_type$.internalGetValueMap(),\n"
235 " $unrecognized_value$);\n");
236 if (SupportUnknownEnumValue(descriptor_->file())) {
237 WriteFieldDocComment(printer, descriptor_);
238 printer->Print(
239 variables_,
240 "$deprecation$\n"
241 "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
242 "get$capitalized_name$Value() {\n"
243 " return internalGet$capitalized_name$().getMap();\n"
244 "}\n");
245 }
246 WriteFieldDocComment(printer, descriptor_);
247 printer->Print(
248 variables_,
249 "$deprecation$\n"
250 "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
251 "get$capitalized_name$() {\n"
252 " return new com.google.protobuf.Internal.MapAdapter<\n"
253 " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
254 " internalGet$capitalized_name$().getMap(),\n"
255 " $name$ValueConverter);\n"
256 "}\n");
257 } else {
258 WriteFieldDocComment(printer, descriptor_);
259 printer->Print(
260 variables_,
261 "$deprecation$\n"
262 "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n"
263 " return internalGet$capitalized_name$().getMap();\n"
264 "}\n");
265 }
266 }
267
268 void ImmutableMapFieldGenerator::
269 GenerateBuilderMembers(io::Printer* printer) const {
270 printer->Print(
271 variables_,
272 "private com.google.protobuf.MapField$lite$<\n"
273 " $type_parameters$> $name$_;\n"
274 "private com.google.protobuf.MapField$lite$<$type_parameters$>\n"
275 "internalGet$capitalized_name$() {\n"
276 " if ($name$_ == null) {\n"
277 " return com.google.protobuf.MapField$lite$.emptyMapField(\n"
278 " $map_field_parameter$);\n"
279 " }\n"
280 " return $name$_;\n"
281 "}\n"
282 "private com.google.protobuf.MapField$lite$<$type_parameters$>\n"
283 "internalGetMutable$capitalized_name$() {\n"
284 " $on_changed$;\n"
285 " if ($name$_ == null) {\n"
286 " $name$_ = com.google.protobuf.MapField$lite$.newMapField(\n"
287 " $map_field_parameter$);\n"
288 " }\n"
289 " if (!$name$_.isMutable()) {\n"
290 " $name$_ = $name$_.copy();\n"
291 " }\n"
292 " return $name$_;\n"
293 "}\n");
294 if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
295 WriteFieldDocComment(printer, descriptor_);
296 printer->Print(
297 variables_,
298 "$deprecation$\n"
299 "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
300 "get$capitalized_name$() {\n"
301 " return new com.google.protobuf.Internal.MapAdapter<\n"
302 " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
303 " internalGet$capitalized_name$().getMap(),\n"
304 " $name$ValueConverter);\n"
305 "}\n");
306 WriteFieldDocComment(printer, descriptor_);
307 printer->Print(
308 variables_,
309 "$deprecation$\n"
310 "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
311 "getMutable$capitalized_name$() {\n"
312 " return new com.google.protobuf.Internal.MapAdapter<\n"
313 " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
314 " internalGetMutable$capitalized_name$().getMutableMap(),\n"
315 " $name$ValueConverter);\n"
316 "}\n");
317 if (SupportUnknownEnumValue(descriptor_->file())) {
318 WriteFieldDocComment(printer, descriptor_);
319 printer->Print(
320 variables_,
321 "$deprecation$\n"
322 "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
323 "get$capitalized_name$Value() {\n"
324 " return internalGet$capitalized_name$().getMap();\n"
325 "}\n");
326 WriteFieldDocComment(printer, descriptor_);
327 printer->Print(
328 variables_,
329 "$deprecation$\n"
330 "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
331 "getMutable$capitalized_name$Value() {\n"
332 " return internalGetMutable$capitalized_name$().getMutableMap();\n"
333 "}\n");
334 }
335 } else {
336 WriteFieldDocComment(printer, descriptor_);
337 printer->Print(
338 variables_,
339 "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n"
340 " return internalGet$capitalized_name$().getMap();\n"
341 "}\n");
342 WriteFieldDocComment(printer, descriptor_);
343 printer->Print(
344 variables_,
345 "public java.util.Map<$type_parameters$>\n"
346 "getMutable$capitalized_name$() {\n"
347 " return internalGetMutable$capitalized_name$().getMutableMap();\n"
348 "}\n");
349 }
350 }
351
352 void ImmutableMapFieldGenerator::
353 GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
354 // Nothing to initialize.
355 }
356
357 void ImmutableMapFieldGenerator::
358 GenerateInitializationCode(io::Printer* printer) const {
359 // Nothing to initialize.
360 }
361
362 void ImmutableMapFieldGenerator::
363 GenerateBuilderClearCode(io::Printer* printer) const {
364 printer->Print(
365 variables_,
366 "internalGetMutable$capitalized_name$().clear();\n");
367 }
368
369 void ImmutableMapFieldGenerator::
370 GenerateMergingCode(io::Printer* printer) const {
371 printer->Print(
372 variables_,
373 "internalGetMutable$capitalized_name$().mergeFrom(\n"
374 " other.internalGet$capitalized_name$());\n");
375 }
376
377 void ImmutableMapFieldGenerator::
378 GenerateBuildingCode(io::Printer* printer) const {
379 printer->Print(
380 variables_,
381 "result.$name$_ = internalGet$capitalized_name$();\n"
382 "result.$name$_.makeImmutable();\n");
383 }
384
385 void ImmutableMapFieldGenerator::
386 GenerateParsingCode(io::Printer* printer) const {
387 printer->Print(
388 variables_,
389 "if (!$get_mutable_bit_parser$) {\n"
390 " $name$_ = com.google.protobuf.MapField$lite$.newMapField(\n"
391 " $map_field_parameter$);\n"
392 " $set_mutable_bit_parser$;\n"
393 "}\n");
394 if (!SupportUnknownEnumValue(descriptor_->file()) &&
395 GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
396 printer->Print(
397 variables_,
398 "com.google.protobuf.ByteString bytes = input.readBytes();\n"
399 "com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
400 "$name$ = $default_entry$.getParserForType().parseFrom(bytes);\n");
401 printer->Print(
402 variables_,
403 "if ($value_enum_type$.valueOf($name$.getValue()) == null) {\n"
404 " unknownFields.mergeLengthDelimitedField($number$, bytes);\n"
405 "} else {\n"
406 " $name$_.getMutableMap().put($name$.getKey(), $name$.getValue());\n"
407 "}\n");
408 } else {
409 printer->Print(
410 variables_,
411 "com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
412 "$name$ = input.readMessage(\n"
413 " $default_entry$.getParserForType(), extensionRegistry);\n"
414 "$name$_.getMutableMap().put($name$.getKey(), $name$.getValue());\n");
415 }
416 }
417
418 void ImmutableMapFieldGenerator::
419 GenerateParsingDoneCode(io::Printer* printer) const {
420 // Nothing to do here.
421 }
422
423 void ImmutableMapFieldGenerator::
424 GenerateSerializationCode(io::Printer* printer) const {
425 printer->Print(
426 variables_,
427 "for (java.util.Map.Entry<$type_parameters$> entry\n"
428 " : internalGet$capitalized_name$().getMap().entrySet()) {\n"
429 " com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
430 " $name$ = $default_entry$.newBuilderForType()\n"
431 " .setKey(entry.getKey())\n"
432 " .setValue(entry.getValue())\n"
433 " .build();\n"
434 " output.writeMessage($number$, $name$);\n"
435 "}\n");
436 }
437
438 void ImmutableMapFieldGenerator::
439 GenerateSerializedSizeCode(io::Printer* printer) const {
440 printer->Print(
441 variables_,
442 "for (java.util.Map.Entry<$type_parameters$> entry\n"
443 " : internalGet$capitalized_name$().getMap().entrySet()) {\n"
444 " com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
445 " $name$ = $default_entry$.newBuilderForType()\n"
446 " .setKey(entry.getKey())\n"
447 " .setValue(entry.getValue())\n"
448 " .build();\n"
449 " size += com.google.protobuf.CodedOutputStream\n"
450 " .computeMessageSize($number$, $name$);\n"
451 "}\n");
452 }
453
454 void ImmutableMapFieldGenerator::
455 GenerateEqualsCode(io::Printer* printer) const {
456 printer->Print(
457 variables_,
458 "result = result && internalGet$capitalized_name$().equals(\n"
459 " other.internalGet$capitalized_name$());\n");
460 }
461
462 void ImmutableMapFieldGenerator::
463 GenerateHashCode(io::Printer* printer) const {
464 printer->Print(
465 variables_,
466 "if (!internalGet$capitalized_name$().getMap().isEmpty()) {\n"
467 " hash = (37 * hash) + $constant_name$;\n"
468 " hash = (53 * hash) + internalGet$capitalized_name$().hashCode();\n"
469 "}\n");
470 }
471
472 string ImmutableMapFieldGenerator::GetBoxedType() const {
473 return name_resolver_->GetImmutableClassName(descriptor_->message_type());
474 }
475
476 } // namespace java
477 } // namespace compiler
478 } // namespace protobuf
479 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698