How to Programmatically determine the Builder method names (getter and setter) for a given field name

1,341 views
Skip to first unread message

Ravi

unread,
Mar 15, 2011, 5:19:17 AM3/15/11
to Protocol Buffers, thisism...@gmail.com
Hi,

We would like to know how to determine the getter and setter method
names generated in java builder class for a particular field defined
in proto message.

Of course, the simple rule says that
* Convert first letter of the field name to upper case
* prefix "get" or "set" to the field name

But we want to know all the cases considered while generating builder
class.
Like what are the special characters considered? what are the
exceptional cases?

Why we want this is,
In our framework we would be programmatically generating proto files
from the xsds which we have already.

Along with this we would be generating an adapter class between our
framework and the java class generated by protoc compiler.

This adapter would call getter and setter methods of the builder class
available in the builder class to retrieve the value and to build the
object.

Any help would be appreciated.

Thanks,
Ravikumar K

Dmitriy Ryaboy

unread,
Mar 15, 2011, 6:09:42 AM3/15/11
to Ravi, Protocol Buffers, thisism...@gmail.com
We needed to do something similar for our project "Elephant-Bird" that provides support for working with Protocol Buffers in Hadoop.

Protobufs have Descriptors that allow you to determine all of these things on the fly, without encoding method naming rules and such.


public static Message addField(Message m, String name, Object value) {
    Message.Builder builder = m.toBuilder();
    setFieldByName(builder, name, value);
    return builder.build();
}

public static void setFieldByName(Message.Builder builder, String name, Object value) {
    FieldDescriptor fieldDescriptor = builder.getDescriptorForType().findFieldByName(name);
    if (value == null) {
      builder.clearField(fieldDescriptor);
    } else {
      builder.setField(fieldDescriptor, value);
    }
  }



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


Ravi

unread,
Mar 16, 2011, 2:42:59 AM3/16/11
to Protocol Buffers
Hi Thanks a lot for your time.

Yes the approach looks very clean as the api getField and setField are
in built. I think we would go with this approach.

But i am just curious to know whether protobuf published anywhere the
algorithm for deriving getter and setter method names.

Thanks,
Ravikumar K

On Mar 15, 3:09 pm, Dmitriy Ryaboy <dvrya...@gmail.com> wrote:
> We needed to do something similar for our project "Elephant-Bird" that
> provides support for working with Protocol Buffers in Hadoop.
>
> Protobufs have Descriptors that allow you to determine all of these things
> on the fly, without encoding method naming rules and such.
>
> Here is the relevant snippet fromhttps://github.com/kevinweil/elephant-bird/blob/master/src/java/com/t...
>
> public static Message addField(Message m, String name, Object value) {
>     Message.Builder builder = m.toBuilder();
>     setFieldByName(builder, name, value);
>     return builder.build();
>
> }
>
> public static void setFieldByName(Message.Builder builder, String
> name, Object value) {
>     FieldDescriptor fieldDescriptor =
> builder.getDescriptorForType().findFieldByName(name);
>     if (value == null) {
>       builder.clearField(fieldDescriptor);
>     } else {
>       builder.setField(fieldDescriptor, value);
>     }
>   }
> *
> *

Pherl Liu

unread,
Mar 24, 2011, 8:21:21 AM3/24/11
to Ravi, Protocol Buffers
Reply all
Reply to author
Forward
0 new messages