类com.google.protobuf.Descriptors.EnumValueDescriptor源码实例Demo

下面列出了怎么用com.google.protobuf.Descriptors.EnumValueDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: android-chromium   文件: GeneratedMessage.java
private GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever,
                           Class singularType,
                           Message messageDefaultInstance) {
  if (Message.class.isAssignableFrom(singularType) &&
      !singularType.isInstance(messageDefaultInstance)) {
    throw new IllegalArgumentException(
        "Bad messageDefaultInstance for " + singularType.getName());
  }
  this.descriptorRetriever = descriptorRetriever;
  this.singularType = singularType;
  this.messageDefaultInstance = messageDefaultInstance;

  if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) {
    this.enumValueOf = getMethodOrDie(singularType, "valueOf",
                                      EnumValueDescriptor.class);
    this.enumGetValueDescriptor =
        getMethodOrDie(singularType, "getValueDescriptor");
  } else {
    this.enumValueOf = null;
    this.enumGetValueDescriptor = null;
  }
}
 
源代码2 项目: api-compiler   文件: ProtoFieldValueParser.java
/**
 * Parse Enum value {@link EnumValueDescriptor} associated with given {@link EnumDescriptor} from
 * given text if found. Otherwise, throw a {@link ParseException}.
 *
 * <p>The text could be either enum value name or enum value number.
 */
static EnumValueDescriptor parseEnum(EnumDescriptor enumType, String text) {
  EnumValueDescriptor value = null;
  if (lookingAtNumber(text)) {
    int number = parseUInt32(text);
    value = enumType.findValueByNumber(number);
    if (value == null) {
      throw new ParseException(String.format(
          "Enum type '%s' has no value with number %d", enumType.getFullName(), number));
    }
  } else {
    value = enumType.findValueByName(text);
    if (value == null) {
      throw new ParseException(String.format(
          "Enum type '%s' has no value with name '%s'", enumType.getFullName(), text));
    }
  }
  return value;
}
 
源代码3 项目: play-store-api   文件: GeneratedMessage.java
GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever,
    Class singularType,
    Message messageDefaultInstance,
    ExtensionType extensionType) {
  if (Message.class.isAssignableFrom(singularType) &&
      !singularType.isInstance(messageDefaultInstance)) {
    throw new IllegalArgumentException(
        "Bad messageDefaultInstance for " + singularType.getName());
  }
  this.descriptorRetriever = descriptorRetriever;
  this.singularType = singularType;
  this.messageDefaultInstance = messageDefaultInstance;

  if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) {
    this.enumValueOf = getMethodOrDie(singularType, "valueOf",
                                      EnumValueDescriptor.class);
    this.enumGetValueDescriptor =
        getMethodOrDie(singularType, "getValueDescriptor");
  } else {
    this.enumValueOf = null;
    this.enumGetValueDescriptor = null;
  }
  this.extensionType = extensionType;
}
 
源代码4 项目: android-chromium   文件: GeneratedMessage.java
/**
 * Like {@link #fromReflectionType(Object)}, but if the type is a repeated
 * type, this converts a single element.
 */
private Object singularFromReflectionType(final Object value) {
  FieldDescriptor descriptor = getDescriptor();
  switch (descriptor.getJavaType()) {
    case MESSAGE:
      if (singularType.isInstance(value)) {
        return value;
      } else {
        // It seems the copy of the embedded message stored inside the
        // extended message is not of the exact type the user was
        // expecting.  This can happen if a user defines a
        // GeneratedExtension manually and gives it a different type.
        // This should not happen in normal use.  But, to be nice, we'll
        // copy the message to whatever type the caller was expecting.
        return messageDefaultInstance.newBuilderForType()
                       .mergeFrom((Message) value).build();
      }
    case ENUM:
      return invokeOrDie(enumValueOf, null, (EnumValueDescriptor) value);
    default:
      return value;
  }
}
 
源代码5 项目: play-store-api   文件: GeneratedMessage.java
RepeatedEnumFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass) {
  super(descriptor, camelCaseName, messageClass, builderClass);

  enumDescriptor = descriptor.getEnumType();

  valueOfMethod = getMethodOrDie(type, "valueOf",
                                 EnumValueDescriptor.class);
  getValueDescriptorMethod =
    getMethodOrDie(type, "getValueDescriptor");

  supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue();
  if (supportUnknownEnumValue) {
    getRepeatedValueMethod =
        getMethodOrDie(messageClass, "get" + camelCaseName + "Value", int.class);
    getRepeatedValueMethodBuilder =
        getMethodOrDie(builderClass, "get" + camelCaseName + "Value", int.class);
    setRepeatedValueMethod =
        getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class, int.class);
    addRepeatedValueMethod =
        getMethodOrDie(builderClass, "add" + camelCaseName + "Value", int.class);
  }
}
 
源代码6 项目: travelguide   文件: GeneratedMessage.java
private GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever,
                           Class singularType,
                           Message messageDefaultInstance) {
  if (Message.class.isAssignableFrom(singularType) &&
      !singularType.isInstance(messageDefaultInstance)) {
    throw new IllegalArgumentException(
        "Bad messageDefaultInstance for " + singularType.getName());
  }
  this.descriptorRetriever = descriptorRetriever;
  this.singularType = singularType;
  this.messageDefaultInstance = messageDefaultInstance;

  if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) {
    this.enumValueOf = getMethodOrDie(singularType, "valueOf",
                                      EnumValueDescriptor.class);
    this.enumGetValueDescriptor =
        getMethodOrDie(singularType, "getValueDescriptor");
  } else {
    this.enumValueOf = null;
    this.enumGetValueDescriptor = null;
  }
}
 
源代码7 项目: travelguide   文件: GeneratedMessage.java
/**
 * Like {@link #fromReflectionType(Object)}, but if the type is a repeated
 * type, this converts a single element.
 */
private Object singularFromReflectionType(final Object value) {
  FieldDescriptor descriptor = getDescriptor();
  switch (descriptor.getJavaType()) {
    case MESSAGE:
      if (singularType.isInstance(value)) {
        return value;
      } else {
        // It seems the copy of the embedded message stored inside the
        // extended message is not of the exact type the user was
        // expecting.  This can happen if a user defines a
        // GeneratedExtension manually and gives it a different type.
        // This should not happen in normal use.  But, to be nice, we'll
        // copy the message to whatever type the caller was expecting.
        return messageDefaultInstance.newBuilderForType()
                       .mergeFrom((Message) value).build();
      }
    case ENUM:
      return invokeOrDie(enumValueOf, null, (EnumValueDescriptor) value);
    default:
      return value;
  }
}
 
源代码8 项目: 365browser   文件: GeneratedMessage.java
GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever,
    Class singularType,
    Message messageDefaultInstance,
    ExtensionType extensionType) {
  if (Message.class.isAssignableFrom(singularType) &&
      !singularType.isInstance(messageDefaultInstance)) {
    throw new IllegalArgumentException(
        "Bad messageDefaultInstance for " + singularType.getName());
  }
  this.descriptorRetriever = descriptorRetriever;
  this.singularType = singularType;
  this.messageDefaultInstance = messageDefaultInstance;

  if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) {
    this.enumValueOf = getMethodOrDie(singularType, "valueOf",
                                      EnumValueDescriptor.class);
    this.enumGetValueDescriptor =
        getMethodOrDie(singularType, "getValueDescriptor");
  } else {
    this.enumValueOf = null;
    this.enumGetValueDescriptor = null;
  }
  this.extensionType = extensionType;
}
 
源代码9 项目: 365browser   文件: GeneratedMessage.java
/**
 * Like {@link #fromReflectionType(Object)}, but if the type is a repeated
 * type, this converts a single element.
 */
// @Override
protected Object singularFromReflectionType(final Object value) {
  FieldDescriptor descriptor = getDescriptor();
  switch (descriptor.getJavaType()) {
    case MESSAGE:
      if (singularType.isInstance(value)) {
        return value;
      } else {
        return messageDefaultInstance.newBuilderForType()
            .mergeFrom((Message) value).build();
      }
    case ENUM:
      return invokeOrDie(enumValueOf, null, (EnumValueDescriptor) value);
    default:
      return value;
  }
}
 
源代码10 项目: gson   文件: ProtoTypeAdapter.java
/**
 * Finds an enum value in the given {@link EnumDescriptor} that matches the given JSON element,
 * either by name if the current adapter is using {@link EnumSerialization#NAME}, otherwise by
 * number. If matching by name, it uses the extension value if it is defined, otherwise it uses
 * its default value.
 *
 * @throws IllegalArgumentException if a matching name/number was not found
 */
private EnumValueDescriptor findValueByNameAndExtension(EnumDescriptor desc,
    JsonElement jsonElement) {
  if (enumSerialization == EnumSerialization.NAME) {
    // With enum name
    for (EnumValueDescriptor enumDesc : desc.getValues()) {
      String enumValue = getCustSerializedEnumValue(enumDesc.getOptions(), enumDesc.getName());
      if (enumValue.equals(jsonElement.getAsString())) {
        return enumDesc;
      }
    }
    throw new IllegalArgumentException(
        String.format("Unrecognized enum name: %s", jsonElement.getAsString()));
  } else {
    // With enum value
    EnumValueDescriptor fieldValue = desc.findValueByNumber(jsonElement.getAsInt());
    if (fieldValue == null) {
      throw new IllegalArgumentException(
          String.format("Unrecognized enum value: %s", jsonElement.getAsInt()));
    }
    return fieldValue;
  }
}
 
源代码11 项目: xresloader   文件: DataDstPbHelper.java
@SuppressWarnings("unchecked")
static public void dumpConstIntoHashMap(String package_name, HashMap<String, Object> parent,
        Descriptors.EnumDescriptor enum_desc) {
    String enum_seg = enum_desc.getName();
    HashMap<String, Object> enum_root;
    if (parent.containsKey(enum_seg)) {
        Object node = parent.get(enum_seg);
        if (node instanceof HashMap) {
            enum_root = (HashMap<String, Object>) node;
        } else {
            ProgramOptions.getLoger().error("enum name %s.%s conflict.", package_name, enum_seg);
            return;
        }
    } else {
        enum_root = new HashMap<String, Object>();
        parent.put(enum_seg, enum_root);
    }

    // 写出所有常量值
    for (Descriptors.EnumValueDescriptor enum_val : enum_desc.getValues()) {
        enum_root.put(enum_val.getName(), enum_val.getNumber());
    }
}
 
源代码12 项目: xresloader   文件: DataDstPbHelper.java
static public HashMap<String, Object> dumpOptionsIntoHashMap(Descriptors.EnumValueDescriptor msg_desc,
        com.google.protobuf.ExtensionRegistry custom_extensions) {
    HashMap<String, Object> msg_root = new HashMap<String, Object>();
    boolean has_data = false;

    HashMap<String, Object> options_data = dumpMessageExtensions(msg_desc.getOptions(),
            msg_desc.getOptions().getAllFields(), custom_extensions);
    if (options_data != null && !options_data.isEmpty()) {
        has_data = true;
        msg_root.put("options", options_data);
    }

    if (has_data) {
        msg_root.put("name", msg_desc.getName());
        msg_root.put("number", msg_desc.getNumber());
        return msg_root;
    }

    return null;
}
 
源代码13 项目: js-dossier   文件: JsonEncoder.java
@SuppressWarnings("unchecked")
private JsonElement encodeValue(FieldDescriptor field, Object value) {
  switch (field.getJavaType()) {
    case ENUM:
      return new JsonPrimitive(((EnumValueDescriptor) value).getNumber());

    case MESSAGE:
      return encode((Message) value);

    case BOOLEAN:
      return !((boolean) value) ? new JsonPrimitive(0) : new JsonPrimitive(1);

    case INT:
    case LONG:
      return new JsonPrimitive((Number) value);

    case STRING:
      return isNullOrEmpty((String) value)
          ? JsonNull.INSTANCE
          : new JsonPrimitive((String) value);

    default:
      return JsonNull.INSTANCE;
  }
}
 
源代码14 项目: android-chromium   文件: GeneratedMessage.java
private GeneratedExtension(ExtensionDescriptorRetriever descriptorRetriever,
                           Class singularType,
                           Message messageDefaultInstance) {
  if (Message.class.isAssignableFrom(singularType) &&
      !singularType.isInstance(messageDefaultInstance)) {
    throw new IllegalArgumentException(
        "Bad messageDefaultInstance for " + singularType.getName());
  }
  this.descriptorRetriever = descriptorRetriever;
  this.singularType = singularType;
  this.messageDefaultInstance = messageDefaultInstance;

  if (ProtocolMessageEnum.class.isAssignableFrom(singularType)) {
    this.enumValueOf = getMethodOrDie(singularType, "valueOf",
                                      EnumValueDescriptor.class);
    this.enumGetValueDescriptor =
        getMethodOrDie(singularType, "getValueDescriptor");
  } else {
    this.enumValueOf = null;
    this.enumGetValueDescriptor = null;
  }
}
 
源代码15 项目: android-chromium   文件: GeneratedMessage.java
/**
 * Like {@link #fromReflectionType(Object)}, but if the type is a repeated
 * type, this converts a single element.
 */
private Object singularFromReflectionType(final Object value) {
  FieldDescriptor descriptor = getDescriptor();
  switch (descriptor.getJavaType()) {
    case MESSAGE:
      if (singularType.isInstance(value)) {
        return value;
      } else {
        // It seems the copy of the embedded message stored inside the
        // extended message is not of the exact type the user was
        // expecting.  This can happen if a user defines a
        // GeneratedExtension manually and gives it a different type.
        // This should not happen in normal use.  But, to be nice, we'll
        // copy the message to whatever type the caller was expecting.
        return messageDefaultInstance.newBuilderForType()
                       .mergeFrom((Message) value).build();
      }
    case ENUM:
      return invokeOrDie(enumValueOf, null, (EnumValueDescriptor) value);
    default:
      return value;
  }
}
 
源代码16 项目: rejoiner   文件: ProtoToGql.java
static GraphQLEnumType convert(
    EnumDescriptor descriptor, SchemaOptions schemaOptions) {
  GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum().name(getReferenceName(descriptor));
  for (EnumValueDescriptor value : descriptor.getValues()) {
    builder.value(
        value.getName(),
        value.getName(),
        schemaOptions.commentsMap().get(value.getFullName()),
        value.getOptions().getDeprecated() ? "deprecated in proto" : null);
  }
  return builder.build();
}
 
源代码17 项目: curiostack   文件: SerializeSupport.java
public static void printEnum(int value, JsonGenerator gen, EnumDescriptor descriptor)
    throws IOException {
  EnumValueDescriptor valueDescriptor = descriptor.findValueByNumber(value);
  if (valueDescriptor == null) {
    gen.writeNumber(value);
  } else {
    gen.writeString(valueDescriptor.getName());
  }
}
 
源代码18 项目: curiostack   文件: SerializeSupport.java
public static void printEnum(EnumValueDescriptor value, JsonGenerator gen) throws IOException {
  if (value.getIndex() == -1) {
    gen.writeString(Integer.toString(value.getNumber()));
  } else {
    gen.writeString(value.getName());
  }
}
 
源代码19 项目: api-compiler   文件: ProtoHelpers.java
@Nullable
public static <Type> Type getFieldIfPresent(
    MessageOrBuilder mob, FieldDescriptor fieldDesc, Class<Type> clazz) {
  if (mob.hasField(fieldDesc)) {
    Object fieldValue = null;
    try {
      fieldValue = mob.getField(fieldDesc);
      if (null == fieldValue) {
        return null;
      }

      if (fieldValue instanceof EnumValueDescriptor && clazz.isEnum()) {
        // Do some sanity checks and convert the EnumValueDescriptor into the (Type) class (which
        // has to be an enum)
        EnumValueDescriptor fieldEnumValue = (EnumValueDescriptor) fieldValue;
        if (clazz.getSimpleName().equals(fieldEnumValue.getType().getName())) {
          Type[] enumValues = clazz.getEnumConstants();
          if (fieldEnumValue.getIndex() >= 0 && fieldEnumValue.getIndex() < enumValues.length) {
            Type value = enumValues[fieldEnumValue.getIndex()];
            return value;
          }
        }
        throw new RuntimeException(
            String.format("Couldn't convert '%s' to class '%s'", fieldValue, clazz.getName()));
      }

      return clazz.cast(fieldValue);
    } catch (ClassCastException ex) {
      throw new RuntimeException(
          String.format(
              "Expected (%s) type, not (%s), for field '%s' of (%s)%s",
              clazz, fieldValue.getClass(), fieldDesc.getName(), mob.getClass(), getName(mob)),
          ex);
    }
  }
  return null;
}
 
源代码20 项目: android-chromium   文件: 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");
}
 
源代码21 项目: play-store-api   文件: GeneratedMessage.java
@Override
public void set(final Builder builder, final Object value) {
  if (supportUnknownEnumValue) {
    invokeOrDie(setValueMethod, builder,
        ((EnumValueDescriptor) value).getNumber());
    return;
  }
  super.set(builder, invokeOrDie(valueOfMethod, null, value));
}
 
源代码22 项目: play-store-api   文件: GeneratedMessage.java
@Override
public void setRepeated(final Builder builder,
                        final int index, final Object value) {
  if (supportUnknownEnumValue) {
    invokeOrDie(setRepeatedValueMethod, builder, index,
        ((EnumValueDescriptor) value).getNumber());
    return;
  }
  super.setRepeated(builder, index, invokeOrDie(valueOfMethod, null,
                    value));
}
 
源代码23 项目: play-store-api   文件: GeneratedMessage.java
@Override
public void addRepeated(final Builder builder, final Object value) {
  if (supportUnknownEnumValue) {
    invokeOrDie(addRepeatedValueMethod, builder,
        ((EnumValueDescriptor) value).getNumber());
    return;
  }
  super.addRepeated(builder, invokeOrDie(valueOfMethod, null, value));
}
 
源代码24 项目: android-chromium   文件: GeneratedMessage.java
RepeatedEnumFieldAccessor(
    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");
}
 
源代码25 项目: play-store-api   文件: MapEntry.java
@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;
}
 
源代码26 项目: travelguide   文件: 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");
}
 
源代码27 项目: travelguide   文件: GeneratedMessage.java
RepeatedEnumFieldAccessor(
    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");
}
 
源代码28 项目: travelguide   文件: DescriptorsTest.java
public void testEnumDescriptor() throws Exception {
  EnumDescriptor enumType = ForeignEnum.getDescriptor();
  EnumDescriptor nestedType = TestAllTypes.NestedEnum.getDescriptor();

  assertEquals("ForeignEnum", enumType.getName());
  assertEquals("protobuf_unittest.ForeignEnum", enumType.getFullName());
  assertEquals(UnittestProto.getDescriptor(), enumType.getFile());
  assertNull(enumType.getContainingType());
  assertEquals(DescriptorProtos.EnumOptions.getDefaultInstance(),
               enumType.getOptions());

  assertEquals("NestedEnum", nestedType.getName());
  assertEquals("protobuf_unittest.TestAllTypes.NestedEnum",
               nestedType.getFullName());
  assertEquals(UnittestProto.getDescriptor(), nestedType.getFile());
  assertEquals(TestAllTypes.getDescriptor(), nestedType.getContainingType());

  EnumValueDescriptor value = ForeignEnum.FOREIGN_FOO.getValueDescriptor();
  assertEquals(value, enumType.getValues().get(0));
  assertEquals("FOREIGN_FOO", value.getName());
  assertEquals(4, value.getNumber());
  assertEquals(value, enumType.findValueByName("FOREIGN_FOO"));
  assertEquals(value, enumType.findValueByNumber(4));
  assertNull(enumType.findValueByName("NO_SUCH_VALUE"));
  for (int i = 0; i < enumType.getValues().size(); i++) {
    assertEquals(i, enumType.getValues().get(i).getIndex());
  }
}
 
源代码29 项目: 365browser   文件: DynamicMessage.java
/** Verifies that the value is EnumValueDescriptor and matches Enum Type. */
private void ensureSingularEnumValueDescriptor(
    FieldDescriptor field, Object value) {
  if (value == null) {
    throw new NullPointerException();
  }
  if (!(value instanceof EnumValueDescriptor)) {
    throw new IllegalArgumentException(
      "DynamicMessage should use EnumValueDescriptor to set Enum Value.");
  }
  if (field.getEnumType() != ((EnumValueDescriptor) value).getType()) {
    throw new IllegalArgumentException(
      "EnumValueDescriptor doesn't much Enum Field.");
  }
}
 
源代码30 项目: 365browser   文件: GeneratedMessage.java
SingularEnumFieldAccessor(
    final FieldDescriptor descriptor, final String camelCaseName,
    final Class<? extends GeneratedMessage> messageClass,
    final Class<? extends Builder> builderClass,
    final String containingOneofCamelCaseName) {
  super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName);

  valueOfMethod = getMethodOrDie(type, "valueOf",
                                 EnumValueDescriptor.class);
  getValueDescriptorMethod =
    getMethodOrDie(type, "getValueDescriptor");
}
 
 类所在包
 类方法
 同包方法