本文整理汇总了Java中com.google.protobuf.Descriptors.EnumDescriptor.findValueByName方法的典型用法代码示例。如果您正苦于以下问题:Java EnumDescriptor.findValueByName方法的具体用法?Java EnumDescriptor.findValueByName怎么用?Java EnumDescriptor.findValueByName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Descriptors.EnumDescriptor
的用法示例。
在下文中一共展示了EnumDescriptor.findValueByName方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseEnum
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json)
throws InvalidProtocolBufferException {
String value = json.getAsString();
EnumValueDescriptor result = enumDescriptor.findValueByName(value);
if (result == null) {
// Try to interpret the value as a number.
try {
int numericValue = parseInt32(json);
if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) {
result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue);
} else {
result = enumDescriptor.findValueByNumber(numericValue);
}
} catch (InvalidProtocolBufferException e) {
// Fall through. This exception is about invalid int32 value we get from parseInt32() but
// that's not the exception we want the user to see. Since result == null, we will throw
// an exception later.
}
if (result == null) {
throw new InvalidProtocolBufferException(
"Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName());
}
}
return result;
}
示例2: parseEnum
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
/**
* 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: handlePrimitive
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
private Object handlePrimitive(XMLEventReader parser, FieldDescriptor field, String value) {
Object result = null;
if (value == null) // precondition check
return result;
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
result = parseInt32(value);
break;
case INT64:
case SINT64:
case SFIXED64:
result = parseInt64(value);
break;
case UINT32:
case FIXED32:
result = parseUInt32(value);
break;
case UINT64:
case FIXED64:
result = parseUInt64(value);
break;
case FLOAT:
result = parseFloat(value);
break;
case DOUBLE:
result = parseDouble(value);
break;
case BOOL:
result = parseBoolean(value);
break;
case STRING:
result = value;
break;
case BYTES:
result = unescapeBytes(value);
break;
case ENUM: {
EnumDescriptor enumType = field.getEnumType();
if (isDigits(value)) {
int number = parseInt32(value);
result = enumType.findValueByNumber(number);
if (result == null) {
throw new RuntimeException("Enum type \""
+ enumType.getFullName()
+ "\" has no value with number "
+ number + ".");
}
} else {
String id = value;
result = enumType.findValueByName(id);
if (result == null) {
throw new RuntimeException("Enum type \""
+ enumType.getFullName()
+ "\" has no value named \"" + id + "\".");
}
}
break;
}
case MESSAGE:
case GROUP:
throw new RuntimeException("Can't get here.");
}
return result;
}
示例4: handlePrimitive
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
private Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field) throws ParseException {
Object value = null;
if ("null".equals(tokenizer.currentToken())) {
tokenizer.consume("null");
return value;
}
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
value = tokenizer.consumeInt32();
break;
case INT64:
case SINT64:
case SFIXED64:
value = tokenizer.consumeInt64();
break;
case UINT32:
case FIXED32:
value = tokenizer.consumeUInt32();
break;
case UINT64:
case FIXED64:
value = tokenizer.consumeUInt64();
break;
case FLOAT:
value = tokenizer.consumeFloat();
break;
case DOUBLE:
value = tokenizer.consumeDouble();
break;
case BOOL:
value = tokenizer.consumeBoolean();
break;
case STRING:
value = tokenizer.consumeString();
break;
case BYTES:
value = tokenizer.consumeByteString();
break;
case ENUM: {
EnumDescriptor enumType = field.getEnumType();
if (tokenizer.lookingAtInteger()) {
int number = tokenizer.consumeInt32();
value = enumType.findValueByNumber(number);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken("Enum type \""
+ enumType.getFullName()
+ "\" has no value with number "
+ number + ".");
}
} else {
String id = tokenizer.consumeIdentifier();
value = enumType.findValueByName(id);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken("Enum type \""
+ enumType.getFullName()
+ "\" has no value named \""
+ id + "\".");
}
}
break;
}
case MESSAGE:
case GROUP:
throw new RuntimeException("Can't get here.");
}
return value;
}
示例5: handlePrimitive
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
private Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field) throws ParseException {
Object value = null;
if ("null".equals(tokenizer.currentToken())) {
tokenizer.consume("null");
return value;
}
switch (field.getType()) {
case INT32:
case SINT32:
case SFIXED32:
value = tokenizer.consumeInt32();
break;
case INT64:
case SINT64:
case SFIXED64:
value = tokenizer.consumeInt64();
break;
case UINT32:
case FIXED32:
value = tokenizer.consumeUInt32();
break;
case UINT64:
case FIXED64:
value = tokenizer.consumeUInt64();
break;
case FLOAT:
value = tokenizer.consumeFloat();
break;
case DOUBLE:
value = tokenizer.consumeDouble();
break;
case BOOL:
value = tokenizer.consumeBoolean();
break;
case STRING:
value = tokenizer.consumeString();
break;
case BYTES:
value = tokenizer.consumeByteString();
break;
case ENUM: {
EnumDescriptor enumType = field.getEnumType();
if (tokenizer.lookingAtInteger()) {
int number = tokenizer.consumeInt32();
value = enumType.findValueByNumber(number);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken("Enum type \""
+ enumType.getFullName()
+ "\" has no value with number "
+ number + ".");
}
} else {
String id = tokenizer.consumeIdentifier();
value = enumType.findValueByName(id);
if (value == null) {
throw tokenizer.parseExceptionPreviousToken("Enum type \""
+ enumType.getFullName()
+ "\" has no value named \""
+ id + "\".");
}
}
break;
}
case MESSAGE:
case GROUP:
throw new RuntimeException("Can't get here.");
}
return value;
}
示例6: getEnumValue
import com.google.protobuf.Descriptors.EnumDescriptor; //导入方法依赖的package包/类
/**
* Gets the enum value for the given enum type and name
*
* @param enumTypeName the enum type name
* @param enumName the enum name
* @return the enum value descriptor (null if not found)
*/
public EnumValueDescriptor getEnumValue(String enumTypeName, String enumName) {
EnumDescriptor enumType = getEnumDescriptor(enumTypeName);
if (enumType == null) return null;
return enumType.findValueByName(enumName);
}