How to convert Descriptors.Descriptor to Java class name?

560 views
Skip to first unread message

ondrej par

unread,
Jun 6, 2011, 7:36:27 PM6/6/11
to prot...@googlegroups.com
Hi all,

is there a convenient way to extract the name of Java (generated) class that represents the Message from Descriptors.Descriptor?

What I need is a method with the following signature:
public Class<? extends Message> extractJavaClass(Descriptors.Descriptor d);

I can derive class name using file/message descriptors methods, but that seems quite difficult, since I have to take java_package_name and java_outer_class_name options into account, and I have to convert underscores in proto file name to camel case

Thanks for answer,
Ondrej

Pherl Liu

unread,
Jun 21, 2011, 1:15:31 AM6/21/11
to prot...@googlegroups.com
Quoted from our internal discussion by Kenton:

You should build a Map<Descriptor, Message> upfront, mapping every
descriptor you care about to the corresponding default instance.  Avoid
using Class objects because that will make your code incompatible with
DynamicMessage.  Using default instances instead avoids this problem and
should cover all the same use cases.

Note that it should be easy to write a function like:
  void populate(Message root, Map<Descriptor, Message> map);
which recursively populates the map with the given message type and all of
its dependencies.

In C++, we have a singleton MessageFactory::generated_factory() which
effectively provides a Descriptor -> default instance map for all
compiled-in types.  But, this leads to all the usual singleton problems, and
code that uses it is often needlessly incompatible with DynamicMessage.
 People like the idea of being able to look up any compiled-in type because
it seems convenient, but in practice the convenience is typically not very
large, whereas the loss of reusability can be a big problem down the road.


--
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/-/ZDBENWpTLVlYdDhK.
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=en.

Reply all
Reply to author
Forward
0 new messages