Skip to content

Instantly share code, notes, and snippets.

@kshchepanovskyi
Last active March 7, 2025 17:41
Show Gist options
  • Save kshchepanovskyi/c0bf9b9f95a02742a310 to your computer and use it in GitHub Desktop.
Save kshchepanovskyi/c0bf9b9f95a02742a310 to your computer and use it in GitHub Desktop.
Protobuf: message field and default values

About default value for message fields

If field type is message, what should getter return if it is not set?

Proto2 specification does not define default value for message type, it is defined only for scalar types:

If the default value is not specified for an optional element, a type-specific default value is used instead: for strings, the default value is the empty string. For bools, the default value is false. For numeric types, the default value is zero. For enums, the default value is the first value listed in the enum's type definition.

In proto2, at least for java, getters never return null. So you can write code without additional checks - if some field is not set, it will return default value. For instance, xxx.getFeatureSettings().isEnabled() will not throw an exception if featureSettings is not set. I really like this approach.

Few weeks ago Google updated specification for proto3. Proto3 specification defines it explicitly - default value is null:

When a message is parsed, if the encoded message does not contain a particular singular element, the corresponding field in the parsed object is set to the default value for that field. These defaults are type-specific:

  • For strings, the default value is the empty string.
  • For bytes, the default value is empty bytes.
  • For bools, the default value is false.
  • For numeric types, the default value is zero.
  • For enums, the default value is the first defined enum value, which must be 0.
  • For message fields, the default value is null.

The default value for repeated fields is empty (generally an empty list in the appropriate language).

Note that for scalar message fields, once a message is parsed there's no way of telling whether a field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types. For example, don't have a boolean that switches on some behaviour when set to false if you don't want that behaviour to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.

However, for java_out situation is different. In the code, generated with java_out, getters does not return null - if message field is not set, then getter returns default value.

For example, let's check code generated for following proto file.

syntax = "proto3";
package test;

message TestMessage {   
  MyData data = 1; 
}

message MyData {}

Code generated by protoc (version 2.6.1) - java_out:

private void initFields() {
  data_ = test.Test2.MyData.getDefaultInstance();
}

private TestMessage(
    com.google.protobuf.CodedInputStream input,
    com.google.protobuf.ExtensionRegistryLite extensionRegistry)
    throws com.google.protobuf.InvalidProtocolBufferException {
  initFields();
  int mutable_bitField0_ = 0;
  com.google.protobuf.UnknownFieldSet.Builder unknownFields =
      com.google.protobuf.UnknownFieldSet.newBuilder();
  try {
    boolean done = false;
    while (!done) {
      int tag = input.readTag();
      switch (tag) {

Field value is set to default before message is deserialized.

Code generated by protoc (version 3.0-beta1) - java_out:

public boolean hasData() {
  return data_ != null;
}

public test.Test.MyData getData() {
  return data_ == null ? test.Test.MyData.getDefaultInstance() : data_;
}

Field value is null if value is not set, but getter returns default instance.

Code generated by protoc (version 3.0-beta1) - javanano_out:

// optional .test.MyData data = 1;
public test.Test.MyData data;

Field value is null if value is not set.

Sources: https://gist.github.com/kshchepanovskyi/c0bf9b9f95a02742a310

In other languages, supported by protobuf compiler version 3.0.0-beta1, if message field is not set, then field's "accessor" (getter or public field) returns different values: in one case it is default message instance, in other cases it might be null (or None, or undefined - depending on language).

So, answer to original question is "it depends". If your generator already works in some way, I think it does not make much sense to change its behavior.

// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: src/main/proto/test2.proto
package test;
public final class Test2 {
private Test2() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface TestMessageOrBuilder extends
// @@protoc_insertion_point(interface_extends:test.TestMessage)
com.google.protobuf.MessageOrBuilder {
/**
* <code>optional .test.MyData data = 1;</code>
*/
boolean hasData();
/**
* <code>optional .test.MyData data = 1;</code>
*/
test.Test2.MyData getData();
/**
* <code>optional .test.MyData data = 1;</code>
*/
test.Test2.MyDataOrBuilder getDataOrBuilder();
}
/**
* Protobuf type {@code test.TestMessage}
*/
public static final class TestMessage extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:test.TestMessage)
TestMessageOrBuilder {
// Use TestMessage.newBuilder() to construct.
private TestMessage(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private TestMessage(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final TestMessage defaultInstance;
public static TestMessage getDefaultInstance() {
return defaultInstance;
}
public TestMessage getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private TestMessage(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 10: {
test.Test2.MyData.Builder subBuilder = null;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
subBuilder = data_.toBuilder();
}
data_ = input.readMessage(test.Test2.MyData.PARSER, extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom(data_);
data_ = subBuilder.buildPartial();
}
bitField0_ |= 0x00000001;
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test2.internal_static_test_TestMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test2.internal_static_test_TestMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test2.TestMessage.class, test.Test2.TestMessage.Builder.class);
}
public static com.google.protobuf.Parser<TestMessage> PARSER =
new com.google.protobuf.AbstractParser<TestMessage>() {
public TestMessage parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new TestMessage(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<TestMessage> getParserForType() {
return PARSER;
}
private int bitField0_;
public static final int DATA_FIELD_NUMBER = 1;
private test.Test2.MyData data_;
/**
* <code>optional .test.MyData data = 1;</code>
*/
public boolean hasData() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test2.MyData getData() {
return data_;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test2.MyDataOrBuilder getDataOrBuilder() {
return data_;
}
private void initFields() {
data_ = test.Test2.MyData.getDefaultInstance();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeMessage(1, data_);
}
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(1, data_);
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static test.Test2.TestMessage parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test2.TestMessage parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test2.TestMessage parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test2.TestMessage parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test2.TestMessage parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test2.TestMessage parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static test.Test2.TestMessage parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static test.Test2.TestMessage parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static test.Test2.TestMessage parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test2.TestMessage parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(test.Test2.TestMessage prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code test.TestMessage}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:test.TestMessage)
test.Test2.TestMessageOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test2.internal_static_test_TestMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test2.internal_static_test_TestMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test2.TestMessage.class, test.Test2.TestMessage.Builder.class);
}
// Construct using test.Test2.TestMessage.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
getDataFieldBuilder();
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
if (dataBuilder_ == null) {
data_ = test.Test2.MyData.getDefaultInstance();
} else {
dataBuilder_.clear();
}
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return test.Test2.internal_static_test_TestMessage_descriptor;
}
public test.Test2.TestMessage getDefaultInstanceForType() {
return test.Test2.TestMessage.getDefaultInstance();
}
public test.Test2.TestMessage build() {
test.Test2.TestMessage result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public test.Test2.TestMessage buildPartial() {
test.Test2.TestMessage result = new test.Test2.TestMessage(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
if (dataBuilder_ == null) {
result.data_ = data_;
} else {
result.data_ = dataBuilder_.build();
}
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof test.Test2.TestMessage) {
return mergeFrom((test.Test2.TestMessage)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(test.Test2.TestMessage other) {
if (other == test.Test2.TestMessage.getDefaultInstance()) return this;
if (other.hasData()) {
mergeData(other.getData());
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
test.Test2.TestMessage parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (test.Test2.TestMessage) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private test.Test2.MyData data_ = test.Test2.MyData.getDefaultInstance();
private com.google.protobuf.SingleFieldBuilder<
test.Test2.MyData, test.Test2.MyData.Builder, test.Test2.MyDataOrBuilder> dataBuilder_;
/**
* <code>optional .test.MyData data = 1;</code>
*/
public boolean hasData() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test2.MyData getData() {
if (dataBuilder_ == null) {
return data_;
} else {
return dataBuilder_.getMessage();
}
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder setData(test.Test2.MyData value) {
if (dataBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
data_ = value;
onChanged();
} else {
dataBuilder_.setMessage(value);
}
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder setData(
test.Test2.MyData.Builder builderForValue) {
if (dataBuilder_ == null) {
data_ = builderForValue.build();
onChanged();
} else {
dataBuilder_.setMessage(builderForValue.build());
}
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder mergeData(test.Test2.MyData value) {
if (dataBuilder_ == null) {
if (((bitField0_ & 0x00000001) == 0x00000001) &&
data_ != test.Test2.MyData.getDefaultInstance()) {
data_ =
test.Test2.MyData.newBuilder(data_).mergeFrom(value).buildPartial();
} else {
data_ = value;
}
onChanged();
} else {
dataBuilder_.mergeFrom(value);
}
bitField0_ |= 0x00000001;
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder clearData() {
if (dataBuilder_ == null) {
data_ = test.Test2.MyData.getDefaultInstance();
onChanged();
} else {
dataBuilder_.clear();
}
bitField0_ = (bitField0_ & ~0x00000001);
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test2.MyData.Builder getDataBuilder() {
bitField0_ |= 0x00000001;
onChanged();
return getDataFieldBuilder().getBuilder();
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test2.MyDataOrBuilder getDataOrBuilder() {
if (dataBuilder_ != null) {
return dataBuilder_.getMessageOrBuilder();
} else {
return data_;
}
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
private com.google.protobuf.SingleFieldBuilder<
test.Test2.MyData, test.Test2.MyData.Builder, test.Test2.MyDataOrBuilder>
getDataFieldBuilder() {
if (dataBuilder_ == null) {
dataBuilder_ = new com.google.protobuf.SingleFieldBuilder<
test.Test2.MyData, test.Test2.MyData.Builder, test.Test2.MyDataOrBuilder>(
getData(),
getParentForChildren(),
isClean());
data_ = null;
}
return dataBuilder_;
}
// @@protoc_insertion_point(builder_scope:test.TestMessage)
}
static {
defaultInstance = new TestMessage(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:test.TestMessage)
}
public interface MyDataOrBuilder extends
// @@protoc_insertion_point(interface_extends:test.MyData)
com.google.protobuf.MessageOrBuilder {
}
/**
* Protobuf type {@code test.MyData}
*/
public static final class MyData extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:test.MyData)
MyDataOrBuilder {
// Use MyData.newBuilder() to construct.
private MyData(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private MyData(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final MyData defaultInstance;
public static MyData getDefaultInstance() {
return defaultInstance;
}
public MyData getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private MyData(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test2.internal_static_test_MyData_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test2.internal_static_test_MyData_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test2.MyData.class, test.Test2.MyData.Builder.class);
}
public static com.google.protobuf.Parser<MyData> PARSER =
new com.google.protobuf.AbstractParser<MyData>() {
public MyData parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new MyData(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<MyData> getParserForType() {
return PARSER;
}
private void initFields() {
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static test.Test2.MyData parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test2.MyData parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test2.MyData parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test2.MyData parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test2.MyData parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test2.MyData parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static test.Test2.MyData parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static test.Test2.MyData parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static test.Test2.MyData parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test2.MyData parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(test.Test2.MyData prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code test.MyData}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:test.MyData)
test.Test2.MyDataOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test2.internal_static_test_MyData_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test2.internal_static_test_MyData_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test2.MyData.class, test.Test2.MyData.Builder.class);
}
// Construct using test.Test2.MyData.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return test.Test2.internal_static_test_MyData_descriptor;
}
public test.Test2.MyData getDefaultInstanceForType() {
return test.Test2.MyData.getDefaultInstance();
}
public test.Test2.MyData build() {
test.Test2.MyData result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public test.Test2.MyData buildPartial() {
test.Test2.MyData result = new test.Test2.MyData(this);
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof test.Test2.MyData) {
return mergeFrom((test.Test2.MyData)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(test.Test2.MyData other) {
if (other == test.Test2.MyData.getDefaultInstance()) return this;
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
test.Test2.MyData parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (test.Test2.MyData) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
// @@protoc_insertion_point(builder_scope:test.MyData)
}
static {
defaultInstance = new MyData(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:test.MyData)
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_test_TestMessage_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_test_TestMessage_fieldAccessorTable;
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_test_MyData_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_test_MyData_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
java.lang.String[] descriptorData = {
"\n\032src/main/proto/test2.proto\022\004test\")\n\013Te" +
"stMessage\022\032\n\004data\030\001 \001(\0132\014.test.MyData\"\010\n" +
"\006MyData"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_test_TestMessage_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_test_TestMessage_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_test_TestMessage_descriptor,
new java.lang.String[] { "Data", });
internal_static_test_MyData_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_test_MyData_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_test_MyData_descriptor,
new java.lang.String[] { });
}
// @@protoc_insertion_point(outer_class_scope)
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: src/main/proto/test.proto
package test;
public final class Test {
private Test() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface TestMessageOrBuilder extends
// @@protoc_insertion_point(interface_extends:test.TestMessage)
com.google.protobuf.MessageOrBuilder {
/**
* <code>optional .test.MyData data = 1;</code>
*/
boolean hasData();
/**
* <code>optional .test.MyData data = 1;</code>
*/
test.Test.MyData getData();
/**
* <code>optional .test.MyData data = 1;</code>
*/
test.Test.MyDataOrBuilder getDataOrBuilder();
}
/**
* Protobuf type {@code test.TestMessage}
*/
public static final class TestMessage extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:test.TestMessage)
TestMessageOrBuilder {
// Use TestMessage.newBuilder() to construct.
private TestMessage(com.google.protobuf.GeneratedMessage.Builder builder) {
super(builder);
}
private TestMessage() {
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
}
private TestMessage(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry) {
this();
int mutable_bitField0_ = 0;
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!input.skipField(tag)) {
done = true;
}
break;
}
case 10: {
test.Test.MyData.Builder subBuilder = null;
if (data_ != null) {
subBuilder = data_.toBuilder();
}
data_ = input.readMessage(test.Test.MyData.PARSER, extensionRegistry);
if (subBuilder != null) {
subBuilder.mergeFrom(data_);
data_ = subBuilder.buildPartial();
}
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw new RuntimeException(e.setUnfinishedMessage(this));
} catch (java.io.IOException e) {
throw new RuntimeException(
new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this));
} finally {
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test.internal_static_test_TestMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test.internal_static_test_TestMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test.TestMessage.class, test.Test.TestMessage.Builder.class);
}
public static final int DATA_FIELD_NUMBER = 1;
private test.Test.MyData data_;
/**
* <code>optional .test.MyData data = 1;</code>
*/
public boolean hasData() {
return data_ != null;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test.MyData getData() {
return data_ == null ? test.Test.MyData.getDefaultInstance() : data_;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test.MyDataOrBuilder getDataOrBuilder() {
return getData();
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (data_ != null) {
output.writeMessage(1, getData());
}
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (data_ != null) {
size += com.google.protobuf.CodedOutputStream
.computeMessageSize(1, getData());
}
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
public static test.Test.TestMessage parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test.TestMessage parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test.TestMessage parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test.TestMessage parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test.TestMessage parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test.TestMessage parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static test.Test.TestMessage parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static test.Test.TestMessage parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static test.Test.TestMessage parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test.TestMessage parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(test.Test.TestMessage prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code test.TestMessage}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:test.TestMessage)
test.Test.TestMessageOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test.internal_static_test_TestMessage_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test.internal_static_test_TestMessage_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test.TestMessage.class, test.Test.TestMessage.Builder.class);
}
// Construct using test.Test.TestMessage.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
public Builder clear() {
super.clear();
if (dataBuilder_ == null) {
data_ = null;
} else {
data_ = null;
dataBuilder_ = null;
}
return this;
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return test.Test.internal_static_test_TestMessage_descriptor;
}
public test.Test.TestMessage getDefaultInstanceForType() {
return test.Test.TestMessage.getDefaultInstance();
}
public test.Test.TestMessage build() {
test.Test.TestMessage result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public test.Test.TestMessage buildPartial() {
test.Test.TestMessage result = new test.Test.TestMessage(this);
if (dataBuilder_ == null) {
result.data_ = data_;
} else {
result.data_ = dataBuilder_.build();
}
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof test.Test.TestMessage) {
return mergeFrom((test.Test.TestMessage)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(test.Test.TestMessage other) {
if (other == test.Test.TestMessage.getDefaultInstance()) return this;
if (other.hasData()) {
mergeData(other.getData());
}
onChanged();
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
test.Test.TestMessage parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (test.Test.TestMessage) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private test.Test.MyData data_ = null;
private com.google.protobuf.SingleFieldBuilder<
test.Test.MyData, test.Test.MyData.Builder, test.Test.MyDataOrBuilder> dataBuilder_;
/**
* <code>optional .test.MyData data = 1;</code>
*/
public boolean hasData() {
return dataBuilder_ != null || data_ != null;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test.MyData getData() {
if (dataBuilder_ == null) {
return data_ == null ? test.Test.MyData.getDefaultInstance() : data_;
} else {
return dataBuilder_.getMessage();
}
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder setData(test.Test.MyData value) {
if (dataBuilder_ == null) {
if (value == null) {
throw new NullPointerException();
}
data_ = value;
onChanged();
} else {
dataBuilder_.setMessage(value);
}
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder setData(
test.Test.MyData.Builder builderForValue) {
if (dataBuilder_ == null) {
data_ = builderForValue.build();
onChanged();
} else {
dataBuilder_.setMessage(builderForValue.build());
}
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder mergeData(test.Test.MyData value) {
if (dataBuilder_ == null) {
if (data_ != null) {
data_ =
test.Test.MyData.newBuilder(data_).mergeFrom(value).buildPartial();
} else {
data_ = value;
}
onChanged();
} else {
dataBuilder_.mergeFrom(value);
}
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public Builder clearData() {
if (dataBuilder_ == null) {
data_ = null;
onChanged();
} else {
data_ = null;
dataBuilder_ = null;
}
return this;
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test.MyData.Builder getDataBuilder() {
onChanged();
return getDataFieldBuilder().getBuilder();
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
public test.Test.MyDataOrBuilder getDataOrBuilder() {
if (dataBuilder_ != null) {
return dataBuilder_.getMessageOrBuilder();
} else {
return data_ == null ?
test.Test.MyData.getDefaultInstance() : data_;
}
}
/**
* <code>optional .test.MyData data = 1;</code>
*/
private com.google.protobuf.SingleFieldBuilder<
test.Test.MyData, test.Test.MyData.Builder, test.Test.MyDataOrBuilder>
getDataFieldBuilder() {
if (dataBuilder_ == null) {
dataBuilder_ = new com.google.protobuf.SingleFieldBuilder<
test.Test.MyData, test.Test.MyData.Builder, test.Test.MyDataOrBuilder>(
getData(),
getParentForChildren(),
isClean());
data_ = null;
}
return dataBuilder_;
}
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return this;
}
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return this;
}
// @@protoc_insertion_point(builder_scope:test.TestMessage)
}
// @@protoc_insertion_point(class_scope:test.TestMessage)
private static final test.Test.TestMessage DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new test.Test.TestMessage();
}
public static test.Test.TestMessage getDefaultInstance() {
return DEFAULT_INSTANCE;
}
public static final com.google.protobuf.Parser<TestMessage> PARSER =
new com.google.protobuf.AbstractParser<TestMessage>() {
public TestMessage parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
try {
return new TestMessage(input, extensionRegistry);
} catch (RuntimeException e) {
if (e.getCause() instanceof
com.google.protobuf.InvalidProtocolBufferException) {
throw (com.google.protobuf.InvalidProtocolBufferException)
e.getCause();
}
throw e;
}
}
};
@java.lang.Override
public com.google.protobuf.Parser<TestMessage> getParserForType() {
return PARSER;
}
public test.Test.TestMessage getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
public interface MyDataOrBuilder extends
// @@protoc_insertion_point(interface_extends:test.MyData)
com.google.protobuf.MessageOrBuilder {
}
/**
* Protobuf type {@code test.MyData}
*/
public static final class MyData extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:test.MyData)
MyDataOrBuilder {
// Use MyData.newBuilder() to construct.
private MyData(com.google.protobuf.GeneratedMessage.Builder builder) {
super(builder);
}
private MyData() {
}
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
}
private MyData(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry) {
this();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!input.skipField(tag)) {
done = true;
}
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw new RuntimeException(e.setUnfinishedMessage(this));
} catch (java.io.IOException e) {
throw new RuntimeException(
new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this));
} finally {
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test.internal_static_test_MyData_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test.internal_static_test_MyData_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test.MyData.class, test.Test.MyData.Builder.class);
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
public static test.Test.MyData parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test.MyData parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test.MyData parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static test.Test.MyData parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static test.Test.MyData parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test.MyData parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static test.Test.MyData parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static test.Test.MyData parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static test.Test.MyData parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static test.Test.MyData parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(test.Test.MyData prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
}
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code test.MyData}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:test.MyData)
test.Test.MyDataOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return test.Test.internal_static_test_MyData_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return test.Test.internal_static_test_MyData_fieldAccessorTable
.ensureFieldAccessorsInitialized(
test.Test.MyData.class, test.Test.MyData.Builder.class);
}
// Construct using test.Test.MyData.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
public Builder clear() {
super.clear();
return this;
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return test.Test.internal_static_test_MyData_descriptor;
}
public test.Test.MyData getDefaultInstanceForType() {
return test.Test.MyData.getDefaultInstance();
}
public test.Test.MyData build() {
test.Test.MyData result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public test.Test.MyData buildPartial() {
test.Test.MyData result = new test.Test.MyData(this);
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof test.Test.MyData) {
return mergeFrom((test.Test.MyData)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(test.Test.MyData other) {
if (other == test.Test.MyData.getDefaultInstance()) return this;
onChanged();
return this;
}
public final boolean isInitialized() {
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
test.Test.MyData parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (test.Test.MyData) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return this;
}
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return this;
}
// @@protoc_insertion_point(builder_scope:test.MyData)
}
// @@protoc_insertion_point(class_scope:test.MyData)
private static final test.Test.MyData DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new test.Test.MyData();
}
public static test.Test.MyData getDefaultInstance() {
return DEFAULT_INSTANCE;
}
public static final com.google.protobuf.Parser<MyData> PARSER =
new com.google.protobuf.AbstractParser<MyData>() {
public MyData parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
try {
return new MyData(input, extensionRegistry);
} catch (RuntimeException e) {
if (e.getCause() instanceof
com.google.protobuf.InvalidProtocolBufferException) {
throw (com.google.protobuf.InvalidProtocolBufferException)
e.getCause();
}
throw e;
}
}
};
@java.lang.Override
public com.google.protobuf.Parser<MyData> getParserForType() {
return PARSER;
}
public test.Test.MyData getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
}
}
private static com.google.protobuf.Descriptors.Descriptor
internal_static_test_TestMessage_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_test_TestMessage_fieldAccessorTable;
private static com.google.protobuf.Descriptors.Descriptor
internal_static_test_MyData_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_test_MyData_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
java.lang.String[] descriptorData = {
"\n\031src/main/proto/test.proto\022\004test\")\n\013Tes" +
"tMessage\022\032\n\004data\030\001 \001(\0132\014.test.MyData\"\010\n\006" +
"MyDatab\006proto3"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_test_TestMessage_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_test_TestMessage_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_test_TestMessage_descriptor,
new java.lang.String[] { "Data", });
internal_static_test_MyData_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_test_MyData_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_test_MyData_descriptor,
new java.lang.String[] { });
}
// @@protoc_insertion_point(outer_class_scope)
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
package test;
@SuppressWarnings("hiding")
public interface Test {
public static final class TestMessage extends
com.google.protobuf.nano.MessageNano {
private static volatile TestMessage[] _emptyArray;
public static TestMessage[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new TestMessage[0];
}
}
}
return _emptyArray;
}
// optional .test.MyData data = 1;
public test.Test.MyData data;
public TestMessage() {
clear();
}
public TestMessage clear() {
data = null;
cachedSize = -1;
return this;
}
@Override
public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
throws java.io.IOException {
if (this.data != null) {
output.writeMessage(1, this.data);
}
super.writeTo(output);
}
@Override
protected int computeSerializedSize() {
int size = super.computeSerializedSize();
if (this.data != null) {
size += com.google.protobuf.nano.CodedOutputByteBufferNano
.computeMessageSize(1, this.data);
}
return size;
}
@Override
public TestMessage mergeFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
return this;
default: {
if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
return this;
}
break;
}
case 10: {
if (this.data == null) {
this.data = new test.Test.MyData();
}
input.readMessage(this.data);
break;
}
}
}
}
public static TestMessage parseFrom(byte[] data)
throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
return com.google.protobuf.nano.MessageNano.mergeFrom(new TestMessage(), data);
}
public static TestMessage parseFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
return new TestMessage().mergeFrom(input);
}
}
public static final class MyData extends
com.google.protobuf.nano.MessageNano {
private static volatile MyData[] _emptyArray;
public static MyData[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new MyData[0];
}
}
}
return _emptyArray;
}
public MyData() {
clear();
}
public MyData clear() {
cachedSize = -1;
return this;
}
@Override
public MyData mergeFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
while (true) {
int tag = input.readTag();
switch (tag) {
case 0:
return this;
default: {
if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
return this;
}
break;
}
}
}
}
public static MyData parseFrom(byte[] data)
throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
return com.google.protobuf.nano.MessageNano.mergeFrom(new MyData(), data);
}
public static MyData parseFrom(
com.google.protobuf.nano.CodedInputByteBufferNano input)
throws java.io.IOException {
return new MyData().mergeFrom(input);
}
}
}
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: src/main/proto/test.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor.FileDescriptor(
name='src/main/proto/test.proto',
package='test',
syntax='proto3',
serialized_pb=_b('\n\x19src/main/proto/test.proto\x12\x04test\")\n\x0bTestMessage\x12\x1a\n\x04\x64\x61ta\x18\x01 \x01(\x0b\x32\x0c.test.MyData\"\x08\n\x06MyDatab\x06proto3')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
_TESTMESSAGE = _descriptor.Descriptor(
name='TestMessage',
full_name='test.TestMessage',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='data', full_name='test.TestMessage.data', index=0,
number=1, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=35,
serialized_end=76,
)
_MYDATA = _descriptor.Descriptor(
name='MyData',
full_name='test.MyData',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=78,
serialized_end=86,
)
_TESTMESSAGE.fields_by_name['data'].message_type = _MYDATA
DESCRIPTOR.message_types_by_name['TestMessage'] = _TESTMESSAGE
DESCRIPTOR.message_types_by_name['MyData'] = _MYDATA
TestMessage = _reflection.GeneratedProtocolMessageType('TestMessage', (_message.Message,), dict(
DESCRIPTOR = _TESTMESSAGE,
__module__ = 'src.main.proto.test_pb2'
# @@protoc_insertion_point(class_scope:test.TestMessage)
))
_sym_db.RegisterMessage(TestMessage)
MyData = _reflection.GeneratedProtocolMessageType('MyData', (_message.Message,), dict(
DESCRIPTOR = _MYDATA,
__module__ = 'src.main.proto.test_pb2'
# @@protoc_insertion_point(class_scope:test.MyData)
))
_sym_db.RegisterMessage(MyData)
# @@protoc_insertion_point(module_scope)
syntax = "proto3";
package test;
message TestMessage {
MyData data = 1;
}
message MyData {
}
syntax = "proto2";
package test;
message TestMessage {
optional MyData data = 1;
}
message MyData {
}
@agamble-quora
Copy link

Nice comments. I've been looking into defaults behavior and I came across this gist.

Looks like your comment about unset message fields being null is out of date. The current documentation says:

For message fields, the field is not set. Its exact value is language-dependent. See the generated code guide for details.

I know for C++, Java and Python, a default value is provided. I would suspect similar behavior from other language implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment