Hi,
I do only create one FileDescriptor instance that contains all message
definitions, because they are all constructed dynamically.
For creating the FileDescriptorProtoBuilder I call:
FileDescriptorProto.Builder fileDescriptorProtoBuilder =
FileDescriptorProto.newBuilder();
Then I create all the message protos by calling (here A and B):
DescriptorProto.Builder messageProtoBuilderA =
DescriptorProto.newBuilder();
messageProtoBuilderA.setName( "A" );
DescriptorProto.Builder messageProtoBuilderB =
DescriptorProto.newBuilder();
messageProtoBuilderB.setName( "B" );
If message A references message B using field f I call:
messageProtoBuilderA.addFieldBuilder()
.setName("f")
.setNumber(2)
.setTypeName("B");
Then I add the message protos to the file descriptor:
fileDescriptorProtoBuilder.addMessageType(messageProtoBuilderA);
fileDescriptorProtoBuilder.addMessageType(messageProtoBuilderB);
Finally I create the file descriptor using:
FileDescriptorProto fileDescriptorProto =
fileDescriptorProtoBuilder.build();
FileDescriptor fileDescriptor =
FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]);
Now I can create concrete messages by calling:
Builder messageA =
DynamicMessage.newBuilder( fileDescriptor.findMessageTypeByName("A") );
Builder messageB =
DynamicMessage.newBuilder( fileDescriptor.findMessageTypeByName("B") );
messageA.setField(
fileDescriptor.findMessageTypeByName("A").findFieldByName("f"),
messageB.build() );
I hope I did not add any errors when abstracting the code and that I
understood your problem correctly.
Kind regards,
Robert
On 28 Jul., 14:35, Giancarlo Frison <
gfri...@chelab.com> wrote:
> on the field type descriptor previously create i set name as:
> b2.setName(typename)
>
> in the container class (which has reference the b2 field) I setup the
> field as:
> fb = DescriptorProtos.FieldDescriptorProto.newBuilder()
> .setName(fieldName).setNumber(i+
> +).setLabel(label).setTypeName(typename);
> b1.addField(fb.build());
>
> For creating the b1 descriptor, first I generate the descriptor of b2
> then:
> d1 = b1.build();
> fdp1 =
> DescriptorProtos.FileDescriptorProto.newBuilder().addMessageType(d1).build();
> > >
http://groups.google.com/group/protobuf?hl=en.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -