Class GeneratedMessage (3.19.4)

public abstract class GeneratedMessage extends AbstractMessage implements Serializable

All generated protocol message classes extend this class. This class implements most of the Message and Builder interfaces using Java reflection. Users can ignore this class and pretend that generated messages implement the Message interface directly.

Implements

Serializable

Static Fields

alwaysUseFieldBuilders

protected static boolean alwaysUseFieldBuilders

For testing. Allows a test to disable the optimization that avoids using field builders for nested messages until they are requested. By disabling this optimization, existing tests can be reused to test the field builders.

Field Value
TypeDescription
boolean

Static Methods

<ContainingType,Type>newFileScopedGeneratedExtension(Class singularType, Message defaultInstance)

public static GeneratedMessage.GeneratedExtension<ContainingType,Type> <ContainingType,Type>newFileScopedGeneratedExtension(Class singularType, Message defaultInstance)

For use by generated code only.

Parameters
NameDescription
singularTypeClass
defaultInstanceMessage
Returns
TypeDescription
GeneratedExtension<ContainingType,Type>

<ContainingType,Type>newFileScopedGeneratedExtension(Class singularType, Message defaultInstance, String descriptorOuterClass, String extensionName)

public static GeneratedMessage.GeneratedExtension<ContainingType,Type> <ContainingType,Type>newFileScopedGeneratedExtension(Class singularType, Message defaultInstance, String descriptorOuterClass, String extensionName)

Used in proto1 generated code only.

After enabling bridge, we can define proto2 extensions (the extended type is a proto2 mutable message) in a proto1 .proto file. For these extensions we should generate proto2 GeneratedExtensions.

Parameters
NameDescription
singularTypeClass
defaultInstanceMessage
descriptorOuterClassString
extensionNameString
Returns
TypeDescription
GeneratedExtension<ContainingType,Type>

<ContainingType,Type>newMessageScopedGeneratedExtension(Message scope, int descriptorIndex, Class singularType, Message defaultInstance)

public static GeneratedMessage.GeneratedExtension<ContainingType,Type> <ContainingType,Type>newMessageScopedGeneratedExtension(Message scope, int descriptorIndex, Class singularType, Message defaultInstance)

For use by generated code only.

Parameters
NameDescription
scopeMessage
descriptorIndexint
singularTypeClass
defaultInstanceMessage
Returns
TypeDescription
GeneratedExtension<ContainingType,Type>

<ContainingType,Type>newMessageScopedGeneratedExtension(Message scope, String name, Class singularType, Message defaultInstance)

public static GeneratedMessage.GeneratedExtension<ContainingType,Type> <ContainingType,Type>newMessageScopedGeneratedExtension(Message scope, String name, Class singularType, Message defaultInstance)

Used in proto1 generated code only.

After enabling bridge, we can define proto2 extensions (the extended type is a proto2 mutable message) in a proto1 .proto file. For these extensions we should generate proto2 GeneratedExtensions.

Parameters
NameDescription
scopeMessage
nameString
singularTypeClass
defaultInstanceMessage
Returns
TypeDescription
GeneratedExtension<ContainingType,Type>

<M>parseDelimitedWithIOException(Parser<M> parser, InputStream input)

protected static M <M>parseDelimitedWithIOException(Parser<M> parser, InputStream input)
Parameters
NameDescription
parserParser<M>
inputInputStream
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

<M>parseDelimitedWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions)

protected static M <M>parseDelimitedWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions)
Parameters
NameDescription
parserParser<M>
inputInputStream
extensionsExtensionRegistryLite
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

<M>parseWithIOException(Parser<M> parser, CodedInputStream input)

protected static M <M>parseWithIOException(Parser<M> parser, CodedInputStream input)
Parameters
NameDescription
parserParser<M>
inputCodedInputStream
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

<M>parseWithIOException(Parser<M> parser, CodedInputStream input, ExtensionRegistryLite extensions)

protected static M <M>parseWithIOException(Parser<M> parser, CodedInputStream input, ExtensionRegistryLite extensions)
Parameters
NameDescription
parserParser<M>
inputCodedInputStream
extensionsExtensionRegistryLite
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

<M>parseWithIOException(Parser<M> parser, InputStream input)

protected static M <M>parseWithIOException(Parser<M> parser, InputStream input)
Parameters
NameDescription
parserParser<M>
inputInputStream
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

<M>parseWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions)

protected static M <M>parseWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions)
Parameters
NameDescription
parserParser<M>
inputInputStream
extensionsExtensionRegistryLite
Returns
TypeDescription
M
Exceptions
TypeDescription
IOException

computeStringSize(int fieldNumber, Object value)

protected static int computeStringSize(int fieldNumber, Object value)
Parameters
NameDescription
fieldNumberint
valueObject
Returns
TypeDescription
int

computeStringSizeNoTag(Object value)

protected static int computeStringSizeNoTag(Object value)
Parameter
NameDescription
valueObject
Returns
TypeDescription
int

writeString(CodedOutputStream output, int fieldNumber, Object value)

protected static void writeString(CodedOutputStream output, int fieldNumber, Object value)
Parameters
NameDescription
outputCodedOutputStream
fieldNumberint
valueObject
Exceptions
TypeDescription
IOException

writeStringNoTag(CodedOutputStream output, Object value)

protected static void writeStringNoTag(CodedOutputStream output, Object value)
Parameters
NameDescription
outputCodedOutputStream
valueObject
Exceptions
TypeDescription
IOException

Constructors

GeneratedMessage()

protected GeneratedMessage()

GeneratedMessage(GeneratedMessage.Builder<?> builder)

protected GeneratedMessage(GeneratedMessage.Builder<?> builder)
Parameter
NameDescription
builderBuilder<?>

Fields

unknownFields

protected UnknownFieldSet unknownFields

For use by generated code only.

Field Value
TypeDescription
UnknownFieldSet

Methods

getAllFields()

public Map<Descriptors.FieldDescriptor,Object> getAllFields()
Returns
TypeDescription
Map<FieldDescriptor,Object>

getDescriptorForType()

public Descriptors.Descriptor getDescriptorForType()
Returns
TypeDescription
Descriptors.Descriptor

getField(Descriptors.FieldDescriptor field)

public Object getField(Descriptors.FieldDescriptor field)
Parameter
NameDescription
fieldDescriptors.FieldDescriptor
Returns
TypeDescription
Object

getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof)

public Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof)

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

Parameter
NameDescription
oneofDescriptors.OneofDescriptor
Returns
TypeDescription
Descriptors.FieldDescriptor
Overrides

getParserForType()

public Parser<? extends GeneratedMessage> getParserForType()
Returns
TypeDescription
Parser<? extends com.google.protobuf.GeneratedMessage>

getRepeatedField(Descriptors.FieldDescriptor field, int index)

public Object getRepeatedField(Descriptors.FieldDescriptor field, int index)
Parameters
NameDescription
fieldDescriptors.FieldDescriptor
indexint
Returns
TypeDescription
Object

getRepeatedFieldCount(Descriptors.FieldDescriptor field)

public int getRepeatedFieldCount(Descriptors.FieldDescriptor field)
Parameter
NameDescription
fieldDescriptors.FieldDescriptor
Returns
TypeDescription
int

getSerializedSize()

public int getSerializedSize()

Get the number of bytes required to encode this message. The result is only computed on the first call and memoized after that.

If this message requires more than Integer.MAX_VALUE bytes to encode, the return value will be smaller than the actual number of bytes required and might be negative.

Returns
TypeDescription
int
Overrides

getUnknownFields()

public UnknownFieldSet getUnknownFields()
Returns
TypeDescription
UnknownFieldSet

hasField(Descriptors.FieldDescriptor field)

public boolean hasField(Descriptors.FieldDescriptor field)
Parameter
NameDescription
fieldDescriptors.FieldDescriptor
Returns
TypeDescription
boolean

hasOneof(Descriptors.OneofDescriptor oneof)

public boolean hasOneof(Descriptors.OneofDescriptor oneof)

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

Parameter
NameDescription
oneofDescriptors.OneofDescriptor
Returns
TypeDescription
boolean
Overrides

internalGetFieldAccessorTable()

protected abstract GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable()

Get the FieldAccessorTable for this type. We can't have the message class pass this in to the constructor because of bootstrapping trouble with DescriptorProtos.

Returns
TypeDescription
GeneratedMessage.FieldAccessorTable

internalGetMapField(int fieldNumber)

protected MapField internalGetMapField(int fieldNumber)

Gets the map field with the given field number. This method should be overridden in the generated message class if the message contains map fields.

Unlike other field types, reflection support for map fields can't be implemented based on generated public API because we need to access a map field as a list in reflection API but the generated API only allows us to access it as a map. This method returns the underlying map field directly and thus enables us to access the map field as a list.

Parameter
NameDescription
fieldNumberint
Returns
TypeDescription
MapField

isInitialized()

public boolean isInitialized()

Returns true if all required fields in the message and all embedded messages are set, false otherwise.

See also: MessageOrBuilder#getInitializationErrorString()

Returns
TypeDescription
boolean
Overrides

makeExtensionsImmutable()

protected void makeExtensionsImmutable()

Used by parsing constructors in generated classes.

newBuilderForType(AbstractMessage.BuilderParent parent)

protected Message.Builder newBuilderForType(AbstractMessage.BuilderParent parent)

Create a nested builder.

Parameter
NameDescription
parentAbstractMessage.BuilderParent
Returns
TypeDescription
Message.Builder
Overrides

newBuilderForType(GeneratedMessage.BuilderParent parent)

protected abstract Message.Builder newBuilderForType(GeneratedMessage.BuilderParent parent)

TODO(xiaofeng): remove this together with GeneratedMessage.BuilderParent.

Parameter
NameDescription
parentGeneratedMessage.BuilderParent
Returns
TypeDescription
Message.Builder

parseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields, ExtensionRegistryLite extensionRegistry, int tag)

protected boolean parseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields, ExtensionRegistryLite extensionRegistry, int tag)

Called by subclasses to parse an unknown field.

Parameters
NameDescription
inputCodedInputStream
unknownFieldsUnknownFieldSet.Builder
extensionRegistryExtensionRegistryLite
tagint
Returns
TypeDescription
boolean

true unless the tag is an end-group tag.

Exceptions
TypeDescription
IOException

writeReplace()

protected Object writeReplace()

Replaces this object in the output stream with a serialized form. Part of Java's serialization magic. Generated sub-classes must override this method by calling return super.writeReplace();

Returns
TypeDescription
Object

a SerializedForm of this message

Exceptions
TypeDescription
ObjectStreamException

writeTo(CodedOutputStream output)

public void writeTo(CodedOutputStream output)

Serializes the message and writes it to output. This does not flush or close the stream.

Parameter
NameDescription
outputCodedOutputStream
Overrides
Exceptions
TypeDescription
IOException