Re: [protobuf] Re: Message.getField(FieldDescriptor) and enum values.
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 * On Mon, May 23, 2011 at 6:40 PM, Ben Wright wrote: > 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 > > On May 23, 5:09 pm, Jim Mayer wrote: > > 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 > > > > > > > > > > > > > > > > On Mon, May 23, 2011 at 4:37 PM, Ben Wright > wrote: > > > 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 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 ; > > > > } 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( > > > 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. > > -- > 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. > > -- 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.
Re: [protobuf] Re: Message.getField(FieldDescriptor) and enum values.
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 On Mon, May 23, 2011 at 4:37 PM, Ben Wright wrote: > 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 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 ; > > } 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( > 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. > > -- 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.
[protobuf] Message.getField(FieldDescriptor) and enum values.
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 ; } 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(); 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.