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. On May 23, 1:42 pm, Jim Mayer <j...@pentastich.org> wrote: > 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 -- 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.