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 <compuware...@gmail.com> 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 <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.
>
>

-- 
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.

Reply via email to