예제 #1
0
 @Override
 public Builder mergeFrom(Message other) {
   if (other instanceof DynamicMessage) {
     // This should be somewhat faster than calling super.mergeFrom().
     DynamicMessage otherDynamicMessage = (DynamicMessage) other;
     if (otherDynamicMessage.type != type) {
       throw new IllegalArgumentException(
           "mergeFrom(Message) can only merge messages of the same type.");
     }
     ensureIsMutable();
     fields.mergeFrom(otherDynamicMessage.fields);
     mergeUnknownFields(otherDynamicMessage.unknownFields);
     for (int i = 0; i < oneofCases.length; i++) {
       if (oneofCases[i] == null) {
         oneofCases[i] = otherDynamicMessage.oneofCases[i];
       } else {
         if ((otherDynamicMessage.oneofCases[i] != null)
             && (oneofCases[i] != otherDynamicMessage.oneofCases[i])) {
           fields.clearField(oneofCases[i]);
           oneofCases[i] = otherDynamicMessage.oneofCases[i];
         }
       }
     }
     return this;
   } else {
     return super.mergeFrom(other);
   }
 }
 public void writeTo(CodedOutputStream output) throws IOException {
   if (type.getOptions().getMessageSetWireFormat()) {
     fields.writeMessageSetTo(output);
     unknownFields.writeAsMessageSetTo(output);
   } else {
     fields.writeTo(output);
     unknownFields.writeTo(output);
   }
 }
예제 #3
0
 @Override
 public Builder clear() {
   if (fields.isImmutable()) {
     fields = FieldSet.newFieldSet();
   } else {
     fields.clear();
   }
   unknownFields = UnknownFieldSet.getDefaultInstance();
   return this;
 }
예제 #4
0
  static boolean isInitialized(Descriptor type, FieldSet<FieldDescriptor> fields) {
    // Check that all required fields are present.
    for (final FieldDescriptor field : type.getFields()) {
      if (field.isRequired()) {
        if (!fields.hasField(field)) {
          return false;
        }
      }
    }

    // Check that embedded messages are initialized.
    return fields.isInitialized();
  }
  public int getSerializedSize() {
    int size = memoizedSize;
    if (size != -1) return size;

    if (type.getOptions().getMessageSetWireFormat()) {
      size = fields.getMessageSetSerializedSize();
      size += unknownFields.getSerializedSizeAsMessageSet();
    } else {
      size = fields.getSerializedSize();
      size += unknownFields.getSerializedSize();
    }

    memoizedSize = size;
    return size;
  }
  public int getSerializedSize() {
    int size = memoizedSize;
    if (size != -1) {
      return size;
    }

    size = 0;
    final boolean isMessageSet = getDescriptorForType().getOptions().getMessageSetWireFormat();

    for (final Map.Entry<FieldDescriptor, Object> entry : getAllFields().entrySet()) {
      final FieldDescriptor field = entry.getKey();
      final Object value = entry.getValue();
      if (isMessageSet
          && field.isExtension()
          && field.getType() == FieldDescriptor.Type.MESSAGE
          && !field.isRepeated()) {
        size +=
            CodedOutputStream.computeMessageSetExtensionSize(field.getNumber(), (Message) value);
      } else {
        size += FieldSet.computeFieldSize(field, value);
      }
    }

    final UnknownFieldSet unknownFields = getUnknownFields();
    if (isMessageSet) {
      size += unknownFields.getSerializedSizeAsMessageSet();
    } else {
      size += unknownFields.getSerializedSize();
    }

    memoizedSize = size;
    return size;
  }
예제 #7
0
 /**
  * Clones the FieldSet. The returned FieldSet will be mutable even if the original FieldSet was
  * immutable.
  *
  * @return the newly cloned FieldSet
  */
 @Override
 public FieldSet<FieldDescriptorType> clone() {
   // We can't just call fields.clone because List objects in the map
   // should not be shared.
   FieldSet<FieldDescriptorType> clone = FieldSet.newFieldSet();
   for (int i = 0; i < fields.getNumArrayEntries(); i++) {
     Map.Entry<FieldDescriptorType, Object> entry = fields.getArrayEntryAt(i);
     FieldDescriptorType descriptor = entry.getKey();
     clone.setField(descriptor, entry.getValue());
   }
   for (Map.Entry<FieldDescriptorType, Object> entry : fields.getOverflowEntries()) {
     FieldDescriptorType descriptor = entry.getKey();
     clone.setField(descriptor, entry.getValue());
   }
   return clone;
 }
    private static void eagerlyMergeMessageSetExtension(
        CodedInputStream input,
        ExtensionRegistry.ExtensionInfo extension,
        ExtensionRegistryLite extensionRegistry,
        Message.Builder builder,
        FieldSet<FieldDescriptor> extensions)
        throws IOException {

      FieldDescriptor field = extension.descriptor;
      Message value = null;
      if (hasOriginalMessage(builder, extensions, field)) {
        Message originalMessage = getOriginalMessage(builder, extensions, field);
        Message.Builder subBuilder = originalMessage.toBuilder();
        input.readMessage(subBuilder, extensionRegistry);
        value = subBuilder.buildPartial();
      } else {
        value = input.readMessage(extension.defaultInstance.getParserForType(), extensionRegistry);
      }

      if (builder != null) {
        builder.setField(field, value);
      } else {
        extensions.setField(field, value);
      }
    }
예제 #9
0
 public DynamicMessage buildPartial() {
   fields.makeImmutable();
   DynamicMessage result =
       new DynamicMessage(
           type, fields, java.util.Arrays.copyOf(oneofCases, oneofCases.length), unknownFields);
   return result;
 }
 public Builder clear() {
   if (fields == null) {
     throw new IllegalStateException("Cannot call clear() after build().");
   }
   fields.clear();
   return this;
 }
 /** 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);
   }
 }
 /** helper method to handle {@code builder} and {@code extensions}. */
 private static boolean hasOriginalMessage(
     Message.Builder builder, FieldSet<FieldDescriptor> extensions, FieldDescriptor field) {
   if (builder != null) {
     return builder.hasField(field);
   } else {
     return extensions.hasField(field);
   }
 }
예제 #13
0
 /** Get a {@code DynamicMessage} representing the default instance of the given type. */
 public static DynamicMessage getDefaultInstance(Descriptor type) {
   int oneofDeclCount = type.toProto().getOneofDeclCount();
   FieldDescriptor[] oneofCases = new FieldDescriptor[oneofDeclCount];
   return new DynamicMessage(
       type,
       FieldSet.<FieldDescriptor>emptySet(),
       oneofCases,
       UnknownFieldSet.getDefaultInstance());
 }
예제 #14
0
 public Builder setField(FieldDescriptor field, Object value) {
   verifyContainingType(field);
   ensureIsMutable();
   if (field.getType() == FieldDescriptor.Type.ENUM) {
     verifyEnumType(field, value);
   }
   OneofDescriptor oneofDescriptor = field.getContainingOneof();
   if (oneofDescriptor != null) {
     int index = oneofDescriptor.getIndex();
     FieldDescriptor oldField = oneofCases[index];
     if ((oldField != null) && (oldField != field)) {
       fields.clearField(oldField);
     }
     oneofCases[index] = field;
   }
   fields.setField(field, value);
   return this;
 }
 public DynamicMessage buildPartial() {
   if (fields == null) {
     throw new IllegalStateException("build() has already been called on this Builder.");
   }
   fields.makeImmutable();
   DynamicMessage result = new DynamicMessage(type, fields, unknownFields);
   fields = null;
   unknownFields = null;
   return result;
 }
예제 #16
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;
   }
 }
 /** helper method to handle {@code builder} and {@code extensions}. */
 private static void setField(
     Message.Builder builder,
     FieldSet<FieldDescriptor> extensions,
     FieldDescriptor field,
     Object value) {
   if (builder != null) {
     builder.setField(field, value);
   } else {
     extensions.setField(field, 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;
 }
예제 #19
0
 public Builder clearField(FieldDescriptor field) {
   verifyContainingType(field);
   ensureIsMutable();
   OneofDescriptor oneofDescriptor = field.getContainingOneof();
   if (oneofDescriptor != null) {
     int index = oneofDescriptor.getIndex();
     if (oneofCases[index] == field) {
       oneofCases[index] = null;
     }
   }
   fields.clearField(field);
   return this;
 }
 public Builder mergeFrom(Message other) {
   if (other instanceof DynamicMessage) {
     // This should be somewhat faster than calling super.mergeFrom().
     DynamicMessage otherDynamicMessage = (DynamicMessage) other;
     if (otherDynamicMessage.type != type) {
       throw new IllegalArgumentException(
           "mergeFrom(Message) can only merge messages of the same type.");
     }
     fields.mergeFrom(otherDynamicMessage.fields);
     mergeUnknownFields(otherDynamicMessage.unknownFields);
     return this;
   } else {
     return super.mergeFrom(other);
   }
 }
예제 #21
0
 public void writeUntil(final int end, final CodedOutputStream output) throws IOException {
   while (next != null && next.getKey().getNumber() < end) {
     ExtensionDescriptor extension = next.getKey();
     if (messageSetWireFormat
         && extension.getLiteJavaType() == WireFormat.JavaType.MESSAGE
         && !extension.isRepeated()) {
       output.writeMessageSetExtension(extension.getNumber(), (MessageLite) next.getValue());
     } else {
       FieldSet.writeField(extension, next.getValue(), output);
     }
     if (iter.hasNext()) {
       next = iter.next();
     } else {
       next = null;
     }
   }
 }
    private static void mergeMessageSetExtensionFromBytes(
        ByteString rawBytes,
        ExtensionRegistry.ExtensionInfo extension,
        ExtensionRegistryLite extensionRegistry,
        Message.Builder builder,
        FieldSet<FieldDescriptor> extensions)
        throws IOException {

      FieldDescriptor field = extension.descriptor;
      boolean hasOriginalValue = hasOriginalMessage(builder, extensions, field);

      if (hasOriginalValue || ExtensionRegistryLite.isEagerlyParseMessageSets()) {
        // If the field already exists, we just parse the field.
        Message value = null;
        if (hasOriginalValue) {
          Message originalMessage = getOriginalMessage(builder, extensions, field);
          Message.Builder subBuilder = originalMessage.toBuilder();
          subBuilder.mergeFrom(rawBytes, extensionRegistry);
          value = subBuilder.buildPartial();
        } else {
          value =
              extension
                  .defaultInstance
                  .getParserForType()
                  .parsePartialFrom(rawBytes, extensionRegistry);
        }
        setField(builder, extensions, field, value);
      } else {
        // Use LazyField to load MessageSet lazily.
        LazyField lazyField = new LazyField(extension.defaultInstance, extensionRegistry, rawBytes);
        if (builder != null) {
          // TODO(xiangl): it looks like this method can only be invoked by
          // ExtendableBuilder, but I'm not sure. So I double check the type of
          // builder here. It may be useless and need more investigation.
          if (builder instanceof ExtendableBuilder) {
            builder.setField(field, lazyField);
          } else {
            builder.setField(field, lazyField.getValue());
          }
        } else {
          extensions.setField(field, lazyField);
        }
      }
    }
예제 #23
0
  public void writeTo(final CodedOutputStream output) throws IOException {
    final boolean isMessageSet = getDescriptorForType().getOptions().getMessageSetWireFormat();

    for (final Map.Entry<FieldDescriptor, Object> entry : getAllFields().entrySet()) {
      final FieldDescriptor field = entry.getKey();
      final Object value = entry.getValue();
      if (isMessageSet
          && field.isExtension()
          && field.getType() == FieldDescriptor.Type.MESSAGE
          && !field.isRepeated()) {
        output.writeMessageSetExtension(field.getNumber(), (Message) value);
      } else {
        FieldSet.writeField(field, value, output);
      }
    }

    final UnknownFieldSet unknownFields = getUnknownFields();
    if (isMessageSet) {
      unknownFields.writeAsMessageSetTo(output);
    } else {
      unknownFields.writeTo(output);
    }
  }
예제 #24
0
 public Map<FieldDescriptor, Object> getAllFields() {
   return fields.getAllFields();
 }
예제 #25
0
 private void ensureIsMutable() {
   if (fields.isImmutable()) {
     fields = fields.clone();
   }
 }
예제 #26
0
 public Builder addRepeatedField(FieldDescriptor field, Object value) {
   verifyContainingType(field);
   ensureIsMutable();
   fields.addRepeatedField(field, value);
   return this;
 }
예제 #27
0
 public Object getRepeatedField(FieldDescriptor field, int index) {
   verifyContainingType(field);
   return fields.getRepeatedField(field, index);
 }
예제 #28
0
 public int getRepeatedFieldCount(FieldDescriptor field) {
   verifyContainingType(field);
   return fields.getRepeatedFieldCount(field);
 }
예제 #29
0
 /** Construct a {@code Builder} for the given type. */
 private Builder(Descriptor type) {
   this.type = type;
   this.fields = FieldSet.newFieldSet();
   this.unknownFields = UnknownFieldSet.getDefaultInstance();
   this.oneofCases = new FieldDescriptor[type.toProto().getOneofDeclCount()];
 }
예제 #30
0
 public boolean hasField(FieldDescriptor field) {
   verifyContainingType(field);
   return fields.hasField(field);
 }