Class AbstractMessage.Builder<BuilderType> (3.19.4)

public abstract static class AbstractMessage.Builder<BuilderType> extends AbstractMessageLite.Builder implements Message.Builder

A partial implementation of the Message.Builder interface which implements as many methods of that interface as possible in terms of other methods.

Inheritance

java.lang.Object > AbstractMessageLite.Builder > AbstractMessage.Builder<BuilderType>

Implements

Message.Builder

Type Parameter

Name Description
BuilderType

Static Methods

newUninitializedMessageException(Message message)

protected static UninitializedMessageException newUninitializedMessageException(Message message)

Construct an UninitializedMessageException reporting missing fields in the given message.

Parameter
Name Description
message Message
Returns
Type Description
UninitializedMessageException

Constructors

Builder()

public Builder()

Methods

clear()

public BuilderType clear()

Resets all fields to their default values.

Returns
Type Description
BuilderType

clearOneof(Descriptors.OneofDescriptor oneof)

public BuilderType clearOneof(Descriptors.OneofDescriptor oneof)

TODO(jieluo): Clear it when all subclasses have implemented this method.

Parameter
Name Description
oneof Descriptors.OneofDescriptor
Returns
Type Description
BuilderType

clone()

public BuilderType clone()

Clones the Builder.

Returns
Type Description
BuilderType
Overrides

findInitializationErrors()

public List<String> findInitializationErrors()

Returns a list of field paths (e.g. "foo.bar.baz") of required fields which are not set in this message. You should call MessageLiteOrBuilder#isInitialized() first to check if there are any missing fields, as that method is likely to be much faster than this one even when the message is fully-initialized.

Returns
Type Description
List<String>

getFieldBuilder(Descriptors.FieldDescriptor field)

public Message.Builder getFieldBuilder(Descriptors.FieldDescriptor field)

Get a nested builder instance for the given field.

Normally, we hold a reference to the immutable message object for the message type field. Some implementations(the generated message builders), however, can also hold a reference to the builder object (a nested builder) for the field.

If the field is already backed up by a nested builder, the nested builder will be returned. Otherwise, a new field builder will be created and returned. The original message field (if exist) will be merged into the field builder, which will then be nested into its parent builder.

NOTE: implementations that do not support nested builders will throw UnsupportedOperationException.

Parameter
Name Description
field Descriptors.FieldDescriptor
Returns
Type Description
Message.Builder

getInitializationErrorString()

public String getInitializationErrorString()

Returns a comma-delimited list of required fields which are not set in this message object. You should call MessageLiteOrBuilder#isInitialized() first to check if there are any missing fields, as that method is likely to be much faster than this one even when the message is fully-initialized.

Returns
Type Description
String

getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof)

public Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof)

TODO(jieluo): Clear it when all subclasses have implemented this method.

Parameter
Name Description
oneof Descriptors.OneofDescriptor
Returns
Type Description
Descriptors.FieldDescriptor

getRepeatedFieldBuilder(Descriptors.FieldDescriptor field, int index)

public Message.Builder getRepeatedFieldBuilder(Descriptors.FieldDescriptor field, int index)

Get a nested builder instance for the given repeated field instance.

Normally, we hold a reference to the immutable message object for the message type field. Some implementations(the generated message builders), however, can also hold a reference to the builder object (a nested builder) for the field.

If the field is already backed up by a nested builder, the nested builder will be returned. Otherwise, a new field builder will be created and returned. The original message field (if exist) will be merged into the field builder, which will then be nested into its parent builder.

NOTE: implementations that do not support nested builders will throw UnsupportedOperationException.

Parameters
Name Description
field Descriptors.FieldDescriptor
index int
Returns
Type Description
Message.Builder

hasOneof(Descriptors.OneofDescriptor oneof)

public boolean hasOneof(Descriptors.OneofDescriptor oneof)

TODO(jieluo): Clear it when all subclasses have implemented this method.

Parameter
Name Description
oneof Descriptors.OneofDescriptor
Returns
Type Description
boolean

internalMergeFrom(AbstractMessageLite other)

protected BuilderType internalMergeFrom(AbstractMessageLite other)
Parameter
Name Description
other AbstractMessageLite
Returns
Type Description
BuilderType
Overrides

mergeDelimitedFrom(InputStream input)

public boolean mergeDelimitedFrom(InputStream input)

Like #mergeFrom(InputStream), but does not read until EOF. Instead, the size of the message (encoded as a varint) is read first, then the message data. Use MessageLite#writeDelimitedTo(OutputStream) to write messages in this format.

Parameter
Name Description
input InputStream
Returns
Type Description
boolean
Overrides
Exceptions
Type Description
IOException

mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)

public boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)

Like #mergeDelimitedFrom(InputStream) but supporting extensions.

Parameters
Name Description
input InputStream
extensionRegistry ExtensionRegistryLite
Returns
Type Description
boolean
Overrides
Exceptions
Type Description
IOException

mergeFrom(byte[] data)

public BuilderType mergeFrom(byte[] data)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream).

Parameter
Name Description
data byte[]
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)

public BuilderType mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream,ExtensionRegistryLite).

Parameters
Name Description
data byte[]
extensionRegistry ExtensionRegistryLite
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(byte[] data, int off, int len)

public BuilderType mergeFrom(byte[] data, int off, int len)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream).

Parameters
Name Description
data byte[]
off int
len int
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)

public BuilderType mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream,ExtensionRegistryLite).

Parameters
Name Description
data byte[]
off int
len int
extensionRegistry ExtensionRegistryLite
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(ByteString data)

public BuilderType mergeFrom(ByteString data)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream).

Parameter
Name Description
data ByteString
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)

public BuilderType mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)

Parse data as a message of this type and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream,ExtensionRegistryLite).

Parameters
Name Description
data ByteString
extensionRegistry ExtensionRegistryLite
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
InvalidProtocolBufferException

mergeFrom(CodedInputStream input)

public BuilderType mergeFrom(CodedInputStream input)

Parses a message of this type from the input and merges it with this message.

Warning: This does not verify that all required fields are present in the input message. If you call #build() without setting all required fields, it will throw an UninitializedMessageException, which is a RuntimeException and thus might not be caught. There are a few good ways to deal with this:

  • Call #isInitialized() to verify that all required fields are set before building.
  • Use buildPartial() to build, which ignores missing required fields.

Note: The caller should call CodedInputStream#checkLastTagWas(int) after calling this to verify that the last tag seen was the appropriate end-group tag, or zero for EOF.

Parameter
Name Description
input CodedInputStream
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
IOException

mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)

public BuilderType mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)

Like Builder#mergeFrom(CodedInputStream), but also parses extensions. The extensions that you want to be able to parse must be registered in extensionRegistry. Extensions not in the registry will be treated as unknown fields.

Parameters
Name Description
input CodedInputStream
extensionRegistry ExtensionRegistryLite
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
IOException

mergeFrom(Message other)

public BuilderType mergeFrom(Message other)

Merge other into the message being built. other must have the exact same type as this (i.e. getDescriptorForType() == other.getDescriptorForType()).

Merging occurs as follows. For each field:

  • For singular primitive fields, if the field is set in other, then other's value overwrites the value in this message.
  • For singular message fields, if the field is set in other, it is merged into the corresponding sub-message of this message using the same merging rules.
  • For repeated fields, the elements in other are concatenated with the elements in this message.
  • For oneof groups, if the other message has one of the fields set, the group of this message is cleared and replaced by the field of the other message, so that the oneof constraint is preserved.

    This is equivalent to the Message::MergeFrom method in C++.

Parameter
Name Description
other Message
Returns
Type Description
BuilderType

mergeFrom(InputStream input)

public BuilderType mergeFrom(InputStream input)

Parse a message of this type from input and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream). Note that this method always reads the entire input (unless it throws an exception). If you want it to stop earlier, you will need to wrap your input in some wrapper stream that limits reading. Or, use MessageLite#writeDelimitedTo(OutputStream) to write your message and #mergeDelimitedFrom(InputStream) to read it.

Despite usually reading the entire input, this does not close the stream.

Parameter
Name Description
input InputStream
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
IOException

mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)

public BuilderType mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)

Parse a message of this type from input and merge it with the message being built. This is just a small wrapper around #mergeFrom(CodedInputStream,ExtensionRegistryLite).

Parameters
Name Description
input InputStream
extensionRegistry ExtensionRegistryLite
Returns
Type Description
BuilderType
Overrides
Exceptions
Type Description
IOException

mergeUnknownFields(UnknownFieldSet unknownFields)

public BuilderType mergeUnknownFields(UnknownFieldSet unknownFields)

Merge some unknown fields into the UnknownFieldSet for this message.

Parameter
Name Description
unknownFields UnknownFieldSet
Returns
Type Description
BuilderType

toString()

public String toString()
Returns
Type Description
String
Overrides