I trying to develop "flexible" server for reciving message, i'm not
able to create java classes for each message type, but I can upload
proto descripter on server. Of course client (sender) will use
generated java classes for prepare messages:

My code (sender):
Person.Builder person = Person.newBuilder();
person.setId(Integer.valueOf(42));
person.setEmail("test_em...@gmail.com");
person.setName("Viktor Villari");
Person p=person.build();
FileOutputStream fstream = new FileOutputStream ("message.pf");
CodedOutputStream outSream = CodedOutputStream.newInstance(fstream);
p.writeTo(outSream);
outSream.flush();
System.out.println("sent");

Reciver (address.descriptor.proto - grenerated via descriptor.proto):
        FileInputStream input = new FileInputStream
("address.descriptor.proto");
                DescriptorProtos.FileDescriptorSet
fdsProto=DescriptorProtos.FileDescriptorSet.parseFrom(input);
                input = new FileInputStream ("message.pf");
            //
System.out.println(fds.getFile(0).getMessageType(0).getName());
                System.out.println("File name = 
"+fdsProto.getFile(0).getName());
                // System.out.println("Message type =
"+fdsProto.getFile(0).getMessageType(0).getName());
                // System.out.println("Field info =
"+fdsProto.getFile(0).getMessageType(0).getField(0).getName()+"
"+fdsProto.getFile(0).getMessageType(0).getField(0).getType());
                FileDescriptor
fileDescr=FileDescriptor.buildFrom(fdsProto.getFile(0), new
FileDescriptor[0]);
                System.out.println("Message type =
"+fileDescr.getMessageTypes().get(0).getName());
                System.out.println("Field info =
"+fileDescr.getMessageTypes().get(0).getFields().get(0).getName()+"
type=
"+fileDescr.getMessageTypes().get(0).getFields().get(0).getType());
                DynamicMessage 
dm=DynamicMessage.parseFrom(fdsProto.getDescriptor(),
input);
                System.out.println("DynamicMessage to string \n"+dm.toString());
                //---- problem line
                System.out.println("Person.name  = "+
dm.getField(fileDescr.getMessageTypes().get(0).getFields().get(0)));
                //----


Output:
File name = addressbook.proto
Message type = Person
Field info = name type= STRING
DynamicMessage to string
2: "Viktor Villari"
3: 42
4: "test_em...@gmail.com"

Exception in thread "main" java.lang.IllegalArgumentException:
FieldDescriptor does not match message type.
        at
com.google.protobuf.DynamicMessage.verifyContainingType(DynamicMessage.java:
242)
        at com.google.protobuf.DynamicMessage.getField(DynamicMessage.java:
160)
        at net.lookin.protobuf.Test.main(Test.java:64)

Why I recive exception?
How I can access to specific field value in DynamicMessage?

-- 
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.

Reply via email to