I'd be open to a class like: class DescriptorSetBuilder { // Adds the file and all its dependencies, if they have not been added already. // Returns this. public DescriptorSetBuilder add(FileDescriptor file);
// Returns a FileDescriptorSet containing all files added with add() and // their dependencies. Files will be in topological order -- each file will // appear before any files that import it. public FileDescriptorSet build(); } This way it's easy to build a set containing multiple independent files. The next version of protoc will also output FileDescriptorSets in topological order, as a side effect of the plugins change I'm working on. On Sun, Nov 29, 2009 at 12:57 AM, Christopher Smith <cbsm...@gmail.com>wrote: > Given that there is no Java version of protoc, it has been tempting to see > how far one can get with just using FileDescriptorSet's and dynamic > messages. I ended up building this short function for iteratively adding all > the dependencies of a particular Message in to a FileDescriptorSet. While it > wasn't too much pain, it was a tad annoying, and I am wondering why this > kind of functionality isn't built in to the Java API. Here's what I ended up > with: > > /** > * Get the entire descriptor set needed to describe a message in > * dependency order. > * > * @param aMessage the message to inspect > * @return a complete descriptor set containing all the elements needed > to parse this message > */ > > static FileDescriptorSet getDescriptorSetFor(final Message aMessage) { > final FileDescriptor file = > aMessage.getDescriptorForType().getFile(); > final List<FileDescriptor> fileDescriptors = new > ArrayList<FileDescriptor>(); > fileDescriptors.add(file); > > //Don't use an iter as we intend to keep looping until the list > //stops growing > for (int i = 0; i < fileDescriptors.size(); ++i) { > final FileDescriptor nextElement = fileDescriptors.get(i); > for (FileDescriptor dependency : nextElement.getDependencies()) > { > if (!fileDescriptors.contains(dependency)) { > fileDescriptors.add(dependency); > } > } > } > > final FileDescriptorSet.Builder builder = > FileDescriptorSet.newBuilder(); > for (FileDescriptor descriptor : fileDescriptors) { > builder.addFile(descriptor.toProto()); > } > return builder.build(); > } > > Is this really the right way to handle this, and if so, any chance we can > get this built in to the protobuf library? > > -- > Chris > > -- > 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.