javamicro_primitive_field.cc revision e2d542951c059563a3b7f74c257dac4f222d9dc5
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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// Author: kenton@google.com (Kenton Varda)
32//  Based on original Protocol Buffers design by
33//  Sanjay Ghemawat, Jeff Dean, and others.
34
35#include <map>
36#include <string>
37
38#include <google/protobuf/compiler/javamicro/javamicro_primitive_field.h>
39#include <google/protobuf/stubs/common.h>
40#include <google/protobuf/compiler/javamicro/javamicro_helpers.h>
41#include <google/protobuf/io/printer.h>
42#include <google/protobuf/wire_format.h>
43#include <google/protobuf/stubs/strutil.h>
44
45namespace google {
46namespace protobuf {
47namespace compiler {
48namespace javamicro {
49
50using internal::WireFormat;
51using internal::WireFormatLite;
52
53namespace {
54
55const char* PrimitiveTypeName(JavaType type) {
56  switch (type) {
57    case JAVATYPE_INT    : return "int";
58    case JAVATYPE_LONG   : return "long";
59    case JAVATYPE_FLOAT  : return "float";
60    case JAVATYPE_DOUBLE : return "double";
61    case JAVATYPE_BOOLEAN: return "boolean";
62    case JAVATYPE_STRING : return "java.lang.String";
63    case JAVATYPE_BYTES  : return "com.google.protobuf.micro.ByteStringMicro";
64    case JAVATYPE_ENUM   : return NULL;
65    case JAVATYPE_MESSAGE: return NULL;
66
67    // No default because we want the compiler to complain if any new
68    // JavaTypes are added.
69  }
70
71  GOOGLE_LOG(FATAL) << "Can't get here.";
72  return NULL;
73}
74
75bool IsReferenceType(JavaType type) {
76  switch (type) {
77    case JAVATYPE_INT    : return false;
78    case JAVATYPE_LONG   : return false;
79    case JAVATYPE_FLOAT  : return false;
80    case JAVATYPE_DOUBLE : return false;
81    case JAVATYPE_BOOLEAN: return false;
82    case JAVATYPE_STRING : return true;
83    case JAVATYPE_BYTES  : return true;
84    case JAVATYPE_ENUM   : return false;
85    case JAVATYPE_MESSAGE: return true;
86
87    // No default because we want the compiler to complain if any new
88    // JavaTypes are added.
89  }
90
91  GOOGLE_LOG(FATAL) << "Can't get here.";
92  return false;
93}
94
95const char* GetCapitalizedType(const FieldDescriptor* field) {
96  switch (field->type()) {
97    case FieldDescriptor::TYPE_INT32   : return "Int32"   ;
98    case FieldDescriptor::TYPE_UINT32  : return "UInt32"  ;
99    case FieldDescriptor::TYPE_SINT32  : return "SInt32"  ;
100    case FieldDescriptor::TYPE_FIXED32 : return "Fixed32" ;
101    case FieldDescriptor::TYPE_SFIXED32: return "SFixed32";
102    case FieldDescriptor::TYPE_INT64   : return "Int64"   ;
103    case FieldDescriptor::TYPE_UINT64  : return "UInt64"  ;
104    case FieldDescriptor::TYPE_SINT64  : return "SInt64"  ;
105    case FieldDescriptor::TYPE_FIXED64 : return "Fixed64" ;
106    case FieldDescriptor::TYPE_SFIXED64: return "SFixed64";
107    case FieldDescriptor::TYPE_FLOAT   : return "Float"   ;
108    case FieldDescriptor::TYPE_DOUBLE  : return "Double"  ;
109    case FieldDescriptor::TYPE_BOOL    : return "Bool"    ;
110    case FieldDescriptor::TYPE_STRING  : return "String"  ;
111    case FieldDescriptor::TYPE_BYTES   : return "Bytes"   ;
112    case FieldDescriptor::TYPE_ENUM    : return "Enum"    ;
113    case FieldDescriptor::TYPE_GROUP   : return "Group"   ;
114    case FieldDescriptor::TYPE_MESSAGE : return "Message" ;
115
116    // No default because we want the compiler to complain if any new
117    // types are added.
118  }
119
120  GOOGLE_LOG(FATAL) << "Can't get here.";
121  return NULL;
122}
123
124// For encodings with fixed sizes, returns that size in bytes.  Otherwise
125// returns -1.
126int FixedSize(FieldDescriptor::Type type) {
127  switch (type) {
128    case FieldDescriptor::TYPE_INT32   : return -1;
129    case FieldDescriptor::TYPE_INT64   : return -1;
130    case FieldDescriptor::TYPE_UINT32  : return -1;
131    case FieldDescriptor::TYPE_UINT64  : return -1;
132    case FieldDescriptor::TYPE_SINT32  : return -1;
133    case FieldDescriptor::TYPE_SINT64  : return -1;
134    case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
135    case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
136    case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
137    case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
138    case FieldDescriptor::TYPE_FLOAT   : return WireFormatLite::kFloatSize;
139    case FieldDescriptor::TYPE_DOUBLE  : return WireFormatLite::kDoubleSize;
140
141    case FieldDescriptor::TYPE_BOOL    : return WireFormatLite::kBoolSize;
142    case FieldDescriptor::TYPE_ENUM    : return -1;
143
144    case FieldDescriptor::TYPE_STRING  : return -1;
145    case FieldDescriptor::TYPE_BYTES   : return -1;
146    case FieldDescriptor::TYPE_GROUP   : return -1;
147    case FieldDescriptor::TYPE_MESSAGE : return -1;
148
149    // No default because we want the compiler to complain if any new
150    // types are added.
151  }
152  GOOGLE_LOG(FATAL) << "Can't get here.";
153  return -1;
154}
155
156// Return true if the type is a that has variable length
157// for instance String's.
158bool IsVariableLenType(JavaType type) {
159  switch (type) {
160    case JAVATYPE_INT    : return false;
161    case JAVATYPE_LONG   : return false;
162    case JAVATYPE_FLOAT  : return false;
163    case JAVATYPE_DOUBLE : return false;
164    case JAVATYPE_BOOLEAN: return false;
165    case JAVATYPE_STRING : return true;
166    case JAVATYPE_BYTES  : return true;
167    case JAVATYPE_ENUM   : return false;
168    case JAVATYPE_MESSAGE: return true;
169
170    // No default because we want the compiler to complain if any new
171    // JavaTypes are added.
172  }
173
174  GOOGLE_LOG(FATAL) << "Can't get here.";
175  return false;
176}
177
178bool IsStringUtf8Handling(const FieldDescriptor* descriptor,
179      const Params params) {
180  return ((params.optimization() == JAVAMICRO_OPT_SPEED)
181      && (GetJavaType(descriptor) == JAVATYPE_STRING));
182}
183
184void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params params,
185                           map<string, string>* variables) {
186  (*variables)["name"] =
187    UnderscoresToCamelCase(descriptor);
188  (*variables)["capitalized_name"] =
189    UnderscoresToCapitalizedCamelCase(descriptor);
190  (*variables)["number"] = SimpleItoa(descriptor->number());
191  if (IsStringUtf8Handling(descriptor, params)) {
192    (*variables)["type"] = "com.google.protobuf.micro.StringUtf8Micro";
193    string defaultValue = DefaultValue(params, descriptor);
194    if (defaultValue == "\"\"") {
195      (*variables)["default"] =
196          "com.google.protobuf.micro.StringUtf8Micro.EMPTY";
197    } else {
198      (*variables)["default"] = "new com.google.protobuf.micro.StringUtf8Micro("
199              + defaultValue + ")";
200    }
201  } else {
202    (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
203    (*variables)["default"] = DefaultValue(params, descriptor);
204  }
205  (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
206  (*variables)["capitalized_type"] = GetCapitalizedType(descriptor);
207  (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
208  (*variables)["tag_size"] = SimpleItoa(
209      WireFormat::TagSize(descriptor->number(), descriptor->type()));
210  if (IsReferenceType(GetJavaType(descriptor))) {
211    (*variables)["null_check"] =
212        "  if (value == null) {\n"
213        "    throw new NullPointerException();\n"
214        "  }\n";
215  } else {
216    (*variables)["null_check"] = "";
217  }
218  int fixed_size = FixedSize(descriptor->type());
219  if (fixed_size != -1) {
220    (*variables)["fixed_size"] = SimpleItoa(fixed_size);
221  }
222  (*variables)["message_name"] = descriptor->containing_type()->name();
223}
224}  // namespace
225
226// ===================================================================
227
228PrimitiveFieldGenerator::
229PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
230  : FieldGenerator(params), descriptor_(descriptor) {
231  SetPrimitiveVariables(descriptor, params, &variables_);
232}
233
234PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
235
236void PrimitiveFieldGenerator::
237GenerateMembers(io::Printer* printer) const {
238  printer->Print(variables_,
239    "private boolean has$capitalized_name$;\n"
240    "private $type$ $name$_ = $default$;\n"
241    "public boolean has$capitalized_name$() { return has$capitalized_name$; }\n");
242  if (IsStringUtf8Handling(descriptor_, params_)) {
243    printer->Print(variables_,
244      "public String get$capitalized_name$() { return $name$_.getString(); }\n"
245      "public $type$ get$capitalized_name$StringUtf8() { return $name$_; }\n"
246      "public $message_name$ set$capitalized_name$(String value) {\n"
247      "  has$capitalized_name$ = true;\n"
248      "  if ($name$_ == $default$) {\n"
249      "    $name$_ = new $type$(value);\n"
250      "  } else {\n"
251      "    $name$_.setString(value);\n"
252      "  }\n"
253      "  return this;\n"
254      "}\n"
255      "public $message_name$ clear$capitalized_name$() {\n"
256      "  has$capitalized_name$ = false;\n"
257      "  $name$_ = $default$;\n"
258      "  return this;\n"
259      "}\n");
260  } else {
261    printer->Print(variables_,
262      "public $type$ get$capitalized_name$() { return $name$_; }\n");
263    if (IsVariableLenType(GetJavaType(descriptor_))) {
264      printer->Print(variables_,
265        "public $message_name$ set$capitalized_name$($type$ value) {\n"
266        "  has$capitalized_name$ = true;\n"
267        "  $name$_ = value;\n"
268        "  return this;\n"
269        "}\n"
270        "public $message_name$ clear$capitalized_name$() {\n"
271        "  has$capitalized_name$ = false;\n"
272        "  $name$_ = $default$;\n"
273        "  return this;\n"
274        "}\n");
275    } else {
276      printer->Print(variables_,
277        "public $message_name$ set$capitalized_name$($type$ value) {\n"
278        "  has$capitalized_name$ = true;\n"
279        "  $name$_ = value;\n"
280        "  return this;\n"
281        "}\n"
282        "public $message_name$ clear$capitalized_name$() {\n"
283        "  has$capitalized_name$ = false;\n"
284        "  $name$_ = $default$;\n"
285        "  return this;\n"
286        "}\n");
287    }
288  }
289}
290
291void PrimitiveFieldGenerator::
292GenerateMergingCode(io::Printer* printer) const {
293  printer->Print(variables_,
294    "if (other.has$capitalized_name$()) {\n"
295    "  set$capitalized_name$(other.get$capitalized_name$());\n"
296    "}\n");
297}
298
299void PrimitiveFieldGenerator::
300GenerateParsingCode(io::Printer* printer) const {
301  printer->Print(variables_,
302    "set$capitalized_name$(input.read$capitalized_type$());\n");
303}
304
305void PrimitiveFieldGenerator::
306GenerateSerializationCode(io::Printer* printer) const {
307  if (IsStringUtf8Handling(descriptor_, params_)) {
308    printer->Print(variables_,
309      "if (has$capitalized_name$()) {\n"
310      "  output.writeStringUtf8($number$, get$capitalized_name$StringUtf8());\n"
311      "}\n");
312  } else {
313    printer->Print(variables_,
314      "if (has$capitalized_name$()) {\n"
315      "  output.write$capitalized_type$($number$, get$capitalized_name$());\n"
316      "}\n");
317  }
318}
319
320void PrimitiveFieldGenerator::
321GenerateSerializedSizeCode(io::Printer* printer) const {
322  if (IsStringUtf8Handling(descriptor_, params_)) {
323    printer->Print(variables_,
324      "if (has$capitalized_name$()) {\n"
325      "  size += com.google.protobuf.micro.CodedOutputStreamMicro\n"
326      "    .computeStringUtf8Size($number$, get$capitalized_name$StringUtf8());\n"
327      "}\n");
328  } else {
329    printer->Print(variables_,
330      "if (has$capitalized_name$()) {\n"
331      "  size += com.google.protobuf.micro.CodedOutputStreamMicro\n"
332      "    .compute$capitalized_type$Size($number$, get$capitalized_name$());\n"
333      "}\n");
334  }
335}
336
337string PrimitiveFieldGenerator::GetBoxedType() const {
338  return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
339}
340
341// ===================================================================
342
343RepeatedPrimitiveFieldGenerator::
344RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
345  : FieldGenerator(params), descriptor_(descriptor) {
346  SetPrimitiveVariables(descriptor, params, &variables_);
347  if (descriptor_->options().packed()) {
348    GOOGLE_LOG(FATAL) << "MicroRuntime does not support packed";
349  }
350}
351
352RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
353
354void RepeatedPrimitiveFieldGenerator::
355GenerateMembers(io::Printer* printer) const {
356  if (IsStringUtf8Handling(descriptor_, params_)) {
357    if (params_.java_use_vector()) {
358      printer->Print(variables_,
359        "private java.util.Vector $name$_ = new java.util.Vector();\n"
360        "public java.util.Vector get$capitalized_name$List() {\n"
361        "  return $name$_;\n"
362        "}\n"
363        "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
364        "public String get$capitalized_name$(int index) {\n"
365        "  return (($type$)$name$_.elementAt(index)).getString();\n"
366        "}\n"
367        "public $type$ get$capitalized_name$StringUtf8(int index) {\n"
368        "  return ($type$)$name$_.elementAt(index);\n"
369        "}\n"
370        "public $message_name$ set$capitalized_name$(int index, String value) {\n"
371        "$null_check$"
372        "  $name$_.setElementAt(new $type$(value), index);\n"
373        "  return this;\n"
374        "}\n"
375        "public $message_name$ add$capitalized_name$(String value) {\n"
376        "$null_check$"
377        "  $name$_.addElement(new $type$(value));\n"
378        "  return this;\n"
379        "}\n"
380        "public $message_name$ clear$capitalized_name$() {\n"
381        "  $name$_.removeAllElements();\n"
382        "  return this;\n"
383        "}\n");
384    } else {
385      printer->Print(variables_,
386        "private java.util.List<$type$> $name$_ =\n"
387        "  java.util.Collections.emptyList();\n"
388        "public java.util.List<$type$> get$capitalized_name$List() {\n"
389        "  return $name$_;\n"   // note:  unmodifiable list
390        "}\n"
391        "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
392        "public String get$capitalized_name$(int index) {\n"
393        "  return $name$_.get(index).getString();\n"
394        "}\n"
395        "public $message_name$ set$capitalized_name$(int index, String value) {\n"
396        "$null_check$"
397        "  $name$_.set(index, new $type$(value));\n"
398        "  return this;\n"
399        "}\n"
400        "public $message_name$ add$capitalized_name$(String value) {\n"
401        "$null_check$"
402        "  if ($name$_.isEmpty()) {\n"
403        "    $name$_ = new java.util.ArrayList<$type$>();\n"
404        "  }\n"
405        "  $name$_.add(new $type$(value));\n"
406        "  return this;\n"
407        "}\n"
408        "public $message_name$ clear$capitalized_name$() {\n"
409        "  $name$_ = java.util.Collections.emptyList();\n"
410        "  return this;\n"
411        "}\n");
412    }
413  } else if (params_.java_use_vector()) {
414    if (IsReferenceType(GetJavaType(descriptor_))) {
415      printer->Print(variables_,
416        "private java.util.Vector $name$_ = new java.util.Vector();\n"
417        "public java.util.Vector get$capitalized_name$List() {\n"
418        "  return $name$_;\n"
419        "}\n"
420        "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
421        "public $type$ get$capitalized_name$(int index) {\n"
422        "  return ($type$) $name$_.elementAt(index);\n"
423        "}\n"
424        "public $message_name$ set$capitalized_name$(int index, $type$ value) {\n"
425        "$null_check$"
426        "  $name$_.setElementAt(value, index);\n"
427        "  return this;\n"
428        "}\n"
429        "public $message_name$ add$capitalized_name$($type$ value) {\n"
430        "$null_check$"
431        "  $name$_.addElement(value);\n"
432        "  return this;\n"
433        "}\n"
434        "public $message_name$ clear$capitalized_name$() {\n"
435        "  $name$_.removeAllElements();\n"
436        "  return this;\n"
437        "}\n");
438    } else {
439      printer->Print(variables_,
440        "private java.util.Vector $name$_ = new java.util.Vector();\n"
441        "public java.util.Vector get$capitalized_name$List() {\n"
442        "  return $name$_;\n"
443        "}\n"
444        "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
445        "public $type$ get$capitalized_name$(int index) {\n"
446        "  return (($boxed_type$)$name$_.elementAt(index)).$type$Value();\n"
447        "}\n"
448        "public $message_name$ set$capitalized_name$(int index, $type$ value) {\n"
449        "$null_check$"
450        "  $name$_.setElementAt(new $boxed_type$(value), index);\n"
451        "  return this;\n"
452        "}\n"
453        "public $message_name$ add$capitalized_name$($type$ value) {\n"
454        "$null_check$"
455        "  $name$_.addElement(new $boxed_type$(value));\n"
456        "  return this;\n"
457        "}\n"
458        "public $message_name$ clear$capitalized_name$() {\n"
459        "  $name$_.removeAllElements();\n"
460        "  return this;\n"
461        "}\n");
462    }
463  } else {
464    printer->Print(variables_,
465      "private java.util.List<$boxed_type$> $name$_ =\n"
466      "  java.util.Collections.emptyList();\n"
467      "public java.util.List<$boxed_type$> get$capitalized_name$List() {\n"
468      "  return $name$_;\n"   // note:  unmodifiable list
469      "}\n"
470      "public int get$capitalized_name$Count() { return $name$_.size(); }\n"
471      "public $type$ get$capitalized_name$(int index) {\n"
472      "  return $name$_.get(index);\n"
473      "}\n"
474      "public $message_name$ set$capitalized_name$(int index, $type$ value) {\n"
475      "$null_check$"
476      "  $name$_.set(index, value);\n"
477      "  return this;\n"
478      "}\n"
479      "public $message_name$ add$capitalized_name$($type$ value) {\n"
480      "$null_check$"
481      "  if ($name$_.isEmpty()) {\n"
482      "    $name$_ = new java.util.ArrayList<$boxed_type$>();\n"
483      "  }\n"
484      "  $name$_.add(value);\n"
485      "  return this;\n"
486      "}\n"
487      "public $message_name$ clear$capitalized_name$() {\n"
488      "  $name$_ = java.util.Collections.emptyList();\n"
489      "  return this;\n"
490      "}\n");
491  }
492  if (descriptor_->options().packed()) {
493    printer->Print(variables_,
494      "private int $name$MemoizedSerializedSize;\n");
495  }
496}
497
498void RepeatedPrimitiveFieldGenerator::
499GenerateMergingCode(io::Printer* printer) const {
500  if (params_.java_use_vector()) {
501    printer->Print(variables_,
502      "if (other.$name$_.size() != 0) {\n"
503      "  for (int i = 0; i < other.$name$_.size(); i++)) {\n"
504      "    result.$name$_.addElement(other.$name$_.elementAt(i));\n"
505      "  }\n"
506      "}\n");
507  } else {
508    printer->Print(variables_,
509      "if (!other.$name$_.isEmpty()) {\n"
510      "  if (result.$name$_.isEmpty()) {\n"
511      "    result.$name$_ = new java.util.ArrayList<$type$>();\n"
512      "  }\n"
513      "  result.$name$_.addAll(other.$name$_);\n"
514      "}\n");
515  }
516}
517
518void RepeatedPrimitiveFieldGenerator::
519GenerateParsingCode(io::Printer* printer) const {
520  if (descriptor_->options().packed()) {
521    printer->Print(variables_,
522      "int length = input.readRawVarint32();\n"
523      "int limit = input.pushLimit(length);\n"
524      "while (input.getBytesUntilLimit() > 0) {\n"
525      "  add$capitalized_name$(input.read$capitalized_type$());\n"
526      "}\n"
527      "input.popLimit(limit);\n");
528  } else {
529    printer->Print(variables_,
530      "add$capitalized_name$(input.read$capitalized_type$());\n");
531  }
532}
533
534void RepeatedPrimitiveFieldGenerator::
535GenerateSerializationCode(io::Printer* printer) const {
536  if (descriptor_->options().packed()) {
537    printer->Print(variables_,
538        "if (get$capitalized_name$List().size() > 0) {\n"
539        "  output.writeRawVarint32($tag$);\n"
540        "  output.writeRawVarint32($name$MemoizedSerializedSize);\n"
541        "}\n");
542    if (params_.java_use_vector()) {
543      printer->Print(variables_,
544        "for (int i = 0; i < get$capitalized_name$List().size(); i++) {\n"
545        "  output.write$capitalized_type$NoTag(get$capitalized_name$(i));\n"
546        "}\n");
547    } else {
548      printer->Print(variables_,
549        "for ($type$ element : get$capitalized_name$List()) {\n"
550        "  output.write$capitalized_type$NoTag(element);\n"
551        "}\n");
552    }
553  } else {
554    if (params_.java_use_vector()) {
555      if (IsStringUtf8Handling(descriptor_, params_)) {
556        printer->Print(variables_,
557          "for (int i = 0; i < get$capitalized_name$List().size(); i++) {\n"
558          "  output.writeStringUtf8($number$, get$capitalized_name$StringUtf8(i));\n"
559          "}\n");
560      } else {
561        printer->Print(variables_,
562          "for (int i = 0; i < get$capitalized_name$List().size(); i++) {\n"
563          "  output.write$capitalized_type$($number$, get$capitalized_name$(i));\n"
564          "}\n");
565      }
566    } else {
567      if (IsStringUtf8Handling(descriptor_, params_)) {
568        printer->Print(variables_,
569          "for ($type$ element : get$capitalized_name$List()) {\n"
570          "  output.writeStringUtf8($number$, element);\n"
571          "}\n");
572      } else {
573        printer->Print(variables_,
574          "for ($type$ element : get$capitalized_name$List()) {\n"
575          "  output.write$capitalized_type$($number$, element);\n"
576          "}\n");
577      }
578    }
579  }
580}
581
582void RepeatedPrimitiveFieldGenerator::
583GenerateSerializedSizeCode(io::Printer* printer) const {
584  printer->Print(variables_,
585    "{\n"
586    "  int dataSize = 0;\n");
587  printer->Indent();
588
589  if (FixedSize(descriptor_->type()) == -1) {
590    if (params_.java_use_vector()) {
591      printer->Print(variables_,
592        "for (int i = 0; i < get$capitalized_name$List().size(); i++) {\n"
593        "  dataSize += com.google.protobuf.micro.CodedOutputStreamMicro\n");
594      if (IsStringUtf8Handling(descriptor_, params_)) {
595        printer->Print(variables_,
596          "    .computeStringUtf8SizeNoTag(get$capitalized_name$StringUtf8(i));\n"
597          "}\n");
598      } else {
599        printer->Print(variables_,
600          "    .compute$capitalized_type$SizeNoTag(($type$)get$capitalized_name$(i));\n"
601          "}\n");
602      }
603    } else {
604      printer->Print(variables_,
605        "for ($type$ element : get$capitalized_name$List()) {\n"
606        "  dataSize += com.google.protobuf.micro.CodedOutputStreamMicro\n");
607      if (IsStringUtf8Handling(descriptor_, params_)) {
608        printer->Print(variables_,
609          "    .computeStringUtf8SizeNoTag(element);\n"
610          "}\n");
611      } else {
612        printer->Print(variables_,
613          "    .compute$capitalized_type$SizeNoTag(element);\n"
614          "}\n");
615      }
616    }
617  } else {
618    printer->Print(variables_,
619      "dataSize = $fixed_size$ * get$capitalized_name$List().size();\n");
620  }
621
622  printer->Print(
623      "size += dataSize;\n");
624
625  if (descriptor_->options().packed()) {
626    if (params_.java_use_vector()) {
627      printer->Print(variables_,
628        "if (get$capitalized_name$List().size() != 0) {\n");
629    } else {
630      printer->Print(variables_,
631        "if (!get$capitalized_name$List().isEmpty()) {\n");
632    }
633    printer->Print(variables_,
634        "  size += $tag_size$;\n"
635        "  size += com.google.protobuf.micro.CodedOutputStreamMicro\n"
636        "      .computeInt32SizeNoTag(dataSize);\n"
637        "}\n");
638  } else {
639    printer->Print(variables_,
640      "size += $tag_size$ * get$capitalized_name$List().size();\n");
641  }
642
643  // cache the data size for packed fields.
644  if (descriptor_->options().packed()) {
645    printer->Print(variables_,
646      "$name$MemoizedSerializedSize = dataSize;\n");
647  }
648
649  printer->Outdent();
650  printer->Print("}\n");
651}
652
653string RepeatedPrimitiveFieldGenerator::GetBoxedType() const {
654  return BoxedPrimitiveTypeName(GetJavaType(descriptor_));
655}
656
657}  // namespace javamicro
658}  // namespace compiler
659}  // namespace protobuf
660}  // namespace google
661