Message.getField(FieldDescriptor) and enum values.

1,597 views
Skip to first unread message

Jim Mayer

unread,
May 23, 2011, 1:42:08 PM5/23/11
to Protocol Buffers
In the Java protocol buffer binding, is there a way to get the Java
enum value using protocol buffer reflection? Specifically, I'd like
to be able to write something like the following:

Object getFieldValue(MessageOrBuilder source, FieldDescriptor field)
{
Object value = source.getField(field);
if (value instanceof EnumValueDescriptor) {
return <the Java enum corresponding to the descriptor>;
} else {
return value;
}
}

As far as I can tell, this isn't possible as the methods that return
enums from the descriptors are all static on the enum class. Since
they are static, there is no way (short of reflection games) to invoke
them unless one has the class in hand. I would enjoy being proved
wrong :-)

What we'd really like to see is an easy mechanism that would give us
the following:

Something explicitValue = message.getXXX();
Object reflectionValue =
message.getTheJavaValueFromAFieldDescriptor(<the field descriptor for
XXX>);
assert explictValue == reflectionValue;

Similarly, we'd like to be able do the same thing for 'setXXX' methods
for builders, however in this case it's easy to go from an enum value
to its descriptor so we can get by with the 'setField' method.

Thanks!

-- Jim

Ben Wright

unread,
May 23, 2011, 4:37:26 PM5/23/11
to Protocol Buffers
Assuming you know the Java Enum Type for which the EnumValueDescriptor
is describing a type of...

MyJavaEnum.valueOf((EnumValueDescriptor)value)

will return the appropriate java enum value.

Jim Mayer

unread,
May 23, 2011, 5:09:48 PM5/23/11
to Ben Wright, Protocol Buffers
That's exactly the problem.  We don't.  This issue first came up for us when writing code to copy data from one subtree to another of a protocol buffer.  By passing in a field descriptor we were able to eliminate a LOT of duplicated code.  In that case, we only needed the field access to write and so were able to get the descriptor.  We anticipate the same issue coming up for reading, however.

We could define an interface with a 'get' and a 'set' method and avoid using getField/setField at all, but it seems unfortunate that the capability isn't in the API.  The data appears to all be there; in fact, it appears that GeneratedMessage.SingularEnumFieldAccessor adds extra code to get the current behavior.

-- Jim

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Ben Wright

unread,
May 23, 2011, 6:40:10 PM5/23/11
to Protocol Buffers
If that's all you're trying to do, you don't really need the java enum
value...

The EnumValueDescriptor is what you use for the value when calling
setField(FieldDescriptor field, Object value) for an Enum Field

Jim Mayer

unread,
May 31, 2011, 5:54:21 PM5/31/11
to Ben Wright, Protocol Buffers
Are there any other thoughts on this?  We would like to write some methods that operate "generically" on fields.  Solutions such as the one Ben points out force the code for enums differ from the code for all other field types.  We can fake it when assigning fields from a value (the EnumValueDescriptor is accessible from an enum instance), but there does not appear to be a way to go from an EnumValueDescriptor to an enum value that does not rely on calling static methods.

-- Jim
Reply all
Reply to author
Forward
0 new messages