Line data Source code
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 :
45 0 : string TypeName(const Params& params, const FieldDescriptor* field,
46 : bool boxed) {
47 0 : JavaType java_type = GetJavaType(field);
48 0 : switch (java_type) {
49 : case JAVATYPE_MESSAGE:
50 0 : 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 0 : if (boxed) {
60 0 : return BoxedPrimitiveTypeName(java_type);
61 : } else {
62 0 : 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 0 : GOOGLE_LOG(FATAL) << "should not reach here.";
69 0 : return "";
70 : }
71 :
72 0 : const FieldDescriptor* KeyField(const FieldDescriptor* descriptor) {
73 0 : GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
74 0 : const Descriptor* message = descriptor->message_type();
75 0 : GOOGLE_CHECK(message->options().map_entry());
76 0 : return message->FindFieldByName("key");
77 : }
78 :
79 0 : const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) {
80 0 : GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
81 0 : const Descriptor* message = descriptor->message_type();
82 0 : GOOGLE_CHECK(message->options().map_entry());
83 0 : return message->FindFieldByName("value");
84 : }
85 :
86 0 : void SetMapVariables(const Params& params,
87 0 : const FieldDescriptor* descriptor, map<string, string>* variables) {
88 0 : const FieldDescriptor* key = KeyField(descriptor);
89 0 : const FieldDescriptor* value = ValueField(descriptor);
90 0 : (*variables)["name"] =
91 : RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
92 0 : (*variables)["number"] = SimpleItoa(descriptor->number());
93 0 : (*variables)["key_type"] = TypeName(params, key, false);
94 0 : (*variables)["boxed_key_type"] = TypeName(params,key, true);
95 0 : (*variables)["key_desc_type"] =
96 0 : "TYPE_" + ToUpper(FieldDescriptor::TypeName(key->type()));
97 0 : (*variables)["key_tag"] = SimpleItoa(internal::WireFormat::MakeTag(key));
98 0 : (*variables)["value_type"] = TypeName(params, value, false);
99 0 : (*variables)["boxed_value_type"] = TypeName(params, value, true);
100 0 : (*variables)["value_desc_type"] =
101 0 : "TYPE_" + ToUpper(FieldDescriptor::TypeName(value->type()));
102 0 : (*variables)["value_tag"] = SimpleItoa(internal::WireFormat::MakeTag(value));
103 0 : (*variables)["type_parameters"] =
104 0 : (*variables)["boxed_key_type"] + ", " + (*variables)["boxed_value_type"];
105 0 : (*variables)["value_default"] =
106 0 : value->type() == FieldDescriptor::TYPE_MESSAGE
107 0 : ? "new " + (*variables)["value_type"] + "()"
108 0 : : "null";
109 0 : }
110 : } // namespace
111 :
112 : // ===================================================================
113 0 : MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor,
114 : const Params& params)
115 0 : : FieldGenerator(params), descriptor_(descriptor) {
116 0 : SetMapVariables(params, descriptor, &variables_);
117 0 : }
118 :
119 0 : MapFieldGenerator::~MapFieldGenerator() {}
120 :
121 0 : void MapFieldGenerator::
122 : GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
123 : printer->Print(variables_,
124 0 : "public java.util.Map<$type_parameters$> $name$;\n");
125 0 : }
126 :
127 0 : void MapFieldGenerator::
128 : GenerateClearCode(io::Printer* printer) const {
129 : printer->Print(variables_,
130 0 : "$name$ = null;\n");
131 0 : }
132 :
133 0 : void MapFieldGenerator::
134 : 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 0 : "\n");
143 0 : }
144 :
145 0 : void MapFieldGenerator::
146 : 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 0 : "}\n");
154 0 : }
155 :
156 0 : void MapFieldGenerator::
157 : 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 0 : "}\n");
165 0 : }
166 :
167 0 : void MapFieldGenerator::
168 : 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 0 : "}\n");
174 0 : }
175 :
176 0 : void MapFieldGenerator::
177 : GenerateHashCodeCode(io::Printer* printer) const {
178 : printer->Print(variables_,
179 : "result = 31 * result +\n"
180 0 : " com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
181 0 : }
182 :
183 : } // namespace javanano
184 : } // namespace compiler
185 : } // namespace protobuf
186 : } // namespace google
|