I'm just starting using Protocol Buffers for Java for a network

Here is how I designed my proto file:

message Message {
        enum Type {
                AUTH = 100;
                  CHAT = 101;

        required Type type = 1;
        optional int32 id = 2;

        extensions 100 to max;

message AuthRequest {
        extend Message {
                optional AuthRequest auth_request = 100;
        required string name = 1;
        required string pass = 2;

message ChatRequest {
        extend Message {
                optional ChatRequest chat_request = 101;
        required int32 player_id = 1;
        optional string message_string = 2;

This is based on the Union Types technique described in the

Now, all is well, I can do:
ChatRequest chat = 
Message request = 
and then serialize request to the wire.

What I'm trying to achieve now is to generalize this operation for all
my defined request types, using the descriptors and registry extension:

ExtensionRegistry registry = ExtensionRegistry.newInstance();

Then I can create "generically" a builder for a given type with:

public Builder getBuilder(Type type)
  ExtensionRegistry.ExtensionInfo info = 
registry.findExtensionByNumber(Message.getDescriptor(), type.getNumber());
  return info.defaultInstance.newBuilderForType();
And on client call site:
ChatRequest chat = (ChatRequest.Builder)getBuilder(Type.CHAT);

Now, I'm trying to generically associate this ChatRequest to the correct
Extension field in the Message type, but unfortunately I can't find a
way of doing that, because setExtension() requires a
GeneratedMessage.GeneratedExtension which I can't have generically (ie
the extension registry even though generated with this information,
doesn't keep it).
What's worse is tha the Extension Registry has all the information, but
I can't recreate this specific generic type or add the extension from
the Registry information.

First question: how can I solve this?
Second question: if it's not solvable, then that means I'm not using PB
as it was intended. In this case, how should I use it?

Brice Figureau
My Blog: http://www.masterzen.fr/

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