Java 类com.google.protobuf.Descriptors.FieldDescriptor 实例源码

项目:seldon-core    文件:JsonFormat.java   
private void mergeMessage(JsonElement json, Message.Builder builder, boolean skipTypeUrl)
    throws InvalidProtocolBufferException {
  if (!(json instanceof JsonObject)) {
    throw new InvalidProtocolBufferException("Expect message object but got: " + json);
  }
  JsonObject object = (JsonObject) json;
  Map<String, FieldDescriptor> fieldNameMap = getFieldNameMap(builder.getDescriptorForType());
  for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
    if (skipTypeUrl && entry.getKey().equals("@type")) {
      continue;
    }
    FieldDescriptor field = fieldNameMap.get(entry.getKey());
    if (field == null) {
      if (ignoringUnknownFields) {
        continue;
      }
      throw new InvalidProtocolBufferException(
          "Cannot find field: "
              + entry.getKey()
              + " in message "
              + builder.getDescriptorForType().getFullName());
    }
    mergeField(field, entry.getValue(), builder);
  }
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeMapField(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (!(json instanceof JsonObject)) {
    throw new InvalidProtocolBufferException("Expect a map object but found: " + json);
  }
  Descriptor type = field.getMessageType();
  FieldDescriptor keyField = type.findFieldByName("key");
  FieldDescriptor valueField = type.findFieldByName("value");
  if (keyField == null || valueField == null) {
    throw new InvalidProtocolBufferException("Invalid map field: " + field.getFullName());
  }
  JsonObject object = (JsonObject) json;
  for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
    Message.Builder entryBuilder = builder.newBuilderForField(field);
    Object key = parseFieldValue(keyField, new JsonPrimitive(entry.getKey()), entryBuilder);
    Object value = parseFieldValue(valueField, entry.getValue(), entryBuilder);
    if (value == null) {
      throw new InvalidProtocolBufferException("Map value cannot be null.");
    }
    entryBuilder.setField(keyField, key);
    entryBuilder.setField(valueField, value);
    builder.addRepeatedField(field, entryBuilder.build());
  }
}
项目:rejoiner    文件:ProtoToGql.java   
@Override
public GraphQLFieldDefinition apply(FieldDescriptor fieldDescriptor) {
  GraphQLFieldDefinition.Builder builder =
      GraphQLFieldDefinition.newFieldDefinition()
          .type(convertType(fieldDescriptor))
          .dataFetcher(
              new ProtoDataFetcher(UNDERSCORE_TO_CAMEL.convert(fieldDescriptor.getName())))
          .name(UNDERSCORE_TO_CAMEL.convert(fieldDescriptor.getName()));
  if (fieldDescriptor.getFile().toProto().getSourceCodeInfo().getLocationCount()
      > fieldDescriptor.getIndex()) {
    builder.description(
        fieldDescriptor
            .getFile()
            .toProto()
            .getSourceCodeInfo()
            .getLocation(fieldDescriptor.getIndex())
            .getLeadingComments());
  }
  if (fieldDescriptor.getOptions().hasDeprecated()
      && fieldDescriptor.getOptions().getDeprecated()) {
    builder.deprecate("deprecated in proto");
  }
  return builder.build();
}
项目:sstore-soft    文件:GeneratedMessage.java   
@Override
public Object getField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    final Object value = extensions.getField(field);
    if (value == null) {
      if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        // Lacking an ExtensionRegistry, we have no way to determine the
        // extension's real type, so we return a DynamicMessage.
        return DynamicMessage.getDefaultInstance(field.getMessageType());
      } else {
        return field.getDefaultValue();
      }
    } else {
      return value;
    }
  } else {
    return super.getField(field);
  }
}
项目:s-store    文件:GeneratedMessage.java   
/**
 * 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.
 */
@SuppressWarnings("unchecked")
private Object fromReflectionType(final Object value) {
  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);
  }
}
项目:s-store    文件:AbstractMessage.java   
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);
  }
}
项目:s-store    文件:GeneratedMessage.java   
@Override
public Object getField(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    final Object value = extensions.getField(field);
    if (value == null) {
      if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        // Lacking an ExtensionRegistry, we have no way to determine the
        // extension's real type, so we return a DynamicMessage.
        return DynamicMessage.getDefaultInstance(field.getMessageType());
      } else {
        return field.getDefaultValue();
      }
    } else {
      return value;
    }
  } else {
    return super.getField(field);
  }
}
项目:sstore-soft    文件:GeneratedMessage.java   
/**
 * Convert from the type used by the native accessors to the type used
 * by reflection accessors.  E.g., for enums, the reflection accessors use
 * EnumValueDescriptors but the native accessors use the generated enum
 * type.
 */
@SuppressWarnings("unchecked")
private Object toReflectionType(final Object value) {
  if (descriptor.isRepeated()) {
    if (descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) {
      // Must convert the whole list.
      final List result = new ArrayList();
      for (final Object element : (List) value) {
        result.add(singularToReflectionType(element));
      }
      return result;
    } else {
      return value;
    }
  } else {
    return singularToReflectionType(value);
  }
}
项目:sstore-soft    文件:ExtensionRegistry.java   
private void add(final ExtensionInfo extension) {
  if (!extension.descriptor.isExtension()) {
    throw new IllegalArgumentException(
      "ExtensionRegistry.add() was given a FieldDescriptor for a regular " +
      "(non-extension) field.");
  }

  extensionsByName.put(extension.descriptor.getFullName(), extension);
  extensionsByNumber.put(
    new DescriptorIntPair(extension.descriptor.getContainingType(),
                          extension.descriptor.getNumber()),
    extension);

  final FieldDescriptor field = extension.descriptor;
  if (field.getContainingType().getOptions().getMessageSetWireFormat() &&
      field.getType() == FieldDescriptor.Type.MESSAGE &&
      field.isOptional() &&
      field.getExtensionScope() == field.getMessageType()) {
    // This is an extension of a MessageSet type defined within the extension
    // type's own scope.  For backwards-compatibility, allow it to be looked
    // up by type name.
    extensionsByName.put(field.getMessageType().getFullName(), extension);
  }
}
项目:jigsaw-payment    文件:XmlFormat.java   
private Object handleObject(Tokenizer tokenizer,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension) throws ParseException {

    Object value;
    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    //tokenizer.consume("<");
    String endToken = "</";

    while (!tokenizer.tryConsume(endToken)) {
        if (tokenizer.atEnd()) {
            throw tokenizer.parseException("Expected \"" + endToken + "\".");
        }
        mergeField(tokenizer, extensionRegistry, subBuilder);
    }

    value = subBuilder.build();
    return value;
}
项目:s-store    文件:GeneratedMessage.java   
RepeatedMessageFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass) {
  super(descriptor, camelCaseName, messageClass, builderClass);

  newBuilderMethod = getMethodOrDie(type, "newBuilder");
}
项目:s-store    文件:GeneratedMessage.java   
/** Set the value of one element of a repeated extension. */
public final <Type> BuilderType setExtension(
    final GeneratedExtension<MessageType, List<Type>> extension,
    final int index, final Type value) {
  final ExtendableMessage<MessageType> message = internalGetResult();
  message.verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  message.extensions.setRepeatedField(
    descriptor, index,
    extension.singularToReflectionType(value));
  return (BuilderType) this;
}
项目:sstore-soft    文件:DynamicMessage.java   
private 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();
}
项目:s-store    文件:TextFormat.java   
private static void printField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isRepeated()) {
    // Repeated field.  Print each element.
    for (final Object element : (List) value) {
      printSingleField(field, element, generator);
    }
  } else {
    printSingleField(field, value, generator);
  }
}
项目:s-store    文件:ExtensionRegistry.java   
/** Add a non-message-type extension to the registry by descriptor. */
public void add(final FieldDescriptor type) {
  if (type.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
    throw new IllegalArgumentException(
      "ExtensionRegistry.add() must be provided a default instance when " +
      "adding an embedded message extension.");
  }
  add(new ExtensionInfo(type, null));
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeWrapper(JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  Descriptor type = builder.getDescriptorForType();
  FieldDescriptor field = type.findFieldByName("value");
  if (field == null) {
    throw new InvalidProtocolBufferException("Invalid wrapper type: " + type.getFullName());
  }
  builder.setField(field, parseFieldValue(field, json, builder));
}
项目:s-store    文件:GeneratedMessage.java   
/** Get the number of elements in a repeated extension. */
public final <Type> int getExtensionCount(
    final GeneratedExtension<MessageType, List<Type>> extension) {
  verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  return extensions.getRepeatedFieldCount(descriptor);
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeListValue(JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  Descriptor descriptor = builder.getDescriptorForType();
  FieldDescriptor field = descriptor.findFieldByName("values");
  if (field == null) {
    throw new InvalidProtocolBufferException("Invalid ListValue type.");
  }
  mergeRepeatedField(field, json, builder);
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeRepeatedField(
    FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (!(json instanceof JsonArray)) {
    throw new InvalidProtocolBufferException("Expect an array but found: " + json);
  }
  JsonArray array = (JsonArray) json;
  for (int i = 0; i < array.size(); ++i) {
    Object value = parseFieldValue(field, array.get(i), builder);
    if (value == null) {
      throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
    }
    builder.addRepeatedField(field, value);
  }
}
项目:sstore-soft    文件:TextFormat.java   
public static void printField(final FieldDescriptor field,
                              final Object value,
                              final Appendable output)
                              throws IOException {
  final TextGenerator generator = new TextGenerator(output);
  printField(field, value, generator);
}
项目:seldon-core    文件:JsonFormat.java   
/**
 * Gets the default value for a field type. Note that we use proto3
 * language defaults and ignore any default values set through the
 * proto "default" option.
 */
private Object getDefaultValue(FieldDescriptor field, Message.Builder builder) {
  switch (field.getType()) {
    case INT32:
    case SINT32:
    case SFIXED32:
    case UINT32:
    case FIXED32:
      return 0;
    case INT64:
    case SINT64:
    case SFIXED64:
    case UINT64:
    case FIXED64:
      return 0L;
    case FLOAT:
      return 0.0f;
    case DOUBLE:
      return 0.0;
    case BOOL:
      return false;
    case STRING:
      return "";
    case BYTES:
      return ByteString.EMPTY;
    case ENUM:
      return field.getEnumType().getValues().get(0);
    case MESSAGE:
    case GROUP:
      return builder.newBuilderForField(field).getDefaultInstanceForType();
    default:
      throw new IllegalStateException("Invalid field type: " + field.getType());
  }
}
项目:sstore-soft    文件:TextFormat.java   
private static void printField(final FieldDescriptor field,
                              final Object value,
                              final TextGenerator generator)
                              throws IOException {
  if (field.isRepeated()) {
    // Repeated field.  Print each element.
    for (final Object element : (List) value) {
      printSingleField(field, element, generator);
    }
  } else {
    printSingleField(field, value, generator);
  }
}
项目:s-store    文件:DescriptorsTest.java   
public void testFieldDescriptorLabel() throws Exception {
  FieldDescriptor requiredField =
    TestRequired.getDescriptor().findFieldByName("a");
  FieldDescriptor optionalField =
    TestAllTypes.getDescriptor().findFieldByName("optional_int32");
  FieldDescriptor repeatedField =
    TestAllTypes.getDescriptor().findFieldByName("repeated_int32");

  assertTrue(requiredField.isRequired());
  assertFalse(requiredField.isRepeated());
  assertFalse(optionalField.isRequired());
  assertFalse(optionalField.isRepeated());
  assertFalse(repeatedField.isRequired());
  assertTrue(repeatedField.isRepeated());
}
项目:s-store    文件:DynamicMessage.java   
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;
}
项目:seldon-core    文件:JsonFormat.java   
private Map<String, FieldDescriptor> getFieldNameMap(Descriptor descriptor) {
  if (!fieldNameMaps.containsKey(descriptor)) {
    Map<String, FieldDescriptor> fieldNameMap = new HashMap<String, FieldDescriptor>();
    for (FieldDescriptor field : descriptor.getFields()) {
      fieldNameMap.put(field.getName(), field);
      fieldNameMap.put(field.getJsonName(), field);
    }
    fieldNameMaps.put(descriptor, fieldNameMap);
    return fieldNameMap;
  }
  return fieldNameMaps.get(descriptor);
}
项目:seldon-core    文件:JsonFormat.java   
/** Prints wrapper types (e.g., google.protobuf.Int32Value) */
private void printWrapper(MessageOrBuilder message) throws IOException {
  Descriptor descriptor = message.getDescriptorForType();
  FieldDescriptor valueField = descriptor.findFieldByName("value");
  if (valueField == null) {
    throw new InvalidProtocolBufferException("Invalid Wrapper type.");
  }
  // When formatting wrapper types, we just print its value field instead of
  // the whole message.
  printSingleFieldValue(valueField, message.getField(valueField));
}
项目:sstore-soft    文件:GeneratedMessage.java   
/** Get one element of a repeated extension. */
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
    final GeneratedExtension<MessageType, List<Type>> extension,
    final int index) {
  verifyExtensionContainingType(extension);
  FieldDescriptor descriptor = extension.getDescriptor();
  return (Type) extension.singularFromReflectionType(
      extensions.getRepeatedField(descriptor, index));
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeRepeatedField(
    FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (!(json instanceof JsonArray)) {
    throw new InvalidProtocolBufferException("Expect an array but found: " + json);
  }
  JsonArray array = (JsonArray) json;
  for (int i = 0; i < array.size(); ++i) {
    Object value = parseFieldValue(field, array.get(i), builder);
    if (value == null) {
      throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
    }
    builder.addRepeatedField(field, value);
  }
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeStruct(JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  Descriptor descriptor = builder.getDescriptorForType();
  FieldDescriptor field = descriptor.findFieldByName("fields");
  if (field == null) {
    throw new InvalidProtocolBufferException("Invalid Struct type.");
  }
  mergeMapField(field, json, builder);
}
项目:sstore-soft    文件:GeneratedMessage.java   
@Override
public int getRepeatedFieldCount(final FieldDescriptor field) {
  if (field.isExtension()) {
    verifyContainingType(field);
    return extensions.getRepeatedFieldCount(field);
  } else {
    return super.getRepeatedFieldCount(field);
  }
}
项目:sstore-soft    文件:GeneratedMessage.java   
/** Set the value of an extension. */
public final <Type> BuilderType setExtension(
    final GeneratedExtension<MessageType, Type> extension,
    final Type value) {
  final ExtendableMessage<MessageType> message = internalGetResult();
  message.verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  message.extensions.setField(descriptor,
                              extension.toReflectionType(value));
  return (BuilderType) this;
}
项目:sstore-soft    文件:GeneratedMessage.java   
/** Set the value of one element of a repeated extension. */
public final <Type> BuilderType setExtension(
    final GeneratedExtension<MessageType, List<Type>> extension,
    final int index, final Type value) {
  final ExtendableMessage<MessageType> message = internalGetResult();
  message.verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  message.extensions.setRepeatedField(
    descriptor, index,
    extension.singularToReflectionType(value));
  return (BuilderType) this;
}
项目:sstore-soft    文件:GeneratedMessage.java   
/** Append a value to a repeated extension. */
public final <Type> BuilderType addExtension(
    final GeneratedExtension<MessageType, List<Type>> extension,
    final Type value) {
  final ExtendableMessage<MessageType> message = internalGetResult();
  message.verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  message.extensions.addRepeatedField(
      descriptor, extension.singularToReflectionType(value));
  return (BuilderType) this;
}
项目:s-store    文件:DynamicMessage.java   
private 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();
}
项目:seldon-core    文件:JsonFormat.java   
/** Prints wrapper types (e.g., google.protobuf.Int32Value) */
private void printWrapper(MessageOrBuilder message) throws IOException {
  Descriptor descriptor = message.getDescriptorForType();
  FieldDescriptor valueField = descriptor.findFieldByName("value");
  if (valueField == null) {
    throw new InvalidProtocolBufferException("Invalid Wrapper type.");
  }
  // When formatting wrapper types, we just print its value field instead of
  // the whole message.
  printSingleFieldValue(valueField, message.getField(valueField));
}
项目:seldon-core    文件:JsonFormat.java   
private void mergeField(FieldDescriptor field, JsonElement json, Message.Builder builder)
    throws InvalidProtocolBufferException {
  if (field.isRepeated()) {
    if (builder.getRepeatedFieldCount(field) > 0) {
      throw new InvalidProtocolBufferException(
          "Field " + field.getFullName() + " has already been set.");
    }
  } else {
    if (builder.hasField(field)) {
      throw new InvalidProtocolBufferException(
          "Field " + field.getFullName() + " has already been set.");
    }
    if (field.getContainingOneof() != null
        && builder.getOneofFieldDescriptor(field.getContainingOneof()) != null) {
      FieldDescriptor other = builder.getOneofFieldDescriptor(field.getContainingOneof());
      throw new InvalidProtocolBufferException(
          "Cannot set field "
              + field.getFullName()
              + " because another field "
              + other.getFullName()
              + " belonging to the same oneof has already been set ");
    }
  }
  if (field.isRepeated() && json instanceof JsonNull) {
    // We allow "null" as value for all field types and treat it as if the
    // field is not present.
    return;
  }
  if (field.isMapField()) {
    mergeMapField(field, json, builder);
  } else if (field.isRepeated()) {
    mergeRepeatedField(field, json, builder);
  } else {
    Object value = parseFieldValue(field, json, builder);
    if (value != null) {
      builder.setField(field, value);
    }
  }
}
项目:sstore-soft    文件:GeneratedMessage.java   
/** Get the FieldAccessor for a particular field. */
private FieldAccessor getField(final FieldDescriptor field) {
  if (field.getContainingType() != descriptor) {
    throw new IllegalArgumentException(
      "FieldDescriptor does not match message type.");
  } else if (field.isExtension()) {
    // If this type had extensions, it would subclass ExtendableMessage,
    // which overrides the reflection interface to handle extensions.
    throw new IllegalArgumentException(
      "This type does not have extensions.");
  }
  return fields[field.getIndex()];
}
项目:s-store    文件:GeneratedMessage.java   
SingularFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass) {
  getMethod = getMethodOrDie(messageClass, "get" + camelCaseName);
  type = getMethod.getReturnType();
  setMethod = getMethodOrDie(builderClass, "set" + camelCaseName, type);
  hasMethod =
    getMethodOrDie(messageClass, "has" + camelCaseName);
  clearMethod = getMethodOrDie(builderClass, "clear" + camelCaseName);
}
项目:sstore-soft    文件:GeneratedMessage.java   
SingularEnumFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass) {
  super(descriptor, camelCaseName, messageClass, builderClass);

  valueOfMethod = getMethodOrDie(type, "valueOf",
                                 EnumValueDescriptor.class);
  getValueDescriptorMethod =
    getMethodOrDie(type, "getValueDescriptor");
}
项目:s-store    文件:GeneratedMessage.java   
/** Set the value of an extension. */
public final <Type> BuilderType setExtension(
    final GeneratedExtension<MessageType, Type> extension,
    final Type value) {
  final ExtendableMessage<MessageType> message = internalGetResult();
  message.verifyExtensionContainingType(extension);
  final FieldDescriptor descriptor = extension.getDescriptor();
  message.extensions.setField(descriptor,
                              extension.toReflectionType(value));
  return (BuilderType) this;
}