Class GeneratedMessage

All Implemented Interfaces:
Message, MessageLite, MessageLiteOrBuilder, MessageOrBuilder, Serializable
Direct Known Subclasses:
GeneratedMessage.ExtendableMessage

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.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • 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.
    • unknownFields

      protected UnknownFieldSet unknownFields
      For use by generated code only.
  • Constructor Details

    • GeneratedMessage

      protected GeneratedMessage()
    • GeneratedMessage

      protected GeneratedMessage(GeneratedMessage.Builder<?> builder)
  • Method Details

    • getParserForType

      public Parser<? extends GeneratedMessage> getParserForType()
      Description copied from interface: MessageLite
      Gets the parser for a message of the same type as this message.
      Specified by:
      getParserForType in interface Message
      Specified by:
      getParserForType in interface MessageLite
    • enableAlwaysUseFieldBuildersForTesting

      static void enableAlwaysUseFieldBuildersForTesting()
      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. See RepeatedFieldBuilder and SingleFieldBuilder.
    • 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.
    • getDescriptorForType

      public Descriptors.Descriptor getDescriptorForType()
      Description copied from interface: MessageOrBuilder
      Get the message's type's descriptor. This differs from the getDescriptor() method of generated message classes in that this method is an abstract method of the Message interface whereas getDescriptor() is a static method of a specific class. They return the same thing.
      Specified by:
      getDescriptorForType in interface MessageOrBuilder
    • getAllFieldsMutable

      private Map<Descriptors.FieldDescriptor,Object> getAllFieldsMutable(boolean getBytesForString)
      Internal helper to return a modifiable map containing all the fields. The returned Map is modifialbe so that the caller can add additional extension fields to implement getAllFields().
      Parameters:
      getBytesForString - whether to generate ByteString for string fields
    • isInitialized

      public boolean isInitialized()
      Description copied from interface: MessageLiteOrBuilder
      Returns true if all required fields in the message and all embedded messages are set, false otherwise.

      See also: MessageOrBuilder.getInitializationErrorString()

      Specified by:
      isInitialized in interface MessageLiteOrBuilder
      Overrides:
      isInitialized in class AbstractMessage
    • getAllFields

      public Map<Descriptors.FieldDescriptor,Object> getAllFields()
      Description copied from interface: MessageOrBuilder
      Returns a collection of all the fields in this message which are set and their corresponding values. A singular ("required" or "optional") field is set iff hasField() returns true for that field. A "repeated" field is set iff getRepeatedFieldCount() is greater than zero. The values are exactly what would be returned by calling MessageOrBuilder.getField(Descriptors.FieldDescriptor) for each field. The map is guaranteed to be a sorted map, so iterating over it will return fields in order by field number.
      If this is for a builder, the returned map may or may not reflect future changes to the builder. Either way, the returned map is itself unmodifiable.
      Specified by:
      getAllFields in interface MessageOrBuilder
    • getAllFieldsRaw

      Returns a collection of all the fields in this message which are set and their corresponding values. A singular ("required" or "optional") field is set iff hasField() returns true for that field. A "repeated" field is set iff getRepeatedFieldCount() is greater than zero. The values are exactly what would be returned by calling getFieldRaw(Descriptors.FieldDescriptor) for each field. The map is guaranteed to be a sorted map, so iterating over it will return fields in order by field number.
    • hasOneof

      public boolean hasOneof(Descriptors.OneofDescriptor oneof)
      Description copied from class: AbstractMessage
      TODO(jieluo): Clear it when all subclasses have implemented this method.
      Specified by:
      hasOneof in interface MessageOrBuilder
      Overrides:
      hasOneof in class AbstractMessage
    • getOneofFieldDescriptor

      public Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof)
      Description copied from class: AbstractMessage
      TODO(jieluo): Clear it when all subclasses have implemented this method.
      Specified by:
      getOneofFieldDescriptor in interface MessageOrBuilder
      Overrides:
      getOneofFieldDescriptor in class AbstractMessage
    • hasField

      public boolean hasField(Descriptors.FieldDescriptor field)
      Description copied from interface: MessageOrBuilder
      Returns true if the given field is set. This is exactly equivalent to calling the generated "has" accessor method corresponding to the field.
      Specified by:
      hasField in interface MessageOrBuilder
    • getField

      public Object getField(Descriptors.FieldDescriptor field)
      Description copied from interface: MessageOrBuilder
      Obtains the value of the given field, or the default value if it is not set. For primitive fields, the boxed primitive value is returned. For enum fields, the EnumValueDescriptor for the value is returned. For embedded message fields, the sub-message is returned. For repeated fields, a java.util.List is returned.
      Specified by:
      getField in interface MessageOrBuilder
    • getFieldRaw

      Object getFieldRaw(Descriptors.FieldDescriptor field)
      Obtains the value of the given field, or the default value if it is not set. For primitive fields, the boxed primitive value is returned. For enum fields, the EnumValueDescriptor for the value is returned. For embedded message fields, the sub-message is returned. For repeated fields, a java.util.List is returned. For present string fields, a ByteString is returned representing the bytes that the field contains.
    • getRepeatedFieldCount

      public int getRepeatedFieldCount(Descriptors.FieldDescriptor field)
      Description copied from interface: MessageOrBuilder
      Gets the number of elements of a repeated field. This is exactly equivalent to calling the generated "Count" accessor method corresponding to the field.
      Specified by:
      getRepeatedFieldCount in interface MessageOrBuilder
    • getRepeatedField

      public Object getRepeatedField(Descriptors.FieldDescriptor field, int index)
      Description copied from interface: MessageOrBuilder
      Gets an element of a repeated field. For primitive fields, the boxed primitive value is returned. For enum fields, the EnumValueDescriptor for the value is returned. For embedded message fields, the sub-message is returned.
      Specified by:
      getRepeatedField in interface MessageOrBuilder
    • getUnknownFields

      public UnknownFieldSet getUnknownFields()
      Description copied from interface: MessageOrBuilder
      Get the UnknownFieldSet for this message.
      Specified by:
      getUnknownFields in interface MessageOrBuilder
    • parseUnknownField

      protected boolean parseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields, ExtensionRegistryLite extensionRegistry, int tag) throws IOException
      Called by subclasses to parse an unknown field.
      Returns:
      true unless the tag is an end-group tag.
      Throws:
      IOException
    • parseWithIOException

      protected static <M extends Message> M parseWithIOException(Parser<M> parser, InputStream input) throws IOException
      Throws:
      IOException
    • parseWithIOException

      protected static <M extends Message> M parseWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions) throws IOException
      Throws:
      IOException
    • parseWithIOException

      protected static <M extends Message> M parseWithIOException(Parser<M> parser, CodedInputStream input) throws IOException
      Throws:
      IOException
    • parseWithIOException

      protected static <M extends Message> M parseWithIOException(Parser<M> parser, CodedInputStream input, ExtensionRegistryLite extensions) throws IOException
      Throws:
      IOException
    • parseDelimitedWithIOException

      protected static <M extends Message> M parseDelimitedWithIOException(Parser<M> parser, InputStream input) throws IOException
      Throws:
      IOException
    • parseDelimitedWithIOException

      protected static <M extends Message> M parseDelimitedWithIOException(Parser<M> parser, InputStream input, ExtensionRegistryLite extensions) throws IOException
      Throws:
      IOException
    • writeTo

      public void writeTo(CodedOutputStream output) throws IOException
      Description copied from interface: MessageLite
      Serializes the message and writes it to output. This does not flush or close the stream.
      Specified by:
      writeTo in interface MessageLite
      Overrides:
      writeTo in class AbstractMessage
      Throws:
      IOException
    • getSerializedSize

      public int getSerializedSize()
      Description copied from interface: MessageLite
      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.
      Specified by:
      getSerializedSize in interface MessageLite
      Overrides:
      getSerializedSize in class AbstractMessage
    • makeExtensionsImmutable

      protected void makeExtensionsImmutable()
      Used by parsing constructors in generated classes.
    • newBuilderForType

      protected abstract Message.Builder newBuilderForType(GeneratedMessage.BuilderParent parent)
      TODO(xiaofeng): remove this together with GeneratedMessage.BuilderParent.
    • newBuilderForType

      protected Message.Builder newBuilderForType(AbstractMessage.BuilderParent parent)
      Description copied from class: AbstractMessage
      Create a nested builder.
      Overrides:
      newBuilderForType in class AbstractMessage
    • newMessageScopedGeneratedExtension

      public static <ContainingType extends Message, Type> GeneratedMessage.GeneratedExtension<ContainingType,Type> newMessageScopedGeneratedExtension(Message scope, int descriptorIndex, Class singularType, Message defaultInstance)
      For use by generated code only.
    • newFileScopedGeneratedExtension

      public static <ContainingType extends Message, Type> GeneratedMessage.GeneratedExtension<ContainingType,Type> newFileScopedGeneratedExtension(Class singularType, Message defaultInstance)
      For use by generated code only.
    • newMessageScopedGeneratedExtension

      public static <ContainingType extends Message, Type> GeneratedMessage.GeneratedExtension<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.
    • newFileScopedGeneratedExtension

      public static <ContainingType extends Message, Type> GeneratedMessage.GeneratedExtension<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.
    • getMethodOrDie

      private static Method getMethodOrDie(Class clazz, String name, Class... params)
      Calls Class.getMethod and throws a RuntimeException if it fails.
    • invokeOrDie

      private static Object invokeOrDie(Method method, Object object, Object... params)
      Calls invoke and throws a RuntimeException if it fails.
    • internalGetMapField

      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.
    • writeReplace

      protected Object writeReplace() throws ObjectStreamException
      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:
      a SerializedForm of this message
      Throws:
      ObjectStreamException
    • checkNotLite

      private static <MessageType extends GeneratedMessage.ExtendableMessage<MessageType>, T> Extension<MessageType,T> checkNotLite(ExtensionLite<MessageType,T> extension)
      Checks that the Extension is non-Lite and returns it as a GeneratedMessage.GeneratedExtension.
    • computeStringSize

      protected static int computeStringSize(int fieldNumber, Object value)
    • computeStringSizeNoTag

      protected static int computeStringSizeNoTag(Object value)
    • writeString

      protected static void writeString(CodedOutputStream output, int fieldNumber, Object value) throws IOException
      Throws:
      IOException
    • writeStringNoTag

      protected static void writeStringNoTag(CodedOutputStream output, Object value) throws IOException
      Throws:
      IOException