Generating.proto files from java source file

1,397 views
Skip to first unread message

grasshopper

unread,
Oct 8, 2009, 9:34:48 AM10/8/09
to prot...@googlegroups.com
Hi all, a guy that isn't working in my company anymore has defined some protocol buffer messages that we still use. We need to extend these messages now, but we don't have the .proto file. Is there a straight way to generate the proto files from the java classes? How could I do this?

Thanks

Kenton Varda

unread,
Oct 8, 2009, 2:32:30 PM10/8/09
to grasshopper, prot...@googlegroups.com
Yikes.  That's kind of like someone left you with just .class files without the .java files.

If you look at the code, though, you will notice that there are comments in it defining each field, like:
  // optional int32 i = 1;

These should be the exact field definitions as they might appear in the .proto file.  So if you extract those -- keeping track of which inner class each comment appeared in -- you should be able to reproduce the original .proto file.

Henner Zeller

unread,
Oct 8, 2009, 2:41:08 PM10/8/09
to Kenton Varda, grasshopper, prot...@googlegroups.com
On Thu, Oct 8, 2009 at 11:32, Kenton Varda <ken...@google.com> wrote:
> Yikes.  That's kind of like someone left you with just .class files without
> the .java files.
> If you look at the code, though, you will notice that there are comments in
> it defining each field, like:
>   // optional int32 i = 1;
> These should be the exact field definitions as they might appear in the
> .proto file.  So if you extract those -- keeping track of which inner class
> each comment appeared in -- you should be able to reproduce the original
> .proto file.

Shouldn't it be possible to extract the descriptor from the java class
and then use reflection to emit a proto-file ?

Kenton Varda

unread,
Oct 8, 2009, 2:51:02 PM10/8/09
to Henner Zeller, grasshopper, prot...@googlegroups.com
Hmm, that's true.  Although I'm not sure if there's actual code for writing the .proto file in Java.  In C++, descriptors have a DebugString() method which returns a compilable .proto file.

Henner Zeller

unread,
Oct 8, 2009, 3:02:03 PM10/8/09
to Kenton Varda, grasshopper, prot...@googlegroups.com
Hi,

On Thu, Oct 8, 2009 at 11:51, Kenton Varda <ken...@google.com> wrote:
> Hmm, that's true.  Although I'm not sure if there's actual code for writing
> the .proto file in Java.  In C++, descriptors have a DebugString() method
> which returns a compilable .proto file.

So it would be a matter of either re-implementing the DebugString in
Java or dumping out the descriptor from the Java code (after all, it
is just a proto buffer), re-read that and DebugString it.
(maybe protoc could have a
'read-binary-descriptor-and-dump-it-as-protofile' option ;) ).

grasshopper

unread,
Oct 9, 2009, 7:55:13 AM10/9/09
to Henner Zeller, Kenton Varda, prot...@googlegroups.com
I'll try to implement a simple tool to decompile messages, if I succeed, I post the solution here.

Thank you
Reply all
Reply to author
Forward
0 new messages