Is the descriptor you provide to to the DynamicMessage the same exact
object as the descriptor for the generated message? It looks like you are
only comparing against some descriptorMap, based on the name of the
generated type.

If you want to be able to do this, your descriptorMap should simply contain
the descriptors of the generated types. You are only allowed to call
mergeFrom on two messages that have the same descriptor (compared by
identity).


On Thu, Aug 16, 2012 at 9:55 AM, Kiran Karra <kiran.ka...@gmail.com> wrote:

> Hi,
> I am trying to build a generic deserializer in Java.  We have defined
> several GPB messages, and the receiver is receiving them, not knowing what
> kind of message it is.  I take the serialized bytes and use
> DynamicMessage.parseFrom(descriptor, bytes) (I have the descriptor).
>
> I then take this DynamicMessage and am trying to build a GeneratedMessage
> from it.   The function below demonstrates how I am doing this:
>
>         public Message getGeneratedMessage(DynamicMessage msg,
> GeneratedMessage.Builder builder) {
>                 Descriptor desc = msg.getDescriptorForType();
>                 System.out.println("Received a DynamicMessage with
> descriptor = " + desc.getFullName());
>                 System.out.println("Received GeneratedMessage.Builder with
> descriptor = " + builder.getDescriptorForType().getFullName());
>
>                 if
> (!msg.getDescriptorForType().equals(descriptorMap.get(builder.getDescriptorForType().getFullName())))
> {
>                         throw new IllegalArgumentException("Msg was of
> type " + msg.getDescriptorForType().getFullName() + "; expected type " +
> builder.getDescriptorForType().getFullName());
>                 }
>
>                 builder.mergeFrom(msg);
>                 return builder.build();
>         }
>
> The program output is showing:
> Received a DynamicMessage with descriptor = gpb.SRI
> Received GeneratedMessage.Builder with descriptor = gpb.SRI
>
> However..., the mergeFrom method call actually throws the following
> exception:
>
> java.lang.IllegalArgumentException: mergeFrom(Message) can only merge
> messages of the same type.
>         at
> com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:256)
>
> I'm wondering if anybody has any clues.  The function that I am using to
> get the generated message from the serialized byte array is:
>
> public DynamicMessage parsePacket(final String name, final String
> description, byte[] data) throws InvalidProtocolBufferException,
> IllegalStateException {
>                 Descriptor descriptor = descriptorMap.get(description);
>                 DynamicMessage msg = null;
>                 if (descriptor != null) {
>
>                         msg = DynamicMessage.parseFrom(descriptor, data);
>                         return msg;
>                 }
>                 else {
>                         throw new IllegalStateException("Description does
> not match any known ProtocolBuffer class types!");
>                 }
>         }
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/protobuf/-/jofnQihGNLgJ.
> To post to this group, send email to protobuf@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.
>
>

-- 
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 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to