Muzietto Muzietto - 8 months ago 115
Java Question

Examining a protobuf message - how to get field values by name?

I seem unable to find a way to verify the value of a field inside a protobuf message without explicitly invoking its getter.

I see examples around that make usage of

instances to reach inside the message map, but they are either iterator-based or driven by field number.

Once I have the map:

Map<Descriptors.FieldDescriptor, Object> allFields = myMsg.getAllFields();

how can I get the value of field

I know that I can use
, but this is not usable in a systematic way.

If there is no way to access field values by their names, I'd like to know what is the rationale behind this choice. I may have still to understand the protobuf "philosophy" :-)

Answer Source

I am not sure you are looking for Descriptors#findFieldByName(name). You can try with followings:

FieldDescriptor fieldDescriptor = message.getDescriptorForType().findFieldByName("fieldXyz");
Object value = message.getField(fieldDescriptor);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download