java_map_field.cc
Go to the documentation of this file.
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 
32 
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace java {
43 
44 namespace {
45 
46 const FieldDescriptor* KeyField(const FieldDescriptor* descriptor) {
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) {
55  const Descriptor* message = descriptor->message_type();
56  GOOGLE_CHECK(message->options().map_entry());
57  return message->FindFieldByName("value");
58 }
59 
61  ClassNameResolver* name_resolver, bool boxed) {
63  return name_resolver->GetImmutableClassName(field->message_type());
64  } else if (GetJavaType(field) == JAVATYPE_ENUM) {
65  return name_resolver->GetImmutableClassName(field->enum_type());
66  } else {
69  }
70 }
71 
72 std::string WireType(const FieldDescriptor* field) {
73  return "com.google.protobuf.WireFormat.FieldType." +
75 }
76 
77 void SetMessageVariables(const FieldDescriptor* descriptor, int messageBitIndex,
78  int builderBitIndex, const FieldGeneratorInfo* info,
79  Context* context,
80  std::map<std::string, std::string>* variables) {
81  SetCommonFieldVariables(descriptor, info, variables);
82  ClassNameResolver* name_resolver = context->GetNameResolver();
83 
84  (*variables)["type"] =
85  name_resolver->GetImmutableClassName(descriptor->message_type());
86  const FieldDescriptor* key = KeyField(descriptor);
87  const FieldDescriptor* value = ValueField(descriptor);
88  const JavaType keyJavaType = GetJavaType(key);
89  const JavaType valueJavaType = GetJavaType(value);
90 
91  (*variables)["key_type"] = TypeName(key, name_resolver, false);
92  std::string boxed_key_type = TypeName(key, name_resolver, true);
93  (*variables)["boxed_key_type"] = boxed_key_type;
94  // Used for calling the serialization function.
95  (*variables)["short_key_type"] =
96  boxed_key_type.substr(boxed_key_type.rfind('.') + 1);
97  (*variables)["key_wire_type"] = WireType(key);
98  (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver);
99  (*variables)["key_null_check"] =
100  IsReferenceType(keyJavaType)
101  ? "if (key == null) { throw new java.lang.NullPointerException(); }"
102  : "";
103  (*variables)["value_null_check"] =
104  IsReferenceType(valueJavaType)
105  ? "if (value == null) { throw new java.lang.NullPointerException(); }"
106  : "";
107  if (valueJavaType == JAVATYPE_ENUM) {
108  // We store enums as Integers internally.
109  (*variables)["value_type"] = "int";
110  (*variables)["boxed_value_type"] = "java.lang.Integer";
111  (*variables)["value_wire_type"] = WireType(value);
112  (*variables)["value_default_value"] =
113  DefaultValue(value, true, name_resolver) + ".getNumber()";
114 
115  (*variables)["value_enum_type"] = TypeName(value, name_resolver, false);
116 
117  if (SupportUnknownEnumValue(descriptor->file())) {
118  // Map unknown values to a special UNRECOGNIZED value if supported.
119  (*variables)["unrecognized_value"] =
120  (*variables)["value_enum_type"] + ".UNRECOGNIZED";
121  } else {
122  // Map unknown values to the default value if we don't have UNRECOGNIZED.
123  (*variables)["unrecognized_value"] =
124  DefaultValue(value, true, name_resolver);
125  }
126  } else {
127  (*variables)["value_type"] = TypeName(value, name_resolver, false);
128  (*variables)["boxed_value_type"] = TypeName(value, name_resolver, true);
129  (*variables)["value_wire_type"] = WireType(value);
130  (*variables)["value_default_value"] =
131  DefaultValue(value, true, name_resolver);
132  }
133  (*variables)["type_parameters"] =
134  (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
135  // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
136  // by the proto compiler
137  (*variables)["deprecation"] =
138  descriptor->options().deprecated() ? "@java.lang.Deprecated " : "";
139  (*variables)["on_changed"] = "onChanged();";
140 
141  // For repeated fields, one bit is used for whether the array is immutable
142  // in the parsing constructor.
143  (*variables)["get_mutable_bit_parser"] =
144  GenerateGetBitMutableLocal(builderBitIndex);
145  (*variables)["set_mutable_bit_parser"] =
146  GenerateSetBitMutableLocal(builderBitIndex);
147 
148  (*variables)["default_entry"] =
149  (*variables)["capitalized_name"] + "DefaultEntryHolder.defaultEntry";
150  (*variables)["map_field_parameter"] = (*variables)["default_entry"];
151  (*variables)["descriptor"] =
152  name_resolver->GetImmutableClassName(descriptor->file()) + ".internal_" +
153  UniqueFileScopeIdentifier(descriptor->message_type()) + "_descriptor, ";
154  (*variables)["ver"] = GeneratedCodeVersionSuffix();
155 }
156 
157 } // namespace
158 
160  const FieldDescriptor* descriptor, int messageBitIndex, int builderBitIndex,
161  Context* context)
162  : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
163  SetMessageVariables(descriptor, messageBitIndex, builderBitIndex,
164  context->GetFieldGeneratorInfo(descriptor), context,
165  &variables_);
166 }
167 
169 
171 
173 
175  io::Printer* printer) const {
177  printer->Print(variables_,
178  "$deprecation$int ${$get$capitalized_name$Count$}$();\n");
179  printer->Annotate("{", "}", descriptor_);
181  printer->Print(variables_,
182  "$deprecation$boolean ${$contains$capitalized_name$$}$(\n"
183  " $key_type$ key);\n");
184  printer->Annotate("{", "}", descriptor_);
185  if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
186  printer->Print(variables_,
187  "/**\n"
188  " * Use {@link #get$capitalized_name$Map()} instead.\n"
189  " */\n"
190  "@java.lang.Deprecated\n"
191  "java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
192  "${$get$capitalized_name$$}$();\n");
193  printer->Annotate("{", "}", descriptor_);
195  printer->Print(
196  variables_,
197  "$deprecation$java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
198  "${$get$capitalized_name$Map$}$();\n");
199  printer->Annotate("{", "}", descriptor_);
201  printer->Print(
202  variables_,
203  "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n"
204  " $key_type$ key,\n"
205  " $value_enum_type$ defaultValue);\n");
206  printer->Annotate("{", "}", descriptor_);
208  printer->Print(
209  variables_,
210  "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n"
211  " $key_type$ key);\n");
212  printer->Annotate("{", "}", descriptor_);
214  printer->Print(
215  variables_,
216  "/**\n"
217  " * Use {@link #get$capitalized_name$ValueMap()} instead.\n"
218  " */\n"
219  "@java.lang.Deprecated\n"
220  "java.util.Map<$type_parameters$>\n"
221  "${$get$capitalized_name$Value$}$();\n");
222  printer->Annotate("{", "}", descriptor_);
224  printer->Print(variables_,
225  "$deprecation$java.util.Map<$type_parameters$>\n"
226  "${$get$capitalized_name$ValueMap$}$();\n");
227  printer->Annotate("{", "}", descriptor_);
229  printer->Print(variables_,
230  "$deprecation$\n"
231  "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n"
232  " $key_type$ key,\n"
233  " $value_type$ defaultValue);\n");
234  printer->Annotate("{", "}", descriptor_);
236  printer->Print(variables_,
237  "$deprecation$\n"
238  "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n"
239  " $key_type$ key);\n");
240  printer->Annotate("{", "}", descriptor_);
241  }
242  } else {
243  printer->Print(variables_,
244  "/**\n"
245  " * Use {@link #get$capitalized_name$Map()} instead.\n"
246  " */\n"
247  "@java.lang.Deprecated\n"
248  "java.util.Map<$type_parameters$>\n"
249  "${$get$capitalized_name$$}$();\n");
250  printer->Annotate("{", "}", descriptor_);
252  printer->Print(variables_,
253  "$deprecation$java.util.Map<$type_parameters$>\n"
254  "${$get$capitalized_name$Map$}$();\n");
255  printer->Annotate("{", "}", descriptor_);
257  printer->Print(variables_,
258  "$deprecation$\n"
259  "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n"
260  " $key_type$ key,\n"
261  " $value_type$ defaultValue);\n");
262  printer->Annotate("{", "}", descriptor_);
264  printer->Print(variables_,
265  "$deprecation$\n"
266  "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n"
267  " $key_type$ key);\n");
268  printer->Annotate("{", "}", descriptor_);
269  }
270 }
271 
273  printer->Print(
274  variables_,
275  "private static final class $capitalized_name$DefaultEntryHolder {\n"
276  " static final com.google.protobuf.MapEntry<\n"
277  " $type_parameters$> defaultEntry =\n"
278  " com.google.protobuf.MapEntry\n"
279  " .<$type_parameters$>newDefaultInstance(\n"
280  " $descriptor$\n"
281  " $key_wire_type$,\n"
282  " $key_default_value$,\n"
283  " $value_wire_type$,\n"
284  " $value_default_value$);\n"
285  "}\n");
286  printer->Print(variables_,
287  "private com.google.protobuf.MapField<\n"
288  " $type_parameters$> $name$_;\n"
289  "private com.google.protobuf.MapField<$type_parameters$>\n"
290  "internalGet$capitalized_name$() {\n"
291  " if ($name$_ == null) {\n"
292  " return com.google.protobuf.MapField.emptyMapField(\n"
293  " $map_field_parameter$);\n"
294  " }\n"
295  " return $name$_;\n"
296  "}\n");
297  if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
298  printer->Print(
299  variables_,
300  "private static final\n"
301  "com.google.protobuf.Internal.MapAdapter.Converter<\n"
302  " java.lang.Integer, $value_enum_type$> $name$ValueConverter =\n"
303  " com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n"
304  " $value_enum_type$.internalGetValueMap(),\n"
305  " $unrecognized_value$);\n");
306  printer->Print(
307  variables_,
308  "private static final java.util.Map<$boxed_key_type$, "
309  "$value_enum_type$>\n"
310  "internalGetAdapted$capitalized_name$Map(\n"
311  " java.util.Map<$boxed_key_type$, $boxed_value_type$> map) {\n"
312  " return new com.google.protobuf.Internal.MapAdapter<\n"
313  " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
314  " map, $name$ValueConverter);\n"
315  "}\n");
316  }
317  GenerateMapGetters(printer);
318 }
319 
321  io::Printer* printer) const {
322  printer->Print(variables_,
323  "private com.google.protobuf.MapField<\n"
324  " $type_parameters$> $name$_;\n"
325  "private com.google.protobuf.MapField<$type_parameters$>\n"
326  "internalGet$capitalized_name$() {\n"
327  " if ($name$_ == null) {\n"
328  " return com.google.protobuf.MapField.emptyMapField(\n"
329  " $map_field_parameter$);\n"
330  " }\n"
331  " return $name$_;\n"
332  "}\n"
333  "private com.google.protobuf.MapField<$type_parameters$>\n"
334  "internalGetMutable$capitalized_name$() {\n"
335  " $on_changed$;\n"
336  " if ($name$_ == null) {\n"
337  " $name$_ = com.google.protobuf.MapField.newMapField(\n"
338  " $map_field_parameter$);\n"
339  " }\n"
340  " if (!$name$_.isMutable()) {\n"
341  " $name$_ = $name$_.copy();\n"
342  " }\n"
343  " return $name$_;\n"
344  "}\n");
345  GenerateMapGetters(printer);
346  printer->Print(variables_,
347  "$deprecation$\n"
348  "public Builder ${$clear$capitalized_name$$}$() {\n"
349  " internalGetMutable$capitalized_name$().getMutableMap()\n"
350  " .clear();\n"
351  " return this;\n"
352  "}\n");
353  printer->Annotate("{", "}", descriptor_);
355  printer->Print(variables_,
356  "$deprecation$\n"
357  "public Builder ${$remove$capitalized_name$$}$(\n"
358  " $key_type$ key) {\n"
359  " $key_null_check$\n"
360  " internalGetMutable$capitalized_name$().getMutableMap()\n"
361  " .remove(key);\n"
362  " return this;\n"
363  "}\n");
364  printer->Annotate("{", "}", descriptor_);
365  if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
366  printer->Print(
367  variables_,
368  "/**\n"
369  " * Use alternate mutation accessors instead.\n"
370  " */\n"
371  "@java.lang.Deprecated\n"
372  "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
373  "${$getMutable$capitalized_name$$}$() {\n"
374  " return internalGetAdapted$capitalized_name$Map(\n"
375  " internalGetMutable$capitalized_name$().getMutableMap());\n"
376  "}\n");
377  printer->Annotate("{", "}", descriptor_);
379  printer->Print(variables_,
380  "$deprecation$public Builder ${$put$capitalized_name$$}$(\n"
381  " $key_type$ key,\n"
382  " $value_enum_type$ value) {\n"
383  " $key_null_check$\n"
384  " $value_null_check$\n"
385  " internalGetMutable$capitalized_name$().getMutableMap()\n"
386  " .put(key, $name$ValueConverter.doBackward(value));\n"
387  " return this;\n"
388  "}\n");
389  printer->Annotate("{", "}", descriptor_);
391  printer->Print(
392  variables_,
393  "$deprecation$public Builder ${$putAll$capitalized_name$$}$(\n"
394  " java.util.Map<$boxed_key_type$, $value_enum_type$> values) {\n"
395  " internalGetAdapted$capitalized_name$Map(\n"
396  " internalGetMutable$capitalized_name$().getMutableMap())\n"
397  " .putAll(values);\n"
398  " return this;\n"
399  "}\n");
400  printer->Annotate("{", "}", descriptor_);
402  printer->Print(
403  variables_,
404  "/**\n"
405  " * Use alternate mutation accessors instead.\n"
406  " */\n"
407  "@java.lang.Deprecated\n"
408  "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
409  "${$getMutable$capitalized_name$Value$}$() {\n"
410  " return internalGetMutable$capitalized_name$().getMutableMap();\n"
411  "}\n");
412  printer->Annotate("{", "}", descriptor_);
414  printer->Print(
415  variables_,
416  "$deprecation$public Builder ${$put$capitalized_name$Value$}$(\n"
417  " $key_type$ key,\n"
418  " $value_type$ value) {\n"
419  " $key_null_check$\n"
420  " internalGetMutable$capitalized_name$().getMutableMap()\n"
421  " .put(key, value);\n"
422  " return this;\n"
423  "}\n");
424  printer->Annotate("{", "}", descriptor_);
426  printer->Print(
427  variables_,
428  "$deprecation$public Builder ${$putAll$capitalized_name$Value$}$(\n"
429  " java.util.Map<$boxed_key_type$, $boxed_value_type$> values) {\n"
430  " internalGetMutable$capitalized_name$().getMutableMap()\n"
431  " .putAll(values);\n"
432  " return this;\n"
433  "}\n");
434  printer->Annotate("{", "}", descriptor_);
435  }
436  } else {
437  printer->Print(
438  variables_,
439  "/**\n"
440  " * Use alternate mutation accessors instead.\n"
441  " */\n"
442  "@java.lang.Deprecated\n"
443  "public java.util.Map<$type_parameters$>\n"
444  "${$getMutable$capitalized_name$$}$() {\n"
445  " return internalGetMutable$capitalized_name$().getMutableMap();\n"
446  "}\n");
447  printer->Annotate("{", "}", descriptor_);
449  printer->Print(variables_,
450  "$deprecation$"
451  "public Builder ${$put$capitalized_name$$}$(\n"
452  " $key_type$ key,\n"
453  " $value_type$ value) {\n"
454  " $key_null_check$\n"
455  " $value_null_check$\n"
456  " internalGetMutable$capitalized_name$().getMutableMap()\n"
457  " .put(key, value);\n"
458  " return this;\n"
459  "}\n");
460  printer->Annotate("{", "}", descriptor_);
462  printer->Print(variables_,
463  "$deprecation$\n"
464  "public Builder ${$putAll$capitalized_name$$}$(\n"
465  " java.util.Map<$type_parameters$> values) {\n"
466  " internalGetMutable$capitalized_name$().getMutableMap()\n"
467  " .putAll(values);\n"
468  " return this;\n"
469  "}\n");
470  printer->Annotate("{", "}", descriptor_);
471  }
472 }
473 
475  io::Printer* printer) const {
476  printer->Print(variables_,
477  "$deprecation$\n"
478  "public int ${$get$capitalized_name$Count$}$() {\n"
479  " return internalGet$capitalized_name$().getMap().size();\n"
480  "}\n");
481  printer->Annotate("{", "}", descriptor_);
483  printer->Print(
484  variables_,
485  "$deprecation$\n"
486  "public boolean ${$contains$capitalized_name$$}$(\n"
487  " $key_type$ key) {\n"
488  " $key_null_check$\n"
489  " return internalGet$capitalized_name$().getMap().containsKey(key);\n"
490  "}\n");
491  printer->Annotate("{", "}", descriptor_);
492  if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
493  printer->Print(variables_,
494  "/**\n"
495  " * Use {@link #get$capitalized_name$Map()} instead.\n"
496  " */\n"
497  "@java.lang.Deprecated\n"
498  "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
499  "${$get$capitalized_name$$}$() {\n"
500  " return get$capitalized_name$Map();\n"
501  "}\n");
502  printer->Annotate("{", "}", descriptor_);
504  printer->Print(variables_,
505  "$deprecation$\n"
506  "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
507  "${$get$capitalized_name$Map$}$() {\n"
508  " return internalGetAdapted$capitalized_name$Map(\n"
509  " internalGet$capitalized_name$().getMap());"
510  "}\n");
511  printer->Annotate("{", "}", descriptor_);
513  printer->Print(
514  variables_,
515  "$deprecation$\n"
516  "public $value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n"
517  " $key_type$ key,\n"
518  " $value_enum_type$ defaultValue) {\n"
519  " $key_null_check$\n"
520  " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n"
521  " internalGet$capitalized_name$().getMap();\n"
522  " return map.containsKey(key)\n"
523  " ? $name$ValueConverter.doForward(map.get(key))\n"
524  " : defaultValue;\n"
525  "}\n");
526  printer->Annotate("{", "}", descriptor_);
528  printer->Print(
529  variables_,
530  "$deprecation$\n"
531  "public $value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n"
532  " $key_type$ key) {\n"
533  " $key_null_check$\n"
534  " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n"
535  " internalGet$capitalized_name$().getMap();\n"
536  " if (!map.containsKey(key)) {\n"
537  " throw new java.lang.IllegalArgumentException();\n"
538  " }\n"
539  " return $name$ValueConverter.doForward(map.get(key));\n"
540  "}\n");
541  printer->Annotate("{", "}", descriptor_);
543  printer->Print(
544  variables_,
545  "/**\n"
546  " * Use {@link #get$capitalized_name$ValueMap()} instead.\n"
547  " */\n"
548  "@java.lang.Deprecated\n"
549  "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
550  "${$get$capitalized_name$Value$}$() {\n"
551  " return get$capitalized_name$ValueMap();\n"
552  "}\n");
553  printer->Annotate("{", "}", descriptor_);
555  printer->Print(
556  variables_,
557  "$deprecation$\n"
558  "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
559  "${$get$capitalized_name$ValueMap$}$() {\n"
560  " return internalGet$capitalized_name$().getMap();\n"
561  "}\n");
562  printer->Annotate("{", "}", descriptor_);
564  printer->Print(
565  variables_,
566  "$deprecation$\n"
567  "public $value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n"
568  " $key_type$ key,\n"
569  " $value_type$ defaultValue) {\n"
570  " $key_null_check$\n"
571  " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n"
572  " internalGet$capitalized_name$().getMap();\n"
573  " return map.containsKey(key) ? map.get(key) : defaultValue;\n"
574  "}\n");
575  printer->Annotate("{", "}", descriptor_);
577  printer->Print(
578  variables_,
579  "$deprecation$\n"
580  "public $value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n"
581  " $key_type$ key) {\n"
582  " $key_null_check$\n"
583  " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n"
584  " internalGet$capitalized_name$().getMap();\n"
585  " if (!map.containsKey(key)) {\n"
586  " throw new java.lang.IllegalArgumentException();\n"
587  " }\n"
588  " return map.get(key);\n"
589  "}\n");
590  printer->Annotate("{", "}", descriptor_);
591  }
592  } else {
593  printer->Print(variables_,
594  "/**\n"
595  " * Use {@link #get$capitalized_name$Map()} instead.\n"
596  " */\n"
597  "@java.lang.Deprecated\n"
598  "public java.util.Map<$type_parameters$> "
599  "${$get$capitalized_name$$}$() {\n"
600  " return get$capitalized_name$Map();\n"
601  "}\n");
602  printer->Annotate("{", "}", descriptor_);
604  printer->Print(variables_,
605  "$deprecation$\n"
606  "public java.util.Map<$type_parameters$> "
607  "${$get$capitalized_name$Map$}$() {\n"
608  " return internalGet$capitalized_name$().getMap();\n"
609  "}\n");
610  printer->Annotate("{", "}", descriptor_);
612  printer->Print(
613  variables_,
614  "$deprecation$\n"
615  "public $value_type$ ${$get$capitalized_name$OrDefault$}$(\n"
616  " $key_type$ key,\n"
617  " $value_type$ defaultValue) {\n"
618  " $key_null_check$\n"
619  " java.util.Map<$type_parameters$> map =\n"
620  " internalGet$capitalized_name$().getMap();\n"
621  " return map.containsKey(key) ? map.get(key) : defaultValue;\n"
622  "}\n");
623  printer->Annotate("{", "}", descriptor_);
625  printer->Print(variables_,
626  "$deprecation$\n"
627  "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n"
628  " $key_type$ key) {\n"
629  " $key_null_check$\n"
630  " java.util.Map<$type_parameters$> map =\n"
631  " internalGet$capitalized_name$().getMap();\n"
632  " if (!map.containsKey(key)) {\n"
633  " throw new java.lang.IllegalArgumentException();\n"
634  " }\n"
635  " return map.get(key);\n"
636  "}\n");
637  printer->Annotate("{", "}", descriptor_);
638  }
639 }
640 
642  io::Printer* printer) const {
643  // Nothing to initialize.
644 }
645 
647  io::Printer* printer) const {
648  // Nothing to initialize.
649 }
650 
652  io::Printer* printer) const {
653  printer->Print(variables_,
654  "internalGetMutable$capitalized_name$().clear();\n");
655 }
656 
658  io::Printer* printer) const {
659  printer->Print(variables_,
660  "internalGetMutable$capitalized_name$().mergeFrom(\n"
661  " other.internalGet$capitalized_name$());\n");
662 }
663 
665  io::Printer* printer) const {
666  printer->Print(variables_,
667  "result.$name$_ = internalGet$capitalized_name$();\n"
668  "result.$name$_.makeImmutable();\n");
669 }
670 
672  io::Printer* printer) const {
673  printer->Print(variables_,
674  "if (!$get_mutable_bit_parser$) {\n"
675  " $name$_ = com.google.protobuf.MapField.newMapField(\n"
676  " $map_field_parameter$);\n"
677  " $set_mutable_bit_parser$;\n"
678  "}\n");
680  GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
681  printer->Print(
682  variables_,
683  "com.google.protobuf.ByteString bytes = input.readBytes();\n"
684  "com.google.protobuf.MapEntry<$type_parameters$>\n"
685  "$name$__ = $default_entry$.getParserForType().parseFrom(bytes);\n");
686  printer->Print(
687  variables_,
688  "if ($value_enum_type$.forNumber($name$__.getValue()) == null) {\n"
689  " unknownFields.mergeLengthDelimitedField($number$, bytes);\n"
690  "} else {\n"
691  " $name$_.getMutableMap().put(\n"
692  " $name$__.getKey(), $name$__.getValue());\n"
693  "}\n");
694  } else {
695  printer->Print(
696  variables_,
697  "com.google.protobuf.MapEntry<$type_parameters$>\n"
698  "$name$__ = input.readMessage(\n"
699  " $default_entry$.getParserForType(), extensionRegistry);\n"
700  "$name$_.getMutableMap().put(\n"
701  " $name$__.getKey(), $name$__.getValue());\n");
702  }
703 }
704 
706  io::Printer* printer) const {
707  // Nothing to do here.
708 }
709 
711  io::Printer* printer) const {
712  printer->Print(variables_,
713  "com.google.protobuf.GeneratedMessage$ver$\n"
714  " .serialize$short_key_type$MapTo(\n"
715  " output,\n"
716  " internalGet$capitalized_name$(),\n"
717  " $default_entry$,\n"
718  " $number$);\n");
719 }
720 
722  io::Printer* printer) const {
723  printer->Print(
724  variables_,
725  "for (java.util.Map.Entry<$type_parameters$> entry\n"
726  " : internalGet$capitalized_name$().getMap().entrySet()) {\n"
727  " com.google.protobuf.MapEntry<$type_parameters$>\n"
728  " $name$__ = $default_entry$.newBuilderForType()\n"
729  " .setKey(entry.getKey())\n"
730  " .setValue(entry.getValue())\n"
731  " .build();\n"
732  " size += com.google.protobuf.CodedOutputStream\n"
733  " .computeMessageSize($number$, $name$__);\n"
734  "}\n");
735 }
736 
738  io::Printer* printer) const {
739  printer->Print(variables_,
740  "if (!internalGet$capitalized_name$().equals(\n"
741  " other.internalGet$capitalized_name$())) return false;\n");
742 }
743 
745  printer->Print(
746  variables_,
747  "if (!internalGet$capitalized_name$().getMap().isEmpty()) {\n"
748  " hash = (37 * hash) + $constant_name$;\n"
749  " hash = (53 * hash) + internalGet$capitalized_name$().hashCode();\n"
750  "}\n");
751 }
752 
755 }
756 
757 } // namespace java
758 } // namespace compiler
759 } // namespace protobuf
760 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
GOOGLE_CHECK_EQ
#define GOOGLE_CHECK_EQ(A, B)
Definition: logging.h:156
google::protobuf::compiler::java::ImmutableMapFieldGenerator::variables_
std::map< std::string, std::string > variables_
Definition: java_map_field.h:70
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateHashCode
void GenerateHashCode(io::Printer *printer) const
Definition: java_map_field.cc:744
java_name_resolver.h
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
java_doc_comment.h
java_map_field.h
java_helpers.h
google::protobuf::compiler::java::GenerateGetBitMutableLocal
std::string GenerateGetBitMutableLocal(int bitIndex)
Definition: java_helpers.cc:689
google::protobuf::compiler::java::JAVATYPE_ENUM
@ JAVATYPE_ENUM
Definition: java_helpers.h:213
google::protobuf::compiler::java::ImmutableMapFieldGenerator::~ImmutableMapFieldGenerator
~ImmutableMapFieldGenerator()
Definition: java_map_field.cc:168
google::protobuf::compiler::java::Context
Definition: java_context.h:65
google::protobuf::compiler::java::WriteFieldDocComment
void WriteFieldDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: java_doc_comment.cc:175
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::compiler::java::ClassNameResolver::GetImmutableClassName
std::string GetImmutableClassName(const DescriptorType *descriptor)
Definition: java_name_resolver.h:88
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::java::ImmutableMapFieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: java_map_field.h:69
Descriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:113
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateEqualsCode
void GenerateEqualsCode(io::Printer *printer) const
Definition: java_map_field.cc:737
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateMergingCode
void GenerateMergingCode(io::Printer *printer) const
Definition: java_map_field.cc:657
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateBuilderMembers
void GenerateBuilderMembers(io::Printer *printer) const
Definition: java_map_field.cc:320
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateParsingDoneCode
void GenerateParsingDoneCode(io::Printer *printer) const
Definition: java_map_field.cc:705
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateMapGetters
void GenerateMapGetters(io::Printer *printer) const
Definition: java_map_field.cc:474
google::protobuf::compiler::java::BoxedPrimitiveTypeName
const char * BoxedPrimitiveTypeName(JavaType type)
Definition: java_helpers.cc:400
google::protobuf::compiler::cpp::SetMessageVariables
void SetMessageVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables, const Options &options)
Definition: cpp_map_field.cc:48
google::protobuf::compiler::java::JAVATYPE_MESSAGE
@ JAVATYPE_MESSAGE
Definition: java_helpers.h:214
google::protobuf::compiler::java::SupportUnknownEnumValue
bool SupportUnknownEnumValue(const FileDescriptor *descriptor)
Definition: java_helpers.h:368
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::java::ImmutableMapFieldGenerator::ImmutableMapFieldGenerator
ImmutableMapFieldGenerator(const FieldDescriptor *descriptor, int messageBitIndex, int builderBitIndex, Context *context)
Definition: java_map_field.cc:159
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateBuilderClearCode
void GenerateBuilderClearCode(io::Printer *printer) const
Definition: java_map_field.cc:651
google::protobuf::compiler::java::Context::GetFieldGeneratorInfo
const FieldGeneratorInfo * GetFieldGeneratorInfo(const FieldDescriptor *field) const
Definition: java_context.cc:170
google::protobuf::compiler::java::GetJavaType
JavaType GetJavaType(const FieldDescriptor *field)
Definition: java_helpers.cc:321
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateParsingCode
void GenerateParsingCode(io::Printer *printer) const
Definition: java_map_field.cc:671
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GetBoxedType
std::string GetBoxedType() const
Definition: java_map_field.cc:753
google::protobuf::compiler::java::IsReferenceType
bool IsReferenceType(JavaType type)
Definition: java_helpers.cc:697
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateInterfaceMembers
void GenerateInterfaceMembers(io::Printer *printer) const
Definition: java_map_field.cc:174
printer.h
google::protobuf::compiler::java::ImmutableMapFieldGenerator::name_resolver_
ClassNameResolver * name_resolver_
Definition: java_map_field.h:71
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
google::protobuf::io::Printer::Annotate
void Annotate(const char *varname, const SomeDescriptor *descriptor)
Definition: printer.h:199
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::FieldDescriptor::TYPE_MESSAGE
@ TYPE_MESSAGE
Definition: src/google/protobuf/descriptor.h:536
java
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateFieldBuilderInitializationCode
void GenerateFieldBuilderInitializationCode(io::Printer *printer) const
Definition: java_map_field.cc:641
google::protobuf::compiler::java::FieldTypeName
const char * FieldTypeName(FieldDescriptor::Type field_type)
Definition: java_helpers.cc:445
google::protobuf::compiler::java::UniqueFileScopeIdentifier
std::string UniqueFileScopeIdentifier(const Descriptor *descriptor)
Definition: java_helpers.cc:220
google::protobuf::compiler::java::GenerateSetBitMutableLocal
std::string GenerateSetBitMutableLocal(int bitIndex)
Definition: java_helpers.cc:693
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GetNumBitsForBuilder
int GetNumBitsForBuilder() const
Definition: java_map_field.cc:172
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateInitializationCode
void GenerateInitializationCode(io::Printer *printer) const
Definition: java_map_field.cc:646
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
google::protobuf::compiler::java::SetCommonFieldVariables
void SetCommonFieldVariables(const FieldDescriptor *descriptor, const FieldGeneratorInfo *info, std::map< std::string, std::string > *variables)
Definition: java_field.cc:245
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateMembers
void GenerateMembers(io::Printer *printer) const
Definition: java_map_field.cc:272
google::protobuf::compiler::java::DefaultValue
std::string DefaultValue(const FieldDescriptor *field, bool immutable, ClassNameResolver *name_resolver)
Definition: java_helpers.cc:501
google::protobuf::compiler::php::TypeName
std::string TypeName(FieldDescriptor *field)
java_context.h
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateSerializedSizeCode
void GenerateSerializedSizeCode(io::Printer *printer) const
Definition: java_map_field.cc:721
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateBuildingCode
void GenerateBuildingCode(io::Printer *printer) const
Definition: java_map_field.cc:664
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
google::protobuf::compiler::java::JavaType
JavaType
Definition: java_helpers.h:205
google::protobuf::compiler::java::PrimitiveTypeName
const char * PrimitiveTypeName(JavaType type)
Definition: java_helpers.cc:367
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf::compiler::java::GeneratedCodeVersionSuffix
std::string GeneratedCodeVersionSuffix()
Definition: java_helpers.h:396
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GetNumBitsForMessage
int GetNumBitsForMessage() const
Definition: java_map_field.cc:170
google::protobuf::compiler::java::ImmutableMapFieldGenerator::GenerateSerializationCode
void GenerateSerializationCode(io::Printer *printer) const
Definition: java_map_field.cc:710


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55