Hi Kenton, That seems to be working well. I used mergeFrom(inputStream) on the builder as below which looks the simplest way (I will add in an isInitialized check)
defaultInstance.newBuilderForType().mergeFrom(inputStream).build(); Thanks for the help, Martin. On Nov 1, 8:55 pm, Kenton Varda <ken...@google.com> wrote: > What you want to do is create a Map<Descriptor, Message> which maps from > descriptors to default instances of each of your generated classes. For > example: > > Map<Descriptor, Message> myMap = new ...; > myMap.put(FooMessage.getDescriptor(), FooMessage.getDefaultInstance()); > myMap.put(BarMessage.getDescriptor(), BarMessage.getDefaultInstance()); > myMap.put(BazMessage.getDescriptor(), BazMessage.getDefaultInstance()); > > Now when you have a descriptor desc and you want to create a Builder for > that type, you do: > > Message.Builder builder = myMap.get(desc).newBuilderForType(); > > When you call builder.build(), you will end up with a Message that can be > down-cast to your generated type. > > DynamicMessage is meant to be used for types that aren't compiled in at all. > So of course you can't cast from DynamicMessage to a generated class -- the > generated classes are not subclasses of DynamicMessage. > > On Sat, Oct 31, 2009 at 7:57 AM, Martin Gilday <martingil...@gmail.com>wrote: > > > > > > > I am trying to write a service in Java which will convert back and > > forth using protobufs. The docs say that this should be easy as > > "Reflection is provided as part of the Message and Message.Builder > > interfaces". However I cannot seem to find what this is referring to. > > > I would like have a generic method which takes a Descriptor and a > > stream which gives back an instance of message which I can later cast > > to my desired type. I have tried DynamicMessage from = > > DynamicMessage.parseFrom(descriptor, inputStream); but I get an > > exception when I attempt to cast this as my generate proto class. > > > Is there a solution to this? > > > Thanks, > > Martin. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---