Java and Dynamic messages

137 views
Skip to first unread message

TinCan

unread,
Oct 21, 2009, 5:11:16 AM10/21/09
to Protocol Buffers
Hi,
My problem:
I am working on a Java application which is using protocol buffers to
communicate with an application written in C++. I've met some
challenges at the Java end, because I do not have the opportunity to
compile the .proto files into my Java app.

At the C++ end we did not encounter any problems in parsing .proto
files and constructing protobuf messages (dynamic) at runtime. This
seems to be a little more tricky at the Java end.

What I try to do:
The C++ application is using protocol buffers for internal
communication. This works like a charm, and we are very happy with the
decision of using this API.
Now I have been working on a service application which is supposed to
display protocol buffer data dynamically using reflection. The C++
application will send .proto descriptions and protobuf data via an UDP
connection to the Java application.

My question:
I understand that there is not a .proto file parser available for
Java. Is this correct?
If so: How can I construct a message dynamically in a Java
application?

I've been looking at the classes DynamicMessage, Descriptor,
DescriptorProto and others (including their Builders). But I am
struggling to figure out how they are related, and how to use them
correctly.

Can anyone please help me with more documentation, or a working
example on how to create a protobuf message (dynamically)?

Regards
Ole

Kenton Varda

unread,
Oct 21, 2009, 2:06:22 PM10/21/09
to TinCan, Protocol Buffers
There is no .proto parser in Java.  However, you can use protoc's --descriptor_set_out flag to generate a FileDescriptorProto representing the file (or use FileDescriptor::CopyTo() in C++).  This is a protocol buffer representation of everything defined in the .proto.  In Java, you can then:

1) Parse the FileDescriptorProto.
2) Construct a FileDescriptor from it.
3) Use FindMessageTypeByName() on the FileDescriptor to get a Descriptor for the type you are interested in.
4) Construct a DynamicMessage from that.
Reply all
Reply to author
Forward
0 new messages