diff options
author | Max Cai <maxtroy@google.com> | 2013-11-20 18:59:01 +0000 |
---|---|---|
committer | Max Cai <maxtroy@google.com> | 2014-01-10 11:50:32 +0000 |
commit | 382ddccb550e1c822ef26a0e65988998f7446624 (patch) | |
tree | 5159c510e6a895f4ce56310b8f23b7bb4aafc2f0 /src/google/protobuf/compiler/javanano/javanano_extension.cc | |
parent | e7741c064ee4cdc5fa41e6444ed45131672fed97 (diff) | |
download | external_protobuf-382ddccb550e1c822ef26a0e65988998f7446624.zip external_protobuf-382ddccb550e1c822ef26a0e65988998f7446624.tar.gz external_protobuf-382ddccb550e1c822ef26a0e65988998f7446624.tar.bz2 |
Extension overhaul.
- Get rid of TypeLiteral<T>. It was introduced to read the component
type of a List<T> at runtime. But we use arrays everywhere else,
and we can always read the component type of an array type at
runtime.
- Properly read/write "minor" types (e.g. sint32, sfixed32). The old
implementation could only read/write data as the "typical" types
(one per Java type), e.g. java.lang.Integer -> int32, java.lang.Long
-> int64. So if e.g. an extension specifies sfixed32 as the type, it
would be read/written in the totally incompatible int32 format.
- Properly serialize repeated packed fields. The old implementation
doesn't do packed serialization. As an added bonus, and to be more
aligned with the rest of protobuf nano / main, repeated packable
extensions can deserialize both packed and non-packed data.
- Split Extension class into a hierarchy so under typical usage a
large chunk of code dealing with primitive type extensions can be
removed by ProGuard.
Bug: https://code.google.com/p/android/issues/detail?id=62586
Change-Id: I0d692f35cc2a8ad3a5a1cb3ce001282b2356b041
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_extension.cc')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_extension.cc | 113 |
1 files changed, 82 insertions, 31 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_extension.cc b/src/google/protobuf/compiler/javanano/javanano_extension.cc index 0bc9c9d..754ed55 100644 --- a/src/google/protobuf/compiler/javanano/javanano_extension.cc +++ b/src/google/protobuf/compiler/javanano/javanano_extension.cc @@ -42,28 +42,84 @@ namespace compiler { namespace javanano { using internal::WireFormat; +using internal::WireFormatLite; + +namespace { + +const char* GetTypeConstantName(const FieldDescriptor::Type type) { + switch (type) { + case FieldDescriptor::TYPE_INT32 : return "TYPE_INT32" ; + case FieldDescriptor::TYPE_UINT32 : return "TYPE_UINT32" ; + case FieldDescriptor::TYPE_SINT32 : return "TYPE_SINT32" ; + case FieldDescriptor::TYPE_FIXED32 : return "TYPE_FIXED32" ; + case FieldDescriptor::TYPE_SFIXED32: return "TYPE_SFIXED32"; + case FieldDescriptor::TYPE_INT64 : return "TYPE_INT64" ; + case FieldDescriptor::TYPE_UINT64 : return "TYPE_UINT64" ; + case FieldDescriptor::TYPE_SINT64 : return "TYPE_SINT64" ; + case FieldDescriptor::TYPE_FIXED64 : return "TYPE_FIXED64" ; + case FieldDescriptor::TYPE_SFIXED64: return "TYPE_SFIXED64"; + case FieldDescriptor::TYPE_FLOAT : return "TYPE_FLOAT" ; + case FieldDescriptor::TYPE_DOUBLE : return "TYPE_DOUBLE" ; + case FieldDescriptor::TYPE_BOOL : return "TYPE_BOOL" ; + case FieldDescriptor::TYPE_STRING : return "TYPE_STRING" ; + case FieldDescriptor::TYPE_BYTES : return "TYPE_BYTES" ; + case FieldDescriptor::TYPE_ENUM : return "TYPE_ENUM" ; + case FieldDescriptor::TYPE_GROUP : return "TYPE_GROUP" ; + case FieldDescriptor::TYPE_MESSAGE : return "TYPE_MESSAGE" ; + + // No default because we want the compiler to complain if any new + // types are added. + } + + GOOGLE_LOG(FATAL) << "Can't get here."; + return NULL; +} + +} // namespace void SetVariables(const FieldDescriptor* descriptor, const Params params, map<string, string>* variables) { - (*variables)["name"] = - RenameJavaKeywords(UnderscoresToCamelCase(descriptor)); - (*variables)["number"] = SimpleItoa(descriptor->number()); (*variables)["extends"] = ClassName(params, descriptor->containing_type()); - - string type; + (*variables)["name"] = RenameJavaKeywords(UnderscoresToCamelCase(descriptor)); + bool repeated = descriptor->is_repeated(); + (*variables)["repeated"] = repeated ? "Repeated" : ""; + (*variables)["type"] = GetTypeConstantName(descriptor->type()); JavaType java_type = GetJavaType(descriptor->type()); - switch (java_type) { - case JAVATYPE_ENUM: - type = "java.lang.Integer"; - break; - case JAVATYPE_MESSAGE: - type = ClassName(params, descriptor->message_type()); - break; - default: - type = BoxedPrimitiveTypeName(java_type); - break; + string tag = SimpleItoa(WireFormat::MakeTag(descriptor)); + if (java_type == JAVATYPE_MESSAGE) { + (*variables)["ext_type"] = "MessageTyped"; + string message_type = ClassName(params, descriptor->message_type()); + if (repeated) { + message_type += "[]"; + } + (*variables)["class"] = message_type; + // For message typed extensions, tags_params contains a single tag + // for both singular and repeated cases. + (*variables)["tag_params"] = tag; + } else { + (*variables)["ext_type"] = "PrimitiveTyped"; + if (!repeated) { + (*variables)["class"] = BoxedPrimitiveTypeName(java_type); + (*variables)["tag_params"] = tag; + } else { + (*variables)["class"] = PrimitiveTypeName(java_type) + "[]"; + if (!descriptor->is_packable()) { + // Non-packable: nonPackedTag == tag, packedTag == 0 + (*variables)["tag_params"] = tag + ", " + tag + ", 0"; + } else if (descriptor->options().packed()) { + // Packable and packed: tag == packedTag + string non_packed_tag = SimpleItoa(WireFormatLite::MakeTag( + descriptor->number(), + WireFormat::WireTypeForFieldType(descriptor->type()))); + (*variables)["tag_params"] = tag + ", " + non_packed_tag + ", " + tag; + } else { + // Packable and not packed: tag == nonPackedTag + string packed_tag = SimpleItoa(WireFormatLite::MakeTag( + descriptor->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED)); + (*variables)["tag_params"] = tag + ", " + tag + ", " + packed_tag; + } + } } - (*variables)["type"] = type; } ExtensionGenerator:: @@ -75,21 +131,16 @@ ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params) ExtensionGenerator::~ExtensionGenerator() {} void ExtensionGenerator::Generate(io::Printer* printer) const { - if (descriptor_->is_repeated()) { - printer->Print(variables_, - "\n" - "// extends $extends$\n" - "public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n" - " com.google.protobuf.nano.Extension.createRepeated($number$,\n" - " new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n"); - } else { - printer->Print(variables_, - "\n" - "// extends $extends$\n" - "public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n" - " com.google.protobuf.nano.Extension.create($number$,\n" - " new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n"); - } + printer->Print("\n"); + PrintFieldComment(printer, descriptor_); + printer->Print(variables_, + "public static final com.google.protobuf.nano.Extension<\n" + " $extends$,\n" + " $class$> $name$ =\n" + " com.google.protobuf.nano.Extension.create$repeated$$ext_type$(\n" + " com.google.protobuf.nano.Extension.$type$,\n" + " $class$.class,\n" + " $tag_params$);\n"); } } // namespace javanano |