For any field x of type string, protoc seems to generate two java accessors:
getX()that returns aStringgetXBytes()that returns aByteString
While the getXBytes() method may be useful, I am surprised that this behavior doesn't seem to documented in the official protoc Java documentation.
Moreover, this introduces a potential name conflict issue. Take for example the following proto definition:
syntax = "proto3";
message Message {
string x = 2;
string x_bytes = 3;
}
Here is the corresponding interface generated by protoc
public interface MessageOrBuilder extends
// @@protoc_insertion_point(interface_extends:Message)
com.google.protobuf.MessageOrBuilder {
/**
* <code>string x = 2;</code>
* @return The x.
*/
java.lang.String getX();
/**
* <code>string x = 2;</code>
* @return The bytes for x.
*/
com.google.protobuf.ByteString
getXBytes();
/**
* <code>string x_bytes = 3;</code>
* @return The xBytes.
*/
java.lang.String getXBytes();
/**
* <code>string x_bytes = 3;</code>
* @return The bytes for xBytes.
*/
com.google.protobuf.ByteString
getXBytesBytes();
}
/**
This is invalid java because two methods are named getXBytes.
Interestingly, protoc does seem to handle similar name collisions for repeated fields, as described in the documentation: "If another non-repeated field has a name that conflicts with one of the repeated field’s generated methods, both field names will have their protobuf field number appended to the end.".
This seems like an inconsistency (or a bug). Did I miss something here? Is there a recommended way to handle such name conflicts?
Any insights or clarification would be greatly appreciated!
getXBytes()for every string field, and it does not consistently resolve naming conflicts like it does for repeated fields. Your conflict betweenxandx_bytescauses invalid Java code. To avoid this, rename fields to prevent overlaps (e.g., avoid using_bytessuffix for strings).getXBytes()method is not clearly documented.xandx_bytesare useful names.