| 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 map<string, string>* variables) { | 64 std::map<string, string>* variables) { |
| 65 SetCommonFieldVariables(descriptor, info, variables); | 65 SetCommonFieldVariables(descriptor, info, variables); |
| 66 | 66 JavaType javaType = GetJavaType(descriptor); |
| 67 (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor)); | 67 (*variables)["type"] = PrimitiveTypeName(javaType); |
| 68 (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor)); | 68 (*variables)["boxed_type"] = BoxedPrimitiveTypeName(javaType); |
| 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)); | |
| 73 (*variables)["capitalized_type"] = | 71 (*variables)["capitalized_type"] = |
| 74 GetCapitalizedType(descriptor, /* immutable = */ true); | 72 GetCapitalizedType(descriptor, /* immutable = */ true); |
| 75 (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor)); | 73 (*variables)["tag"] = |
| 74 SimpleItoa(static_cast<int32>(WireFormat::MakeTag(descriptor))); |
| 76 (*variables)["tag_size"] = SimpleItoa( | 75 (*variables)["tag_size"] = SimpleItoa( |
| 77 WireFormat::TagSize(descriptor->number(), GetType(descriptor))); | 76 WireFormat::TagSize(descriptor->number(), GetType(descriptor))); |
| 78 | 77 |
| 79 string capitalized_type = UnderscoresToCamelCase(PrimitiveTypeName( | 78 string capitalized_type = UnderscoresToCamelCase(PrimitiveTypeName(javaType), |
| 80 GetJavaType(descriptor)), true /* cap_next_letter */); | 79 true /* cap_next_letter */); |
| 81 switch (GetJavaType(descriptor)) { | 80 switch (javaType) { |
| 82 case JAVATYPE_INT: | 81 case JAVATYPE_INT: |
| 83 case JAVATYPE_LONG: | 82 case JAVATYPE_LONG: |
| 84 case JAVATYPE_FLOAT: | 83 case JAVATYPE_FLOAT: |
| 85 case JAVATYPE_DOUBLE: | 84 case JAVATYPE_DOUBLE: |
| 86 case JAVATYPE_BOOLEAN: | 85 case JAVATYPE_BOOLEAN: |
| 87 (*variables)["field_list_type"] = | 86 (*variables)["field_list_type"] = |
| 88 "com.google.protobuf.Internal." + capitalized_type + "List"; | 87 "com.google.protobuf.Internal." + capitalized_type + "List"; |
| 89 (*variables)["empty_list"] = "empty" + capitalized_type + "List()"; | 88 (*variables)["empty_list"] = "empty" + capitalized_type + "List()"; |
| 90 (*variables)["make_name_unmodifiable"] = | 89 (*variables)["make_name_unmodifiable"] = |
| 91 (*variables)["name"] + "_.makeImmutable()"; | 90 (*variables)["name"] + "_.makeImmutable()"; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 105 (*variables)["empty_list"] = "emptyProtobufList()"; | 104 (*variables)["empty_list"] = "emptyProtobufList()"; |
| 106 (*variables)["make_name_unmodifiable"] = | 105 (*variables)["make_name_unmodifiable"] = |
| 107 (*variables)["name"] + "_.makeImmutable()"; | 106 (*variables)["name"] + "_.makeImmutable()"; |
| 108 (*variables)["repeated_get"] = (*variables)["name"] + "_.get"; | 107 (*variables)["repeated_get"] = (*variables)["name"] + "_.get"; |
| 109 (*variables)["repeated_add"] = (*variables)["name"] + "_.add"; | 108 (*variables)["repeated_add"] = (*variables)["name"] + "_.add"; |
| 110 (*variables)["repeated_set"] = (*variables)["name"] + "_.set"; | 109 (*variables)["repeated_set"] = (*variables)["name"] + "_.set"; |
| 111 (*variables)["visit_type"] = "ByteString"; | 110 (*variables)["visit_type"] = "ByteString"; |
| 112 (*variables)["visit_type_list"] = "visitList"; | 111 (*variables)["visit_type_list"] = "visitList"; |
| 113 } | 112 } |
| 114 | 113 |
| 115 if (IsReferenceType(GetJavaType(descriptor))) { | 114 if (javaType == JAVATYPE_BYTES) { |
| 115 (*variables)["bytes_default"] = |
| 116 ToUpper((*variables)["name"]) + "_DEFAULT_VALUE"; |
| 117 } |
| 118 |
| 119 if (IsReferenceType(javaType)) { |
| 116 (*variables)["null_check"] = | 120 (*variables)["null_check"] = |
| 117 " if (value == null) {\n" | 121 " if (value == null) {\n" |
| 118 " throw new NullPointerException();\n" | 122 " throw new NullPointerException();\n" |
| 119 " }\n"; | 123 " }\n"; |
| 120 } else { | 124 } else { |
| 121 (*variables)["null_check"] = ""; | 125 (*variables)["null_check"] = ""; |
| 122 } | 126 } |
| 123 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported | 127 // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported |
| 124 // by the proto compiler | 128 // by the proto compiler |
| 125 (*variables)["deprecation"] = descriptor->options().deprecated() | 129 (*variables)["deprecation"] = descriptor->options().deprecated() |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 printer->Print(variables_, | 201 printer->Print(variables_, |
| 198 "$deprecation$boolean has$capitalized_name$();\n"); | 202 "$deprecation$boolean has$capitalized_name$();\n"); |
| 199 } | 203 } |
| 200 WriteFieldDocComment(printer, descriptor_); | 204 WriteFieldDocComment(printer, descriptor_); |
| 201 printer->Print(variables_, | 205 printer->Print(variables_, |
| 202 "$deprecation$$type$ get$capitalized_name$();\n"); | 206 "$deprecation$$type$ get$capitalized_name$();\n"); |
| 203 } | 207 } |
| 204 | 208 |
| 205 void ImmutablePrimitiveFieldLiteGenerator:: | 209 void ImmutablePrimitiveFieldLiteGenerator:: |
| 206 GenerateMembers(io::Printer* printer) const { | 210 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 } |
| 207 printer->Print(variables_, | 218 printer->Print(variables_, |
| 208 "private $field_type$ $name$_;\n"); | 219 "private $field_type$ $name$_;\n"); |
| 209 PrintExtraFieldInfo(variables_, printer); | 220 PrintExtraFieldInfo(variables_, printer); |
| 210 if (SupportFieldPresence(descriptor_->file())) { | 221 if (SupportFieldPresence(descriptor_->file())) { |
| 211 WriteFieldDocComment(printer, descriptor_); | 222 WriteFieldDocComment(printer, descriptor_); |
| 212 printer->Print(variables_, | 223 printer->Print(variables_, |
| 213 "$deprecation$public boolean has$capitalized_name$() {\n" | 224 "$deprecation$public boolean has$capitalized_name$() {\n" |
| 214 " return $get_has_field_bit_message$;\n" | 225 " return $get_has_field_bit_message$;\n" |
| 215 "}\n"); | 226 "}\n"); |
| 216 } | 227 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 "}\n"); | 291 "}\n"); |
| 281 } | 292 } |
| 282 | 293 |
| 283 void ImmutablePrimitiveFieldLiteGenerator:: | 294 void ImmutablePrimitiveFieldLiteGenerator:: |
| 284 GenerateFieldBuilderInitializationCode(io::Printer* printer) const { | 295 GenerateFieldBuilderInitializationCode(io::Printer* printer) const { |
| 285 // noop for primitives | 296 // noop for primitives |
| 286 } | 297 } |
| 287 | 298 |
| 288 void ImmutablePrimitiveFieldLiteGenerator:: | 299 void ImmutablePrimitiveFieldLiteGenerator:: |
| 289 GenerateInitializationCode(io::Printer* printer) const { | 300 GenerateInitializationCode(io::Printer* printer) const { |
| 290 printer->Print(variables_, "$name$_ = $default$;\n"); | 301 if (IsByteStringWithCustomDefaultValue(descriptor_)) { |
| 302 printer->Print(variables_, "$name$_ = $bytes_default$;\n"); |
| 303 } else if (!IsDefaultValueJavaDefault(descriptor_)) { |
| 304 printer->Print(variables_, "$name$_ = $default$;\n"); |
| 305 } |
| 291 } | 306 } |
| 292 | 307 |
| 293 void ImmutablePrimitiveFieldLiteGenerator:: | 308 void ImmutablePrimitiveFieldLiteGenerator:: |
| 294 GenerateBuilderClearCode(io::Printer* printer) const { | 309 GenerateBuilderClearCode(io::Printer* printer) const { |
| 295 // noop for primitives | 310 // noop for primitives |
| 296 } | 311 } |
| 297 | 312 |
| 298 void ImmutablePrimitiveFieldLiteGenerator:: | 313 void ImmutablePrimitiveFieldLiteGenerator:: |
| 299 GenerateVisitCode(io::Printer* printer) const { | 314 GenerateVisitCode(io::Printer* printer) const { |
| 300 if (SupportFieldPresence(descriptor_->file())) { | 315 if (SupportFieldPresence(descriptor_->file())) { |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 } | 825 } |
| 811 | 826 |
| 812 void RepeatedImmutablePrimitiveFieldLiteGenerator:: | 827 void RepeatedImmutablePrimitiveFieldLiteGenerator:: |
| 813 GenerateSerializationCode(io::Printer* printer) const { | 828 GenerateSerializationCode(io::Printer* printer) const { |
| 814 if (descriptor_->options().packed()) { | 829 if (descriptor_->options().packed()) { |
| 815 // We invoke getSerializedSize in writeTo for messages that have packed | 830 // We invoke getSerializedSize in writeTo for messages that have packed |
| 816 // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. | 831 // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. |
| 817 // That makes it safe to rely on the memoized size here. | 832 // That makes it safe to rely on the memoized size here. |
| 818 printer->Print(variables_, | 833 printer->Print(variables_, |
| 819 "if (get$capitalized_name$List().size() > 0) {\n" | 834 "if (get$capitalized_name$List().size() > 0) {\n" |
| 820 " output.writeRawVarint32($tag$);\n" | 835 " output.writeUInt32NoTag($tag$);\n" |
| 821 " output.writeRawVarint32($name$MemoizedSerializedSize);\n" | 836 " output.writeUInt32NoTag($name$MemoizedSerializedSize);\n" |
| 822 "}\n" | 837 "}\n" |
| 823 "for (int i = 0; i < $name$_.size(); i++) {\n" | 838 "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 824 " output.write$capitalized_type$NoTag($repeated_get$(i));\n" | 839 " output.write$capitalized_type$NoTag($repeated_get$(i));\n" |
| 825 "}\n"); | 840 "}\n"); |
| 826 } else { | 841 } else { |
| 827 printer->Print(variables_, | 842 printer->Print(variables_, |
| 828 "for (int i = 0; i < $name$_.size(); i++) {\n" | 843 "for (int i = 0; i < $name$_.size(); i++) {\n" |
| 829 " output.write$capitalized_type$($number$, $repeated_get$(i));\n" | 844 " output.write$capitalized_type$($number$, $repeated_get$(i));\n" |
| 830 "}\n"); | 845 "}\n"); |
| 831 } | 846 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 } | 906 } |
| 892 | 907 |
| 893 string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { | 908 string RepeatedImmutablePrimitiveFieldLiteGenerator::GetBoxedType() const { |
| 894 return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); | 909 return BoxedPrimitiveTypeName(GetJavaType(descriptor_)); |
| 895 } | 910 } |
| 896 | 911 |
| 897 } // namespace java | 912 } // namespace java |
| 898 } // namespace compiler | 913 } // namespace compiler |
| 899 } // namespace protobuf | 914 } // namespace protobuf |
| 900 } // namespace google | 915 } // namespace google |
| OLD | NEW |