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

The following examples show how to use com.google.protobuf.Descriptors.FieldDescriptor#getEnumType() . 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 play-store-api with GNU General Public License v3.0 6 votes vote down vote up
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);

  enumDescriptor = descriptor.getEnumType();

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

  supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue();
  if (supportUnknownEnumValue) {
    getValueMethod =
        getMethodOrDie(messageClass, "get" + camelCaseName + "Value");
    getValueMethodBuilder =
        getMethodOrDie(builderClass, "get" + camelCaseName + "Value");
    setValueMethod =
        getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class);
  }
}
 
Example 2
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
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);
  }
}
 
Example 3
Source File: DoParse.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Determines whether we skip processing of the field if it is null. We usually skip null values
 * in the JSON to treat them as default, but must actually process the null for {@link Value} and
 * {@link NullValue} because it means their value must be set.
 */
private static boolean mustSkipNull(FieldDescriptor field) {
  if (field.isRepeated()) {
    return true;
  }
  if (field.getJavaType() == JavaType.MESSAGE
      && field.getMessageType() == Value.getDescriptor()) {
    return false;
  }
  if (field.getJavaType() == JavaType.ENUM && field.getEnumType() == NullValue.getDescriptor()) {
    return false;
  }
  return true;
}
 
Example 4
Source File: DynamicMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/** 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.");
  }
}
 
Example 5
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testEnumDescriptor() throws Exception {
  Descriptor descriptor = TypicalData.Builder.getDescriptor();
  FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(3);
  assertEquals(Type.ENUM, fieldDescriptor.getType());
  EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType();
  assertNotNull(enumDescriptor);

  EnumValueDescriptor enumValueDescriptor = enumDescriptor.findValueByNumber(1);
  assertEquals(1, enumValueDescriptor.getNumber());
  assertEquals("VALUE1", enumValueDescriptor.getName());
}
 
Example 6
Source File: JsonFormat.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field,
                                      boolean selfType)
        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(field.getFullName(), selfType);
            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();
                if (StringUtils.isAllLowerCase(id)) {
                    char b = id.charAt(0);
                    b = (char) (b + 'A' - 'a');
                    String s = id.substring(1);
                    id = b + s;
                }
                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.");
        default:
    }
    return value;
}
 
Example 7
Source File: MapToMessage.java    From krpc with Apache License 2.0 4 votes vote down vote up
static Object objToMessageObjInner(Builder b, Object value, FieldDescriptor field, boolean isRepeated) {

        switch (field.getType()) {
            case INT32:
            case SINT32:
            case SFIXED32:
                return TypeSafe.anyToInt(value);

            case INT64:
            case SINT64:
            case SFIXED64:
                if( value instanceof Date)
                    return ((Date)value).getTime();
                return TypeSafe.anyToLong(value);

            case BOOL:
                return TypeSafe.anyToBool(value);

            case FLOAT:
                return TypeSafe.anyToFloat(value);

            case DOUBLE:
                return TypeSafe.anyToDouble(value);

            case UINT32:
            case FIXED32:
                return (int) (TypeSafe.anyToLong(value) & 0x00000000FFFFFFFFL);

            case UINT64:
            case FIXED64:
                BigInteger bi = new BigInteger(value.toString());
                return bi.longValue();

            case STRING:
                if( value instanceof Date)
                    return formatDate((Date)value);
                return TypeSafe.anyToString(value);

            case BYTES: {
                    if (value instanceof ByteString) {
                        return value;
                    }
                    if (value instanceof String) {
                        byte[] bb = getBytes((String) value);
                        if (bb == null) return null;
                        return ByteString.copyFrom(bb);
                    }
                    if (value instanceof byte[]) {
                        return ByteString.copyFrom((byte[]) value);
                    }

                    return null;
                }

            case ENUM: {
                    EnumDescriptor ed = field.getEnumType();
                    EnumValueDescriptor evd = ed.findValueByName(value.toString());
                    if (evd == null) {
                        evd = ed.findValueByNumber(TypeSafe.anyToInt(value));
                    }
                    if (evd == null) return null;
                    return evd;
                }

            case MESSAGE:

                Map<String, Object> map = TypeSafe.anyToMap(value);
                if (map == null) {
                    if( value instanceof MapConvertable) {
                        map = ((MapConvertable)value).toMap();
                    }
                    if( map == null ) {
                        return null;
                    }
                }

                Builder b2 = isRepeated ?
                        getRepeatedFieldBuilder(b, field.getName()) :
                                getFieldBuilder(b, field);

                for (FieldDescriptor subfield : b2.getDescriptorForType().getFields()) {
                    String subName = subfield.getName();
                    Object subValue = getValue(map, null, subName);
                    if (subValue == null) continue;
                    if (subfield.isRepeated()) {
                        objToMessageObjRepeated(b2, subValue, subfield);
                    } else {
                        objToMessageObj(b2, subValue, subfield);
                    }
                }

                return isRepeated ? null : b2.build();

            default:
                return null;
        }
    }
 
Example 8
Source File: XmlJavaxFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 9
Source File: XmlFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private Object handlePrimitive(Tokenizer tokenizer, FieldDescriptor field) throws ParseException {
    Object value = null;
    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: {
            Descriptors.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;
}
 
Example 10
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
private Object handlePrimitive(JsonParser parser, FieldDescriptor field) throws IOException {
    Object value = null;

    JsonToken token = parser.getCurrentToken();

    if (token.equals(JsonToken.VALUE_NULL)) {
        return value;
    }

    switch (field.getType()) {
        case INT32:
        case SINT32:
        case SFIXED32:
        	value = parser.getIntValue();
            break;

        case INT64:
        case SINT64:
        case SFIXED64:
        	value = parser.getLongValue();
            break;

        case UINT32:
        case FIXED32:
        	long valueLong = parser.getLongValue();
        	if (valueLong < 0 || valueLong > MAX_UINT_VALUE) {
        		throw new NumberFormatException("Number must be positive: " + valueLong);
        	}
        	value = (int) valueLong;
            break;

        case UINT64:
        case FIXED64:
        	BigInteger valueBigInt = parser.getBigIntegerValue();
            // valueBigInt < 0 || valueBigInt > MAX_ULONG_VALUE
        	if (valueBigInt.compareTo(BigInteger.ZERO) == -1 || valueBigInt.compareTo(MAX_ULONG_VALUE) == 1) {
        		throw new NumberFormatException("Number must be positive: " + valueBigInt);
        	}
        	value = valueBigInt.longValue();
            break;

        case FLOAT:
        	value = parser.getFloatValue();
            break;

        case DOUBLE:
        	value = parser.getDoubleValue();
            break;

        case BOOL:
        	value = parser.getBooleanValue();
            break;

        case STRING:
        	value = parser.getText();
            break;

        case BYTES:
        	value = ByteString.copyFrom(parser.getBinaryValue());
            break;

        case ENUM: {
            EnumDescriptor enumType = field.getEnumType();
            if (token.equals(JsonToken.VALUE_NUMBER_INT)) {
                int number = parser.getIntValue();
                value = enumType.findValueByNumber(number);
                if (value == null) {
                    throw new RuntimeException("Enum type \""
                    		+ enumType.getFullName()
                    		+ "\" has no value with number "
                    		+ number + ".");
                }
            } else {
                String id = parser.getText();
                value = enumType.findValueByName(id);
                if (value == 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 value;
}
 
Example 11
Source File: JsonFormat.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 12
Source File: ProtoDynamicMessageSchema.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
Object convertToProtoValue(FieldDescriptor fieldDescriptor, Object value) {
  Descriptors.EnumDescriptor enumType = fieldDescriptor.getEnumType();
  return enumType.findValueByNumber(((EnumerationType.Value) value).getValue());
}
 
Example 13
Source File: ProtobufJsonFormat.java    From tajo with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: ProtobufJsonFormat.java    From incubator-tajo with Apache License 2.0 4 votes vote down vote up
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;
}