Suppose I have the following messages defined:

message A
    optional string str = 1;
    optional int32 number = 2;

message B
    optional A someA = 1;

Now, let's say I have a class (in Java):

class C
    private B.Builder bBuilder;

    // .... some where later....
    public void setStr(String s)
        // set the param 's' into B.a.str here

Now, from what I can tell, the best way to set the value of str in the
contained A message, without destroying any possible existing
A.number, is:

            A.Builder aBuilder;
            if (bBuilder.hasSomeA())
                A someA = bBuilder.getSomeA();
                aBuilder = A.newBuilder(someA);         // create a
builder from the immutable object already there
                aBuilder = bBuilder.getSomeA().newBuilderForType();

Does this look like the best way to do this?

What I'm trying to do is have my class ("C" in this example) carry
around the builder and modify it, and only build when ready to use the
data.  However, it seems that for composite messages that once I set
values I can't modify them unless I do so in this manner (which seems

Am I missing an easier way?


You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to