DescriptorProto.getDescriptorForType() returns the Descriptor for DescriptorProto, not for the type which that DescriptorProto is describing. Remember that DescriptorProto is just a protocol message like any other -- it does not have any special methods that recognize its higher-level meaning.
To convert DescriptorProtos to Descriptors, you need to use FileDescriptor.buildFrom(). On Sun, Mar 21, 2010 at 5:20 PM, Kevin Tambascio <kevin.tambas...@gmail.com>wrote: > Hi, > > I'm having trouble getting the following code to work. Using > protoc.exe, I generated a file with the descriptor data using the -- > descriptor_set_out file. I've written some Java code to read the > file, and try to instantiate a default instance of one of the objects > in the descriptor, so that I write it out to an XML file using the > protobuf-format-java library. > > Here's my code. The variable "descriptorData" contains the binary > content of the descriptor file, without any modifications: > > DescriptorProtos.FileDescriptorSet fdSet = > FileDescriptorSet.newBuilder().mergeFrom(descriptorData).build(); > FileDescriptorProto fdp = fdSet.getFile(0); > > List<DescriptorProto> messageTypes = > fdp.getMessageTypeList(); > for(DescriptorProto type : messageTypes) > { > System.out.println("Type is: " + type.getName()); > FileDescriptor fd = > type.getDescriptorForType().getFile(); > > DynamicMessage dm = > DynamicMessage.getDefaultInstance(type.getDescriptorForType()); > System.out.println(XmlFormat.printToString(dm)); > } > > I've tried numerous combinations of the above code, but each time I > get the following output: > > Type is: Type1 > <DescriptorProto></DescriptorProto> > Type is: Type2 > <DescriptorProto></DescriptorProto> > Type is: Type3 > <DescriptorProto></DescriptorProto> > Type is: Type4 > <DescriptorProto></DescriptorProto> > Type is: Type5 > <DescriptorProto></DescriptorProto> > Type is: Type6 > <DescriptorProto></DescriptorProto> > > The proto file has Type1, Type2, Type3, etc, defined as messages. The > fact that type.getName() does return the type names from my proto > file, leads me to believe I'm heading in the right direction. > However, the DynamicMessage type that is created (and serialized to > XML) seems to indicate that I'm not passing the right descriptor > instance in to create the object. > > Any thoughts? > > Thanks, > Kevin > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.