Java Code Examples for com.google.protobuf.Descriptors.FieldDescriptor#isRepeated()

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#isRepeated() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 7 votes vote down vote up
/** Internal helper which returns a mutable map. */
private Map<FieldDescriptor, Object> getAllFieldsMutable() {
  final TreeMap<FieldDescriptor, Object> result =
    new TreeMap<FieldDescriptor, Object>();
  final Descriptor descriptor = internalGetFieldAccessorTable().descriptor;
  for (final FieldDescriptor field : descriptor.getFields()) {
    if (field.isRepeated()) {
      final List value = (List) getField(field);
      if (!value.isEmpty()) {
        result.put(field, value);
      }
    } else {
      if (hasField(field)) {
        result.put(field, getField(field));
      }
    }
  }
  return result;
}
 
Example 2
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/** Get the value of an extension. */
@Override
public final <Type> Type getExtension(final ExtensionLite<MessageType, Type> extensionLite) {
  Extension<MessageType, Type> extension = checkNotLite(extensionLite);

  verifyExtensionContainingType(extension);
  FieldDescriptor descriptor = extension.getDescriptor();
  final Object value = extensions.getField(descriptor);
  if (value == null) {
    if (descriptor.isRepeated()) {
      return (Type) Collections.emptyList();
    } else if (descriptor.getJavaType() ==
               FieldDescriptor.JavaType.MESSAGE) {
      return (Type) extension.getMessageDefaultInstance();
    } else {
      return (Type) extension.fromReflectionType(
          descriptor.getDefaultValue());
    }
  } else {
    return (Type) extension.fromReflectionType(value);
  }
}
 
Example 3
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get the value of an extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> Type getExtension(
    final GeneratedExtension<MessageType, Type> extension) {
  verifyExtensionContainingType(extension);
  FieldDescriptor descriptor = extension.getDescriptor();
  final Object value = extensions.getField(descriptor);
  if (value == null) {
    if (descriptor.isRepeated()) {
      return (Type) Collections.emptyList();
    } else if (descriptor.getJavaType() ==
               FieldDescriptor.JavaType.MESSAGE) {
      return (Type) extension.getMessageDefaultInstance();
    } else {
      return (Type) extension.fromReflectionType(
          descriptor.getDefaultValue());
    }
  } else {
    return (Type) extension.fromReflectionType(value);
  }
}
 
Example 4
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get the value of an extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> Type getExtension(
    final GeneratedExtension<MessageType, Type> extension) {
  verifyExtensionContainingType(extension);
  FieldDescriptor descriptor = extension.getDescriptor();
  final Object value = extensions.getField(descriptor);
  if (value == null) {
    if (descriptor.isRepeated()) {
      return (Type) Collections.emptyList();
    } else if (descriptor.getJavaType() ==
               FieldDescriptor.JavaType.MESSAGE) {
      return (Type) extension.getMessageDefaultInstance();
    } else {
      return (Type) extension.fromReflectionType(
          descriptor.getDefaultValue());
    }
  } else {
    return (Type) extension.fromReflectionType(value);
  }
}
 
Example 5
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convert from the type used by the reflection accessors to the type used
 * by native accessors.  E.g., for enums, the reflection accessors use
 * EnumValueDescriptors but the native accessors use the generated enum
 * type.
 */
@Override
@SuppressWarnings("unchecked")
protected Object fromReflectionType(final Object value) {
  FieldDescriptor descriptor = getDescriptor();
  if (descriptor.isRepeated()) {
    if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE ||
        descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) {
      // Must convert the whole list.
      final List result = new ArrayList();
      for (final Object element : (List) value) {
        result.add(singularFromReflectionType(element));
      }
      return result;
    } else {
      return value;
    }
  } else {
    return singularFromReflectionType(value);
  }
}
 
Example 6
Source File: DynamicMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public Object getField(FieldDescriptor field) {
  verifyContainingType(field);
  Object result = fields.getField(field);
  if (result == null) {
    if (field.isRepeated()) {
      result = Collections.emptyList();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      result = getDefaultInstance(field.getMessageType());
    } else {
      result = field.getDefaultValue();
    }
  }
  return result;
}
 
Example 7
Source File: RandomProtobufGenerator.java    From swellrt with Apache License 2.0 5 votes vote down vote up
private Object getRandomValue(FieldDescriptor field, double p) {
  if (field.isRepeated()) {
    List<Object> values = Lists.newArrayList();
    for (int i = 0, length = random.nextInt(MAX_LIST_LENGTH); i < length; i++) {
      values.add(getRandomSingleValue(field, p));
    }
    return values;
  } else {
    return getRandomSingleValue(field, p);
  }
}
 
Example 8
Source File: DynamicMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object getField(FieldDescriptor field) {
  verifyContainingType(field);
  Object result = fields.getField(field);
  if (result == null) {
    if (field.isRepeated()) {
      result = Collections.emptyList();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      result = getDefaultInstance(field.getMessageType());
    } else {
      result = field.getDefaultValue();
    }
  }
  return result;
}
 
Example 9
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public void writeUntil(final int end, final CodedOutputStream output)
                       throws IOException {
  while (next != null && next.getKey().getNumber() < end) {
    FieldDescriptor descriptor = next.getKey();
    if (messageSetWireFormat && descriptor.getLiteJavaType() ==
            WireFormat.JavaType.MESSAGE &&
        !descriptor.isRepeated()) {
      if (next instanceof LazyField.LazyEntry<?>) {
        output.writeRawMessageSetExtension(descriptor.getNumber(),
            ((LazyField.LazyEntry<?>) next).getField().toByteString());
      } else {
        output.writeMessageSetExtension(descriptor.getNumber(),
                                        (Message) next.getValue());
      }
    } else {
      // TODO(xiangl): Taken care of following code, it may cause
      // problem when we use LazyField for normal fields/extensions.
      // Due to the optional field can be duplicated at the end of
      // serialized bytes, which will make the serialized size change
      // after lazy field parsed. So when we use LazyField globally,
      // we need to change the following write method to write cached
      // bytes directly rather than write the parsed message.
      FieldSet.writeField(descriptor, next.getValue(), output);
    }
    if (iter.hasNext()) {
      next = iter.next();
    } else {
      next = null;
    }
  }
}
 
Example 10
Source File: TextFormatForTest.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a message into a string.
 */
public String printToString(MessageOrBuilder message) {
  StringBuilder result = new StringBuilder();
  for (FieldDescriptor field : getFieldsInNumberOrder(message.getDescriptorForType())) {

    // Skip empty fields.
    if ((field.isRepeated() && message.getRepeatedFieldCount(field) == 0)
        || (!field.isRepeated() && !message.hasField(field))) {
      continue;
    }

    // Normalize repeated and singleton fields.
    Object rawValue = message.getField(field);
    @SuppressWarnings("unchecked")
    List<Object> values =
        field.isMapField()
        ? sortMapEntries(field, rawValue)
        : field.isRepeated()
        ? (List<Object>) rawValue
        : ImmutableList.of(rawValue);

    // Print field values.
    for (Object value : values) {
      result.append(printFieldToString(field, value));
    }

  }
  return result.toString();
}
 
Example 11
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void setOption(Message.Builder builder, Option option, String expectedPrefix) {
  if (!option.getName().startsWith(expectedPrefix)) {
    return;
  }
  Descriptor descriptor = builder.getDescriptorForType();
  String optionName = option.getName().substring(expectedPrefix.length());
  FieldDescriptor optionField = descriptor.findFieldByName(optionName);
  if (optionField != null) {
    if (optionField.isRepeated()) {
      builder.addRepeatedField(optionField, fieldValueFrom(option.getValue(), optionField));
    } else {
      builder.setField(optionField, fieldValueFrom(option.getValue(), optionField));
    }
  }
}
 
Example 12
Source File: ProtoSchemaConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private <T> Builder<? extends Builder<?, GroupBuilder<T>>, GroupBuilder<T>> addField(FieldDescriptor descriptor, final GroupBuilder<T> builder) {
  if (descriptor.getJavaType() == JavaType.MESSAGE) {
    return addMessageField(descriptor, builder);
  }

  ParquetType parquetType = getParquetType(descriptor);
  if (descriptor.isRepeated() && parquetSpecsCompliant) {
    // the old schema style did not include the LIST wrapper around repeated fields
    return addRepeatedPrimitive(parquetType.primitiveType, parquetType.logicalTypeAnnotation, builder);
  }

  return builder.primitive(parquetType.primitiveType, getRepetition(descriptor)).as(parquetType.logicalTypeAnnotation);
}
 
Example 13
Source File: DynamicMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object getField(FieldDescriptor field) {
  verifyContainingType(field);
  Object result = fields.getField(field);
  if (result == null) {
    if (field.isRepeated()) {
      result = Collections.emptyList();
    } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      result = getDefaultInstance(field.getMessageType());
    } else {
      result = field.getDefaultValue();
    }
  }
  return result;
}
 
Example 14
Source File: TextFormat.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void printField(final FieldDescriptor field, final Object value,
    final TextGenerator generator) throws IOException {
  if (field.isRepeated()) {
    // Repeated field.  Print each element.
    for (Object element : (List<?>) value) {
      printSingleField(field, element, generator);
    }
  } else {
    printSingleField(field, value, generator);
  }
}
 
Example 15
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) &&
            !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }
  return true;
}
 
Example 16
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 17
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 5 votes vote down vote up
boolean compareFieldValueUsingParentFields(
    Message message1,
    Message message2,
    FieldDescriptor field,
    int index1,
    int index2,
    @Nullable Reporter reporter,
    List<SpecificField> stack) {
  FieldComparator.ComparisonResult result =
      fieldComparator.compare(
          message1, message2, field, index1, index2, ImmutableList.copyOf(stack));
  if (result == FieldComparator.ComparisonResult.RECURSE) {
    Preconditions.checkArgument(
        field.getJavaType() == JavaType.MESSAGE,
        "FieldComparator should not return RECURSE for fields not being submessages!");
    // Get the nested messages and compare them using one of the
    // methods.
    Message nextMessage1 =
        field.isRepeated()
            ? (Message) message1.getRepeatedField(field, index1)
            : (Message) message1.getField(field);
    Message nextMessage2 =
        field.isRepeated()
            ? (Message) message2.getRepeatedField(field, index2)
            : (Message) message2.getField(field);

    stack.add(
        field.isRepeated()
            ? SpecificField.forRepeatedField(field, index1, index2)
            : SpecificField.forField(field));
    boolean isSame = compare(nextMessage1, nextMessage2, reporter, stack);
    pop(stack);
    return isSame;
  }

  return result == FieldComparator.ComparisonResult.SAME;
}
 
Example 18
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public Object getField(final FieldDescriptor field) {
  Object object = internalGetFieldAccessorTable().getField(field).get(this);
  if (field.isRepeated()) {
    // The underlying list object is still modifiable at this point.
    // Make sure not to expose the modifiable list to the caller.
    return Collections.unmodifiableList((List) object);
  } else {
    return object;
  }
}
 
Example 19
Source File: GeneratedMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean isInitialized() {
  for (final FieldDescriptor field : getDescriptorForType().getFields()) {
    // Check that all required fields are present.
    if (field.isRequired()) {
      if (!hasField(field)) {
        return false;
      }
    }
    // Check that embedded messages are initialized.
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
      if (field.isRepeated()) {
        @SuppressWarnings("unchecked") final
        List<Message> messageList = (List<Message>) getField(field);
        for (final Message element : messageList) {
          if (!element.isInitialized()) {
            return false;
          }
        }
      } else {
        if (hasField(field) && !((Message) getField(field)).isInitialized()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 20
Source File: GeneratedMessage.java    From travelguide with Apache License 2.0 5 votes vote down vote up
public Object getField(final FieldDescriptor field) {
  Object object = internalGetFieldAccessorTable().getField(field).get(this);
  if (field.isRepeated()) {
    // The underlying list object is still modifiable at this point.
    // Make sure not to expose the modifiable list to the caller.
    return Collections.unmodifiableList((List) object);
  } else {
    return object;
  }
}