OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 using internal::WireFormat; | 54 using internal::WireFormat; |
55 using internal::WireFormatLite; | 55 using internal::WireFormatLite; |
56 | 56 |
57 namespace { | 57 namespace { |
58 | 58 |
59 void SetPrimitiveVariables(const FieldDescriptor* descriptor, | 59 void SetPrimitiveVariables(const FieldDescriptor* descriptor, |
60 int messageBitIndex, | 60 int messageBitIndex, |
61 int builderBitIndex, | 61 int builderBitIndex, |
62 const FieldGeneratorInfo* info, | 62 const FieldGeneratorInfo* info, |
63 ClassNameResolver* name_resolver, | 63 ClassNameResolver* name_resolver, |
64 std::map<string, string>* variables) { | 64 map<string, string>* variables) { |
65 SetCommonFieldVariables(descriptor, info, variables); | 65 SetCommonFieldVariables(descriptor, info, variables); |
66 JavaType javaType = GetJavaType(descriptor); | 66 |
67 (*variables)["type"] = PrimitiveTypeName(javaType); | 67 (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor)); |
68 (*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType); | 68 (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor)); |
69 (*variables)["field_type"] = (*variables)["type"]; | 69 (*variables)["field_type"] = (*variables)["type"]; |
70 (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); | 70 (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); |
| 71 (*variables)["default_init"] = IsDefaultValueJavaDefault(descriptor) ? |
| 72 "" : ("= " + ImmutableDefaultValue(descriptor, name_resolver)); |
71 (*variables)["capitalized_type"] = | 73 (*variables)["capitalized_type"] = |
72 GetCapitalizedType(descriptor, /* immutable = */ true); | 74 GetCapitalizedType(descriptor, /* immutable = */ true); |
73 (*variables)["tag"] = | 75 (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor)); |
74 SimpleItoa(static_cast<int32>(WireFormat::MakeTag(descriptor))); | |
75 (*variables)["tag_size"] = SimpleItoa( | 76 (*variables)["tag_size"] = SimpleItoa( |
76 WireFormat::TagSize(descriptor->number(), GetType(descriptor))); | 77 WireFormat::TagSize(descriptor->number(), GetType(descriptor))); |
77 | 78 |
78 string capitalized_type = UnderscoresToCamelCase(PrimitiveTypeName(javaType), | 79 string capitalized_type = UnderscoresToCamelCase(PrimitiveTypeName( |
79 true /* cap_next_letter */); | 80 GetJavaType(descriptor)), true /* cap_next_letter */); |
80 switch (javaType) { | 81 switch (GetJavaType(descriptor)) { |
81 case JAVATYPE_INT: | 82 case JAVATYPE_INT: |
82 case JAVATYPE_LONG: | 83 case JAVATYPE_LONG: |
83 case JAVATYPE_FLOAT: | 84 case JAVATYPE_FLOAT: |
84 case JAVATYPE_DOUBLE: | 85 case JAVATYPE_DOUBLE: |
85 case JAVATYPE_BOOLEAN: | 86 case JAVATYPE_BOOLEAN: |
86 (*variables)["field_list_type"] = | 87 (*variables)["field_list_type"] = |
87 "com.google.protobuf.Internal." + capitalized_type + "List"; | 88 "com.google.protobuf.Internal." + capitalized_type + "List"; |
88 (*variables)["empty_list"] = "empty" + capitalized_type + "List()"; | 89 (*variables)["empty_list"] = "empty" + capitalized_type + "List()"; |
89 (*variables)["make_name_unmodifiable"] = | 90 (*variables)["make_name_unmodifiable"] = |
90 (*variables)["name"] + "_.makeImmutable()"; | 91 (*variables)["name"] + "_.makeImmutable()"; |
(...skipping 13 matching lines...) Expand all Loading... |
104 (*variables)["empty_list"] = "emptyProtobufList()"; | 105 (*variables)["empty_list"] = "emptyProtobufList()"; |
105 (*variables)["make_name_unmodifiable"] = | 106 (*variables)["make_name_unmodifiable"] = |
106 (*variables)["name"] + "_.makeImmutable()"; | 107 (*variables)["name"] + "_.makeImmutable()"; |
107 (*variables)["repeated_get"] = (*variables)["name"] + "_.get"; | 108 (*variables)["repeated_get"] = (*variables)["name"] + "_.get"; |
108 (*variables)["repeated_add"] = (*variables)["name"] + "_.add"; | 109 (*variables)["repeated_add"] = (*variables)["name"] + "_.add"; |
109 (*variables)["repeated_set"] = (*variables)["name"] + "_.set"; | 110 (*variables)["repeated_set"] = (*variables)["name"] + "_.set"; |
110 (*variables)["visit_type"] = "ByteString"; | 111 (*variables)["visit_type"] = "ByteString"; |
111 (*variables)["visit_type_list"] = "visitList"; | 112 (*variables)["visit_type_list"] = "visitList"; |
112 } | 113 } |
113 | 114 |
114 if (javaType == JAVATYPE_BYTES) { | 115 if (IsReferenceType(GetJavaType(descriptor))) { |
115 (*variables)["bytes_default"] = | |
116 ToUpper((*variables)["name"]) + "_DEFAULT_VALUE"; | |
117 } | |
118 | |
119 if (IsReferenceType(javaType)) { | |
120 (*variables)["null_check"] = | 116 (*variables)["null_check"] = |
121 " if (value == null) {\n" | 117 " if (value == null) {\n" |
122 " throw new NullPointerException();\n" | 118 " throw new NullPointerException();\n" |
123 " }\n"; | 119 " }\n"; |
124 } else { | 120 } else { |
125 (*variables)["null_check"] = ""; | 121 (*variables)["null_check"] = ""; |
126 } | 122 } |
127 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported | 123 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported |
128 // by the proto compiler | 124 // by the proto compiler |
129 (*variables)["deprecation"] = descriptor->options().deprecated() | 125 (*variables)["deprecation"] = descriptor->options().deprecated() |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 printer->Print(variables_, | 197 printer->Print(variables_, |
202 "$deprecation$boolean has$capitalized_name$();\n"); | 198 "$deprecation$boolean has$capitalized_name$();\n"); |
203 } | 199 } |
204 WriteFieldDocComment(printer, descriptor_); | 200 WriteFieldDocComment(printer, descriptor_); |
205 printer->Print(variables_, | 201 printer->Print(variables_, |
206 "$deprecation$$type$ get$capitalized_name$();\n"); | 202 "$deprecation$$type$ get$capitalized_name$();\n"); |
207 } | 203 } |
208 | 204 |
209 void ImmutablePrimitiveFieldLiteGenerator:: | 205 void ImmutablePrimitiveFieldLiteGenerator:: |
210 GenerateMembers(io::Printer* printer) const { | 206 GenerateMembers(io::Printer* printer) const { |
211 if (IsByteStringWithCustomDefaultValue(descriptor_)) { | |
212 // allocate this once statically since we know ByteStrings are immutable | |
213 // values that can be reused. | |
214 printer->Print( | |
215 variables_, | |
216 "private static final $field_type$ $bytes_default$ = $default$;\n"); | |
217 } | |
218 printer->Print(variables_, | 207 printer->Print(variables_, |
219 "private $field_type$ $name$_;\n"); | 208 "private $field_type$ $name$_;\n"); |
220 PrintExtraFieldInfo(variables_, printer); | 209 PrintExtraFieldInfo(variables_, printer); |
221 if (SupportFieldPresence(descriptor_->file())) { | 210 if (SupportFieldPresence(descriptor_->file())) { |
222 WriteFieldDocComment(printer, descriptor_); | 211 WriteFieldDocComment(printer, descriptor_); |
223 printer->Print(variables_, | 212 printer->Print(variables_, |
224 "$deprecation$public boolean has$capitalized_name$() {\n" | 213 "$deprecation$public boolean has$capitalized_name$() {\n" |
225 " return $get_has_field_bit_message$;\n" | 214 " return $get_has_field_bit_message$;\n" |
226 "}\n"); | 215 "}\n"); |
227 } | 216 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 "}\n"); | 280 "}\n"); |
292 } | 281 } |
293 | 282 |
294 void ImmutablePrimitiveFieldLiteGenerator:: | 283 void ImmutablePrimitiveFieldLiteGenerator:: |
295 GenerateFieldBuilderInitializationCode(io::Printer* printer) const { | 284 GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
296 // noop for primitives | 285 // noop for primitives |
297 } | 286 } |
298 | 287 |
299 void ImmutablePrimitiveFieldLiteGenerator:: | 288 void ImmutablePrimitiveFieldLiteGenerator:: |
300 GenerateInitializationCode(io::Printer* printer) const { | 289 GenerateInitializationCode(io::Printer* printer) const { |
301 if (IsByteStringWithCustomDefaultValue(descriptor_)) { | 290 printer->Print(variables_, "$name$_ = $default$;\n"); |
302 printer->Print(variables_, "$name$_ = $bytes_default$;\n"); | |
303 } else if (!IsDefaultValueJavaDefault(descriptor_)) { | |
304 printer->Print(variables_, "$name$_ = $default$;\n"); | |
305 } | |
306 } | 291 } |
307 | 292 |
308 void ImmutablePrimitiveFieldLiteGenerator:: | 293 void ImmutablePrimitiveFieldLiteGenerator:: |
309 GenerateBuilderClearCode(io::Printer* printer) const { | 294 GenerateBuilderClearCode(io::Printer* printer) const { |
310 // noop for primitives | 295 // noop for primitives |
311 } | 296 } |
312 | 297 |
313 void ImmutablePrimitiveFieldLiteGenerator:: | 298 void ImmutablePrimitiveFieldLiteGenerator:: |
314 GenerateVisitCode(io::Printer* printer) const { | 299 GenerateVisitCode(io::Printer* printer) const { |
315 if (SupportFieldPresence(descriptor_->file())) { | 300 if (SupportFieldPresence(descriptor_->file())) { |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 } | 810 } |
826 | 811 |
827 void RepeatedImmutablePrimitiveFieldLiteGenerator:: | 812 void RepeatedImmutablePrimitiveFieldLiteGenerator:: |
828 GenerateSerializationCode(io::Printer* printer) const { | 813 GenerateSerializationCode(io::Printer* printer) const { |
829 if (descriptor_->options().packed()) { | 814 if (descriptor_->options().packed()) { |
830 // We invoke getSerializedSize in writeTo for messages that have packed | 815 // We invoke getSerializedSize in writeTo for messages that have packed |
831 // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. | 816 // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. |
832 // That makes it safe to rely on the memoized size here. | 817 // That makes it safe to rely on the memoized size here. |
833 printer->Print(variables_, | 818 printer->Print(variables_, |
834 "if (get$capitalized_name$List().size() > 0) {\n" | 819 "if (get$capitalized_name$List().size() > 0) {\n" |
835 " output.writeUInt32NoTag($tag$);\n" | 820 " output.writeRawVarint32($tag$);\n" |
836 " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" | 821 " output.writeRawVarint32($name$MemoizedSerializedSize);\n" |
837 "}\n" | 822 "}\n" |
838 "for (int i = 0; i < $name$_.size(); i++) {\n" | 823 "for (int i = 0; i < $name$_.size(); i++) {\n" |
839 " output.write$capitalized_type$NoTag($repeated_get$(i));\n" | 824 " output.write$capitalized_type$NoTag($repeated_get$(i));\n" |
840 "}\n"); | 825 "}\n"); |
841 } else { | 826 } else { |
842 printer->Print(variables_, | 827 printer->Print(variables_, |
843 "for (int i = 0; i < $name$_.size(); i++) {\n" | 828 "for (int i = 0; i < $name$_.size(); i++) {\n" |
844 " output.write$capitalized_type$($number$, $repeated_get$(i));\n" | 829 " output.write$capitalized_type$($number$, $repeated_get$(i));\n" |
845 "}\n"); | 830 "}\n"); |
846 } | 831 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
906 } | 891 } |
907 | 892 |
908 string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { | 893 string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { |
909 return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); | 894 return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
910 } | 895 } |
911 | 896 |
912 } // namespace java | 897 } // namespace java |
913 } // namespace compiler | 898 } // namespace compiler |
914 } // namespace protobuf | 899 } // namespace protobuf |
915 } // namespace google | 900 } // namespace google |
OLD | NEW |