Getting all fields of Message through the java reflection api

2,633 views
Skip to first unread message

Thibaut

unread,
Nov 20, 2009, 11:25:01 AM11/20/09
to Protocol Buffers
Hi,

I can't find a method how I can access all the fields of the message.
There is a method .getAllFields(); on the Message and Builder objects,
but this will only return the fields which are set.

I need to get access to all the fields (also those not set!) of a
Message m object, so I can update the unset fields through the
corresponding builder object (Message.Builder b = m.newBuilderForType
().mergeFrom(m);).
How can I do this?

Thanks,
Thibaut

Kenton Varda

unread,
Nov 20, 2009, 12:23:08 PM11/20/09
to Thibaut, Protocol Buffers
Use getDescriptor() to get the descriptor, then iterate through descriptor.fields().

Note that this will not cover extensions.

BTW, I believe m.toBuilder() is equivalent to m.newBuilderForType().mergeFrom(m).


--

You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=.



Thibaut

unread,
Nov 24, 2009, 8:10:06 AM11/24/09
to Protocol Buffers
Thanks,

It's working fine now!
Reply all
Reply to author
Forward
0 new messages