/** * Like {@link #fromReflectionType(Object)}, but if the type is a repeated * type, this converts a single element. */ private Object singularFromReflectionType(final Object value) { switch (descriptor.getJavaType()) { case MESSAGE: if (type.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; } }
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; }
/** * 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; }
/** 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."); } // TODO(xiaofeng): Re-enable this check after Orgstore is fixed to not // set incorrect EnumValueDescriptors. // EnumDescriptor fieldType = field.getEnumType(); // EnumDescriptor fieldValueType = ((EnumValueDescriptor) value).getType(); // if (fieldType != fieldValueType) { // throw new IllegalArgumentException(String.format( // "EnumDescriptor %s of field doesn't match EnumDescriptor %s of field value", // fieldType.getFullName(), fieldValueType.getFullName())); // } }
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; }
/** * 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; } }
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); } }
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); } }
/** * 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; } }
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; } }
/** * 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; } }
private static Object toMap(FieldDescriptor descr, Object value) { if (value == null) { return null; } final Object result; switch(descr.getJavaType()) { case MESSAGE: result = toMap((Message )value); break; case ENUM: result = ((EnumValueDescriptor )value).getName(); break; default: result = value; break; } return result; }
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; }