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/javanano/javanano_message_field.h>
39 #include <google/protobuf/compiler/javanano/javanano_helpers.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/wire_format.h>
42 #include <google/protobuf/stubs/strutil.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace javanano {
48 
49 using internal::WireFormat;
50 using internal::WireFormatLite;
51 
52 namespace {
53 
54 // TODO(kenton):  Factor out a "SetCommonFieldVariables()" to get rid of
55 //   repeat code between this and the other field types.
SetMessageVariables(const Params & params,const FieldDescriptor * descriptor,map<string,string> * variables)56 void SetMessageVariables(const Params& params,
57     const FieldDescriptor* descriptor, map<string, string>* variables) {
58   (*variables)["name"] =
59     RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
60   (*variables)["capitalized_name"] =
61     RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
62   (*variables)["number"] = SimpleItoa(descriptor->number());
63   (*variables)["type"] = ClassName(params, descriptor->message_type());
64   (*variables)["group_or_message"] =
65     (descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
66     "Group" : "Message";
67   (*variables)["message_name"] = descriptor->containing_type()->name();
68   //(*variables)["message_type"] = descriptor->message_type()->name();
69   (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
70 }
71 
72 }  // namespace
73 
74 // ===================================================================
75 
76 MessageFieldGenerator::
MessageFieldGenerator(const FieldDescriptor * descriptor,const Params & params)77 MessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
78   : FieldGenerator(params), descriptor_(descriptor) {
79   SetMessageVariables(params, descriptor, &variables_);
80 }
81 
~MessageFieldGenerator()82 MessageFieldGenerator::~MessageFieldGenerator() {}
83 
84 void MessageFieldGenerator::
GenerateMembers(io::Printer * printer,bool) const85 GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
86   printer->Print(variables_,
87     "public $type$ $name$;\n");
88 }
89 
90 void MessageFieldGenerator::
GenerateClearCode(io::Printer * printer) const91 GenerateClearCode(io::Printer* printer) const {
92   printer->Print(variables_,
93     "$name$ = null;\n");
94 }
95 
96 void MessageFieldGenerator::
GenerateMergingCode(io::Printer * printer) const97 GenerateMergingCode(io::Printer* printer) const {
98   printer->Print(variables_,
99     "if (this.$name$ == null) {\n"
100     "  this.$name$ = new $type$();\n"
101     "}\n");
102 
103   if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
104     printer->Print(variables_,
105       "input.readGroup(this.$name$, $number$);\n");
106   } else {
107     printer->Print(variables_,
108       "input.readMessage(this.$name$);\n");
109   }
110 }
111 
112 void MessageFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const113 GenerateSerializationCode(io::Printer* printer) const {
114   printer->Print(variables_,
115     "if (this.$name$ != null) {\n"
116     "  output.write$group_or_message$($number$, this.$name$);\n"
117     "}\n");
118 }
119 
120 void MessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const121 GenerateSerializedSizeCode(io::Printer* printer) const {
122   printer->Print(variables_,
123     "if (this.$name$ != null) {\n"
124     "  size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
125     "    .compute$group_or_message$Size($number$, this.$name$);\n"
126     "}\n");
127 }
128 
129 void MessageFieldGenerator::
GenerateFixClonedCode(io::Printer * printer) const130 GenerateFixClonedCode(io::Printer* printer) const {
131   printer->Print(variables_,
132     "if (this.$name$ != null) {\n"
133     "  cloned.$name$ = this.$name$.clone();\n"
134     "}\n");
135 }
136 
137 void MessageFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const138 GenerateEqualsCode(io::Printer* printer) const {
139   printer->Print(variables_,
140     "if (this.$name$ == null) { \n"
141     "  if (other.$name$ != null) {\n"
142     "    return false;\n"
143     "  }\n"
144     "} else {\n"
145     "  if (!this.$name$.equals(other.$name$)) {\n"
146     "    return false;\n"
147     "  }\n"
148     "}\n");
149 }
150 
151 void MessageFieldGenerator::
GenerateHashCodeCode(io::Printer * printer) const152 GenerateHashCodeCode(io::Printer* printer) const {
153   printer->Print(variables_,
154     "result = 31 * result +\n"
155     "    (this.$name$ == null ? 0 : this.$name$.hashCode());\n");
156 }
157 
158 // ===================================================================
159 
160 RepeatedMessageFieldGenerator::
RepeatedMessageFieldGenerator(const FieldDescriptor * descriptor,const Params & params)161 RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
162   : FieldGenerator(params), descriptor_(descriptor) {
163   SetMessageVariables(params, descriptor, &variables_);
164 }
165 
~RepeatedMessageFieldGenerator()166 RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
167 
168 void RepeatedMessageFieldGenerator::
GenerateMembers(io::Printer * printer,bool) const169 GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
170   printer->Print(variables_,
171     "public $type$[] $name$;\n");
172 }
173 
174 void RepeatedMessageFieldGenerator::
GenerateClearCode(io::Printer * printer) const175 GenerateClearCode(io::Printer* printer) const {
176   printer->Print(variables_,
177     "$name$ = $type$.emptyArray();\n");
178 }
179 
180 void RepeatedMessageFieldGenerator::
GenerateMergingCode(io::Printer * printer) const181 GenerateMergingCode(io::Printer* printer) const {
182   // First, figure out the length of the array, then parse.
183   printer->Print(variables_,
184     "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
185     "    .getRepeatedFieldArrayLength(input, $tag$);\n"
186     "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
187     "$type$[] newArray =\n"
188     "    new $type$[i + arrayLength];\n"
189     "if (i != 0) {\n"
190     "  java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
191     "}\n"
192     "for (; i < newArray.length - 1; i++) {\n"
193     "  newArray[i] = new $type$();\n");
194 
195   if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
196     printer->Print(variables_,
197       "  input.readGroup(newArray[i], $number$);\n");
198   } else {
199     printer->Print(variables_,
200       "  input.readMessage(newArray[i]);\n");
201   }
202 
203   printer->Print(variables_,
204     "  input.readTag();\n"
205     "}\n"
206     "// Last one without readTag.\n"
207     "newArray[i] = new $type$();\n");
208 
209   if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
210     printer->Print(variables_,
211       "input.readGroup(newArray[i], $number$);\n");
212   } else {
213     printer->Print(variables_,
214       "input.readMessage(newArray[i]);\n");
215   }
216 
217   printer->Print(variables_,
218     "this.$name$ = newArray;\n");
219 }
220 
221 void RepeatedMessageFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const222 GenerateSerializationCode(io::Printer* printer) const {
223   printer->Print(variables_,
224     "if (this.$name$ != null && this.$name$.length > 0) {\n"
225     "  for (int i = 0; i < this.$name$.length; i++) {\n"
226     "    $type$ element = this.$name$[i];\n"
227     "    if (element != null) {\n"
228     "      output.write$group_or_message$($number$, element);\n"
229     "    }\n"
230     "  }\n"
231     "}\n");
232 }
233 
234 void RepeatedMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const235 GenerateSerializedSizeCode(io::Printer* printer) const {
236   printer->Print(variables_,
237     "if (this.$name$ != null && this.$name$.length > 0) {\n"
238     "  for (int i = 0; i < this.$name$.length; i++) {\n"
239     "    $type$ element = this.$name$[i];\n"
240     "    if (element != null) {\n"
241     "      size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
242     "        .compute$group_or_message$Size($number$, element);\n"
243     "    }\n"
244     "  }\n"
245     "}\n");
246 }
247 
248 void RepeatedMessageFieldGenerator::
GenerateFixClonedCode(io::Printer * printer) const249 GenerateFixClonedCode(io::Printer* printer) const {
250   printer->Print(variables_,
251     "if (this.$name$ != null && this.$name$.length > 0) {\n"
252     "  cloned.$name$ = new $type$[this.$name$.length];\n"
253     "  for (int i = 0; i < this.$name$.length; i++) {\n"
254     "    if (this.$name$[i] != null) {\n"
255     "      cloned.$name$[i] = this.$name$[i].clone();\n"
256     "    }\n"
257     "  }\n"
258     "}\n");
259 }
260 
261 void RepeatedMessageFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const262 GenerateEqualsCode(io::Printer* printer) const {
263   printer->Print(variables_,
264     "if (!com.google.protobuf.nano.InternalNano.equals(\n"
265     "    this.$name$, other.$name$)) {\n"
266     "  return false;\n"
267     "}\n");
268 }
269 
270 void RepeatedMessageFieldGenerator::
GenerateHashCodeCode(io::Printer * printer) const271 GenerateHashCodeCode(io::Printer* printer) const {
272   printer->Print(variables_,
273     "result = 31 * result\n"
274     "    + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
275 }
276 
277 }  // namespace javanano
278 }  // namespace compiler
279 }  // namespace protobuf
280 }  // namespace google
281