• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/compiler/javanano/javanano_map_field.h>
32 #include <google/protobuf/compiler/javanano/javanano_helpers.h>
33 #include <google/protobuf/stubs/common.h>
34 #include <google/protobuf/io/printer.h>
35 #include <google/protobuf/wire_format.h>
36 #include <google/protobuf/stubs/strutil.h>
37 
38 namespace google {
39 namespace protobuf {
40 namespace compiler {
41 namespace javanano {
42 
43 namespace {
44 
TypeName(const Params & params,const FieldDescriptor * field,bool boxed)45 string TypeName(const Params& params, const FieldDescriptor* field,
46                 bool boxed) {
47   JavaType java_type = GetJavaType(field);
48   switch (java_type) {
49     case JAVATYPE_MESSAGE:
50       return ClassName(params, field->message_type());
51     case JAVATYPE_INT:
52     case JAVATYPE_LONG:
53     case JAVATYPE_FLOAT:
54     case JAVATYPE_DOUBLE:
55     case JAVATYPE_BOOLEAN:
56     case JAVATYPE_STRING:
57     case JAVATYPE_BYTES:
58     case JAVATYPE_ENUM:
59       if (boxed) {
60         return BoxedPrimitiveTypeName(java_type);
61       } else {
62         return PrimitiveTypeName(java_type);
63       }
64     // No default because we want the compiler to complain if any new JavaTypes
65     // are added..
66   }
67 
68   GOOGLE_LOG(FATAL) << "should not reach here.";
69   return "";
70 }
71 
KeyField(const FieldDescriptor * descriptor)72 const FieldDescriptor* KeyField(const FieldDescriptor* descriptor) {
73   GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
74   const Descriptor* message = descriptor->message_type();
75   GOOGLE_CHECK(message->options().map_entry());
76   return message->FindFieldByName("key");
77 }
78 
ValueField(const FieldDescriptor * descriptor)79 const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) {
80   GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
81   const Descriptor* message = descriptor->message_type();
82   GOOGLE_CHECK(message->options().map_entry());
83   return message->FindFieldByName("value");
84 }
85 
SetMapVariables(const Params & params,const FieldDescriptor * descriptor,map<string,string> * variables)86 void SetMapVariables(const Params& params,
87     const FieldDescriptor* descriptor, map<string, string>* variables) {
88   const FieldDescriptor* key = KeyField(descriptor);
89   const FieldDescriptor* value = ValueField(descriptor);
90   (*variables)["name"] =
91     RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
92   (*variables)["number"] = SimpleItoa(descriptor->number());
93   (*variables)["key_type"] = TypeName(params, key, false);
94   (*variables)["boxed_key_type"] = TypeName(params,key, true);
95   (*variables)["key_desc_type"] =
96       "TYPE_" + ToUpper(FieldDescriptor::TypeName(key->type()));
97   (*variables)["key_tag"] = SimpleItoa(internal::WireFormat::MakeTag(key));
98   (*variables)["value_type"] = TypeName(params, value, false);
99   (*variables)["boxed_value_type"] = TypeName(params, value, true);
100   (*variables)["value_desc_type"] =
101       "TYPE_" + ToUpper(FieldDescriptor::TypeName(value->type()));
102   (*variables)["value_tag"] = SimpleItoa(internal::WireFormat::MakeTag(value));
103   (*variables)["type_parameters"] =
104       (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
105   (*variables)["value_default"] =
106       value->type() == FieldDescriptor::TYPE_MESSAGE
107           ? "new " + (*variables)["value_type"] + "()"
108           : "null";
109 }
110 }  // namespace
111 
112 // ===================================================================
MapFieldGenerator(const FieldDescriptor * descriptor,const Params & params)113 MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor,
114                                      const Params& params)
115     : FieldGenerator(params), descriptor_(descriptor) {
116   SetMapVariables(params, descriptor, &variables_);
117 }
118 
~MapFieldGenerator()119 MapFieldGenerator::~MapFieldGenerator() {}
120 
121 void MapFieldGenerator::
GenerateMembers(io::Printer * printer,bool) const122 GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
123   printer->Print(variables_,
124     "public java.util.Map<$type_parameters$> $name$;\n");
125 }
126 
127 void MapFieldGenerator::
GenerateClearCode(io::Printer * printer) const128 GenerateClearCode(io::Printer* printer) const {
129   printer->Print(variables_,
130     "$name$ = null;\n");
131 }
132 
133 void MapFieldGenerator::
GenerateMergingCode(io::Printer * printer) const134 GenerateMergingCode(io::Printer* printer) const {
135   printer->Print(variables_,
136     "this.$name$ = com.google.protobuf.nano.InternalNano.mergeMapEntry(\n"
137     "  input, this.$name$, mapFactory,\n"
138     "  com.google.protobuf.nano.InternalNano.$key_desc_type$,\n"
139     "  com.google.protobuf.nano.InternalNano.$value_desc_type$,\n"
140     "  $value_default$,\n"
141     "  $key_tag$, $value_tag$);\n"
142     "\n");
143 }
144 
145 void MapFieldGenerator::
GenerateSerializationCode(io::Printer * printer) const146 GenerateSerializationCode(io::Printer* printer) const {
147   printer->Print(variables_,
148     "if (this.$name$ != null) {\n"
149     "  com.google.protobuf.nano.InternalNano.serializeMapField(\n"
150     "    output, this.$name$, $number$,\n"
151     "  com.google.protobuf.nano.InternalNano.$key_desc_type$,\n"
152     "  com.google.protobuf.nano.InternalNano.$value_desc_type$);\n"
153     "}\n");
154 }
155 
156 void MapFieldGenerator::
GenerateSerializedSizeCode(io::Printer * printer) const157 GenerateSerializedSizeCode(io::Printer* printer) const {
158   printer->Print(variables_,
159     "if (this.$name$ != null) {\n"
160     "  size += com.google.protobuf.nano.InternalNano.computeMapFieldSize(\n"
161     "    this.$name$, $number$,\n"
162     "  com.google.protobuf.nano.InternalNano.$key_desc_type$,\n"
163     "  com.google.protobuf.nano.InternalNano.$value_desc_type$);\n"
164     "}\n");
165 }
166 
167 void MapFieldGenerator::
GenerateEqualsCode(io::Printer * printer) const168 GenerateEqualsCode(io::Printer* printer) const {
169   printer->Print(variables_,
170     "if (!com.google.protobuf.nano.InternalNano.equals(\n"
171     "  this.$name$, other.$name$)) {\n"
172     "  return false;\n"
173     "}\n");
174 }
175 
176 void MapFieldGenerator::
GenerateHashCodeCode(io::Printer * printer) const177 GenerateHashCodeCode(io::Printer* printer) const {
178   printer->Print(variables_,
179     "result = 31 * result +\n"
180     "    com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
181 }
182 
183 }  // namespace javanano
184 }  // namespace compiler
185 }  // namespace protobuf
186 }  // namespace google
187