Tobias Neef | 24 Jul 2012 16:55
Picon

How to convert protobuf with same structure in java

Hi,

I try to convert the Java representation of a protobuf message into another message with the same fields (ordering, names, types). I try to do it the following way but this ends in a java.lang.InstantiationException:

public static <T extends GeneratedMessage> T commandToEvent(GeneratedMessage command,
Class<T> clazz) {
try {
Builder builder = clazz.newInstance().newBuilderForType();
Map<FieldDescriptor, Object> fields = command.getAllFields();
for(Entry<FieldDescriptor, Object> entry : fields.entrySet()) {
builder.setField(entry.getKey(), entry.getValue());
}
return (T)builder.build();
} catch (Exception e) {
Throwables.propagate(e);
}
return null;
}

Does somebody has an idea how to do this?

Thanks,
Tobias 

--
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/-/lhtLryvHEJsJ.
To post to this group, send email to protobuf <at> googlegroups.com.
To unsubscribe from this group, send email to protobuf+unsubscribe <at> googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

Gmane