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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.