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

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#getNumber() . 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: AbstractMessage.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
protected static int hashFields(int hash, Map<FieldDescriptor, Object> map) {
  for (Map.Entry<FieldDescriptor, Object> entry : map.entrySet()) {
    FieldDescriptor field = entry.getKey();
    Object value = entry.getValue();
    hash = (37 * hash) + field.getNumber();
    if (field.getType() != FieldDescriptor.Type.ENUM){
      hash = (53 * hash) + value.hashCode();
    } else if (field.isRepeated()) {
      List<? extends EnumLite> list = (List<? extends EnumLite>) value;
      hash = (53 * hash) + Internal.hashEnumList(list);
    } else {
      hash = (53 * hash) + Internal.hashEnum((EnumLite) value);
    }
  }
  return hash;
}
 
Example 2
Source File: AbstractMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
protected int hashFields(int hash, Map<FieldDescriptor, Object> map) {
  for (Map.Entry<FieldDescriptor, Object> entry : map.entrySet()) {
    FieldDescriptor field = entry.getKey();
    Object value = entry.getValue();
    hash = (37 * hash) + field.getNumber();
    if (field.getType() != FieldDescriptor.Type.ENUM){
      hash = (53 * hash) + value.hashCode();
    } else if (field.isRepeated()) {
      List<? extends EnumLite> list = (List<? extends EnumLite>) value;
      hash = (53 * hash) + hashEnumList(list);
    } else {
      hash = (53 * hash) + hashEnum((EnumLite) value);
    }
  }
  return hash;
}
 
Example 3
Source File: JsonEncoder.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
/** Encode the given message as a JSON array. */
public JsonArray encode(Message message) {
  JsonArray array = new JsonArray();
  for (FieldDescriptor field : message.getDescriptorForType().getFields()) {
    if (field.isRepeated() || message.hasField(field)) {
      JsonElement element = encodeField(field, message.getField(field));
      if (!element.isJsonNull()) {
        while (array.size() < field.getNumber()) {
          array.add(JsonNull.INSTANCE);
        }
        array.set(field.getNumber() - 1, element);
      }
    }
  }
  return array;
}
 
Example 4
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testFindFieldByNumber() throws Exception {
  Descriptor descriptor = TypicalData.Builder.getDescriptor();
  Collection<FieldDescriptor> fields = descriptor.getFields();
  for (FieldDescriptor field : fields) {
    FieldDescriptor.Type type = field.getType();
    int fieldId = field.getNumber();
    switch (fieldId) {
      case 1:
        assertEquals(Type.INT32, type);
        break;
      case 2:
        assertEquals(Type.BYTES, type);
        break;
      case 3:
        assertEquals(Type.ENUM, type);
        break;
    }
    FieldDescriptor result = descriptor.findFieldByNumber(fieldId);
    assertEquals(field.getNumber(), result.getNumber());
    assertEquals(field.getName(), result.getName());
  }
}
 
Example 5
Source File: AbstractMessage.java    From travelguide with Apache License 2.0 6 votes vote down vote up
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
protected int hashFields(int hash, Map<FieldDescriptor, Object> map) {
  for (Map.Entry<FieldDescriptor, Object> entry : map.entrySet()) {
    FieldDescriptor field = entry.getKey();
    Object value = entry.getValue();
    hash = (37 * hash) + field.getNumber();
    if (field.getType() != FieldDescriptor.Type.ENUM){
      hash = (53 * hash) + value.hashCode();
    } else if (field.isRepeated()) {
      List<? extends EnumLite> list = (List<? extends EnumLite>) value;
      hash = (53 * hash) + hashEnumList(list);
    } else {
      hash = (53 * hash) + hashEnum((EnumLite) value);
    }
  }
  return hash;
}
 
Example 6
Source File: AbstractMessage.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
protected int hashFields(int hash, Map<FieldDescriptor, Object> map) {
  for (Map.Entry<FieldDescriptor, Object> entry : map.entrySet()) {
    FieldDescriptor field = entry.getKey();
    Object value = entry.getValue();
    hash = (37 * hash) + field.getNumber();
    if (field.getType() != FieldDescriptor.Type.ENUM){
      hash = (53 * hash) + value.hashCode();
    } else if (field.isRepeated()) {
      List<? extends EnumLite> list = (List<? extends EnumLite>) value;
      hash = (53 * hash) + hashEnumList(list);
    } else {
      hash = (53 * hash) + hashEnum((EnumLite) value);
    }
  }
  return hash;
}
 
Example 7
Source File: AbstractMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
protected static int hashFields(int hash, Map<FieldDescriptor, Object> map) {
  for (Map.Entry<FieldDescriptor, Object> entry : map.entrySet()) {
    FieldDescriptor field = entry.getKey();
    Object value = entry.getValue();
    hash = (37 * hash) + field.getNumber();
    if (field.isMapField()) {
      hash = (53 * hash) + hashMapField(value);
    } else if (field.getType() != FieldDescriptor.Type.ENUM){
      hash = (53 * hash) + value.hashCode();
    } else if (field.isRepeated()) {
      List<? extends EnumLite> list = (List<? extends EnumLite>) value;
      hash = (53 * hash) + Internal.hashEnumList(list);
    } else {
      hash = (53 * hash) + Internal.hashEnum((EnumLite) value);
    }
  }
  return hash;
}
 
Example 8
Source File: MapEntry.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Builder<K, V> setField(FieldDescriptor field, Object value) {
  checkFieldDescriptor(field);
  if (field.getNumber() == 1) {
    setKey((K) value);
  } else {
    if (field.getType() == FieldDescriptor.Type.ENUM) {
      value = ((EnumValueDescriptor) value).getNumber();
    }
    setValue((V) value);
  }
  return this;
}
 
Example 9
Source File: MapEntry.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Builder<K, V> clearField(FieldDescriptor field) {
  checkFieldDescriptor(field);
  if (field.getNumber() == 1) {
    clearKey();
  } else {
    clearValue();
  }
  return this;
}
 
Example 10
Source File: MapEntry.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Message.Builder newBuilderForField(FieldDescriptor field) {
  checkFieldDescriptor(field);;
  // This method should be called for message fields and in a MapEntry
  // message only the value field can possibly be a message field.
  if (field.getNumber() != 2
      || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) {
    throw new RuntimeException(
        "\"" + field.getFullName() + "\" is not a message value field.");
  }
  return ((Message) value).newBuilderForType();
}
 
Example 11
Source File: MapEntry.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object getField(FieldDescriptor field) {
  checkFieldDescriptor(field);
  Object result = field.getNumber() == 1 ? getKey() : getValue();
  // Convert enums to EnumValueDescriptor.
  if (field.getType() == FieldDescriptor.Type.ENUM) {
    result = field.getEnumType().findValueByNumberCreatingIfUnknown(
        (java.lang.Integer) result);
  }
  return result;
}
 
Example 12
Source File: ProtoReflectionService.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void processExtension(FieldDescriptor extension, FileDescriptor fd) {
  String extensionName = extension.getContainingType().getFullName();
  int extensionNumber = extension.getNumber();
  if (!fileDescriptorsByExtensionAndNumber.containsKey(extensionName)) {
    fileDescriptorsByExtensionAndNumber.put(
        extensionName, new HashMap<Integer, FileDescriptor>());
  }
  checkState(
      !fileDescriptorsByExtensionAndNumber.get(extensionName).containsKey(extensionNumber),
      "Extension name and number already defined: %s, %s",
      extensionName,
      extensionNumber);
  fileDescriptorsByExtensionAndNumber.get(extensionName).put(extensionNumber, fd);
}
 
Example 13
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Object getField(FieldDescriptor field) {
    checkFieldDescriptor(field);
    Object result = field.getNumber() == 1 ? getKey() : getValue();
    // Convert enums to EnumValueDescriptor.
    if (field.getType() == FieldDescriptor.Type.ENUM) {
        result = field.getEnumType().findValueByNumberCreatingIfUnknown((java.lang.Integer) result);
    }
    return result;
}
 
Example 14
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Message.Builder newBuilderForField(FieldDescriptor field) {
    checkFieldDescriptor(field);
    ;
    // This method should be called for message fields and in a MapEntry
    // message only the value field can possibly be a message field.
    if (field.getNumber() != 2 || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) {
        throw new RuntimeException("\"" + field.getFullName() + "\" is not a message value field.");
    }
    return ((Message) value).newBuilderForType();
}
 
Example 15
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Builder<K, V> setField(FieldDescriptor field, Object value) {
    checkFieldDescriptor(field);
    if (field.getNumber() == 1) {
        setKey((K) value);
    } else {
        if (field.getType() == FieldDescriptor.Type.ENUM) {
            value = ((EnumValueDescriptor) value).getNumber();
        }
        setValue((V) value);
    }
    return this;
}
 
Example 16
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Builder<K, V> clearField(FieldDescriptor field) {
    checkFieldDescriptor(field);
    if (field.getNumber() == 1) {
        clearKey();
    } else {
        clearValue();
    }
    return this;
}
 
Example 17
Source File: MapEntry.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Object getField(FieldDescriptor field) {
    checkFieldDescriptor(field);
    Object result = field.getNumber() == 1 ? getKey() : getValue();
    // Convert enums to EnumValueDescriptor.
    if (field.getType() == FieldDescriptor.Type.ENUM) {
        result = field.getEnumType().findValueByNumberCreatingIfUnknown((Integer) result);
    }
    return result;
}
 
Example 18
Source File: ProtoReflectionService.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void processExtension(FieldDescriptor extension, FileDescriptor fd) {
  String extensionName = extension.getContainingType().getFullName();
  int extensionNumber = extension.getNumber();
  if (!fileDescriptorsByExtensionAndNumber.containsKey(extensionName)) {
    fileDescriptorsByExtensionAndNumber.put(
        extensionName, new HashMap<Integer, FileDescriptor>());
  }
  checkState(
      !fileDescriptorsByExtensionAndNumber.get(extensionName).containsKey(extensionNumber),
      "Extension name and number already defined: %s, %s",
      extensionName,
      extensionNumber);
  fileDescriptorsByExtensionAndNumber.get(extensionName).put(extensionNumber, fd);
}
 
Example 19
Source File: MessageDifferencer.java    From startup-os with Apache License 2.0 5 votes vote down vote up
private boolean fieldBefore(FieldDescriptor field1, FieldDescriptor field2) {
  if (field1 == null) {
    return false;
  }
  if (field2 == null) {
    return true;
  }
  return field1.getNumber() < field2.getNumber();
}
 
Example 20
Source File: MetaDataEvolutionValidator.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
/**
 * Validate that the record types have all been evolved in a legal way. In particular, this makes sure that
 * each record type defined in the union descriptor is in the new union descriptor in the correct
 * place. It will then verify that each message type has been updated in a legal way, i.e., that it only
 * includes new fields.
 *
 * @param oldUnionDescriptor the union descriptor for the existing meta-data for some record store
 * @param newUnionDescriptor the new proposed meta-data
 */
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public void validateUnion(@Nonnull Descriptor oldUnionDescriptor, @Nonnull Descriptor newUnionDescriptor) {
    if (oldUnionDescriptor == newUnionDescriptor) {
        // Don't bother validating the record types if they are all the same.
        return;
    }
    final BiMap<Descriptor, Descriptor> updatedDescriptors = HashBiMap.create(oldUnionDescriptor.getFields().size());
    final Set<Pair<Descriptor, Descriptor>> seenDescriptors = new HashSet<>();

    for (FieldDescriptor oldUnionField : oldUnionDescriptor.getFields()) {
        if (!oldUnionField.getType().equals(FieldDescriptor.Type.MESSAGE)) {
            throw new MetaDataException("field in union is not a message type", LogMessageKeys.FIELD_NAME, oldUnionField.getName());
        }
        int fieldNumber = oldUnionField.getNumber();
        FieldDescriptor newUnionField = newUnionDescriptor.findFieldByNumber(fieldNumber);
        if (newUnionField != null) {
            if (!newUnionField.getType().equals(FieldDescriptor.Type.MESSAGE)) {
                throw new MetaDataException("field in new union is not a message type", LogMessageKeys.FIELD_NAME, newUnionField.getName());
            }
            Descriptor oldRecord = oldUnionField.getMessageType();
            Descriptor newRecord = newUnionField.getMessageType();

            // Verify that all fields of the same type in the old union are also of the same type
            // in the new union (i.e., that there are no "splits" or "merges" of record types).
            Descriptor alreadySeenNewRecord = updatedDescriptors.get(oldRecord);
            if (alreadySeenNewRecord != null) {
                if (alreadySeenNewRecord != newRecord) {
                    // A "split" -- the same type in the old union points to two different types in the new union
                    throw new MetaDataException("record type corresponds to multiple types in new meta-data",
                            LogMessageKeys.OLD_RECORD_TYPE, oldRecord.getName(),
                            LogMessageKeys.NEW_RECORD_TYPE, newRecord.getName() + " & " + alreadySeenNewRecord.getName());
                }
            } else {
                if (updatedDescriptors.containsValue(newRecord)) {
                    // A "merge" -- two different types in the old union point to the same type in the new union
                    final Descriptor alreadySeenOldRecord = updatedDescriptors.inverse().get(newRecord);
                    throw new MetaDataException("record type corresponds to multiple types in old meta-data",
                            LogMessageKeys.OLD_RECORD_TYPE, oldRecord.getName() + " & " + alreadySeenOldRecord.getName(),
                            LogMessageKeys.NEW_RECORD_TYPE, newRecord.getName());
                }
            }
            updatedDescriptors.put(oldRecord, newRecord);

            // Validate the form of the old and new record types
            validateMessage(oldRecord, newRecord, seenDescriptors);
        } else {
            throw new MetaDataException("record type removed from union", LogMessageKeys.RECORD_TYPE, oldUnionField.getMessageType());
        }
    }
}