On Oct 31, 2009, at 11:57 , Martin Gilday wrote:
> I would like have a generic method which takes a Descriptor and a
> stream which gives back an instance of message which I can later cast
> to my desired type.  I have tried DynamicMessage from =
> DynamicMessage.parseFrom(descriptor, inputStream); but I get an
> exception when I attempt to cast this as my generate proto class.
> Is there a solution to this?

Here are a few alternatives, depending on what you want to do:

a) You want to write Java code which takes multiple types of protocol  
buffer messages, examines them, and does something with them.

In this case, you need to create your specific kind of message  
(MyFooMessage.Builder.build()), then pass it in to the "generic"  
method as a Message. Then you can use getDescriptor() and getField()  
to manipulate it however you want. Later downcasts will work.

b) You want to pass around a message that contains another message of  
some type. See: 

For my RPC implementation, I just use something like:

message RpcRequest {
     required string message_name = 2;
     required bytes request = 3;

Then I have a mapping from message names to Message "prototypes", so  
you can create a Message of an appropriate type.

Hope this helps,


Evan Jones

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to