/** helper method to handle {@code builder} and {@code extensions}. */
 private static Message getOriginalMessage(
     Message.Builder builder, FieldSet<FieldDescriptor> extensions, FieldDescriptor field) {
   if (builder != null) {
     return (Message) builder.getField(field);
   } else {
     return (Message) extensions.getField(field);
   }
 }
예제 #2
0
 /** Get the value of an extension. */
 @SuppressWarnings("unchecked")
 public final <Type> Type getExtension(final GeneratedExtension<MessageType, Type> extension) {
   verifyExtensionContainingType(extension);
   final Object value = extensions.getField(extension.descriptor);
   if (value == null) {
     return extension.defaultValue;
   } else {
     return (Type) value;
   }
 }
 public Object getField(FieldDescriptor field) {
   verifyContainingType(field);
   Object result = fields.getField(field);
   if (result == null) {
     if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
       result = getDefaultInstance(field.getMessageType());
     } else {
       result = field.getDefaultValue();
     }
   }
   return result;
 }
예제 #4
0
    /**
     * Called by subclasses to parse an unknown field or an extension.
     *
     * @return {@code true} unless the tag is an end-group tag.
     */
    @Override
    protected boolean parseUnknownField(
        final CodedInputStream input, final ExtensionRegistryLite extensionRegistry, final int tag)
        throws IOException {
      final FieldSet<ExtensionDescriptor> extensions =
          ((ExtendableMessage) internalGetResult()).extensions;

      final int wireType = WireFormat.getTagWireType(tag);
      final int fieldNumber = WireFormat.getTagFieldNumber(tag);

      final GeneratedExtension<MessageType, ?> extension =
          extensionRegistry.findLiteExtensionByNumber(getDefaultInstanceForType(), fieldNumber);

      boolean unknown = false;
      boolean packed = false;
      if (extension == null) {
        unknown = true; // Unknown field.
      } else if (wireType
          == FieldSet.getWireFormatForFieldType(
              extension.descriptor.getLiteType(), false /* isPacked */)) {
        packed = false; // Normal, unpacked value.
      } else if (extension.descriptor.isRepeated
          && extension.descriptor.type.isPackable()
          && wireType
              == FieldSet.getWireFormatForFieldType(
                  extension.descriptor.getLiteType(), true /* isPacked */)) {
        packed = true; // Packed value.
      } else {
        unknown = true; // Wrong wire type.
      }

      if (unknown) { // Unknown field or wrong wire type.  Skip.
        return input.skipField(tag);
      }

      if (packed) {
        final int length = input.readRawVarint32();
        final int limit = input.pushLimit(length);
        if (extension.descriptor.getLiteType() == WireFormat.FieldType.ENUM) {
          while (input.getBytesUntilLimit() > 0) {
            final int rawValue = input.readEnum();
            final Object value = extension.descriptor.getEnumType().findValueByNumber(rawValue);
            if (value == null) {
              // If the number isn't recognized as a valid value for this
              // enum, drop it (don't even add it to unknownFields).
              return true;
            }
            extensions.addRepeatedField(extension.descriptor, value);
          }
        } else {
          while (input.getBytesUntilLimit() > 0) {
            final Object value =
                FieldSet.readPrimitiveField(input, extension.descriptor.getLiteType());
            extensions.addRepeatedField(extension.descriptor, value);
          }
        }
        input.popLimit(limit);
      } else {
        final Object value;
        switch (extension.descriptor.getLiteJavaType()) {
          case MESSAGE:
            {
              MessageLite.Builder subBuilder = null;
              if (!extension.descriptor.isRepeated()) {
                MessageLite existingValue = (MessageLite) extensions.getField(extension.descriptor);
                if (existingValue != null) {
                  subBuilder = existingValue.toBuilder();
                }
              }
              if (subBuilder == null) {
                subBuilder = extension.messageDefaultInstance.newBuilderForType();
              }
              if (extension.descriptor.getLiteType() == WireFormat.FieldType.GROUP) {
                input.readGroup(extension.getNumber(), subBuilder, extensionRegistry);
              } else {
                input.readMessage(subBuilder, extensionRegistry);
              }
              value = subBuilder.build();
              break;
            }
          case ENUM:
            final int rawValue = input.readEnum();
            value = extension.descriptor.getEnumType().findValueByNumber(rawValue);
            // If the number isn't recognized as a valid value for this enum,
            // drop it.
            if (value == null) {
              return true;
            }
            break;
          default:
            value = FieldSet.readPrimitiveField(input, extension.descriptor.getLiteType());
            break;
        }

        if (extension.descriptor.isRepeated()) {
          extensions.addRepeatedField(extension.descriptor, value);
        } else {
          extensions.setField(extension.descriptor, value);
        }
      }

      return true;
    }