/** For enum fields, gets the field's type. */ public EnumDescriptor getEnumType() { if (getJavaType() != JavaType.ENUM) { throw new UnsupportedOperationException( "This field is not of enum type."); } return enumType; }
/** For embedded message and group fields, gets the field's type. */ public Descriptor getMessageType() { if (getJavaType() != JavaType.MESSAGE) { throw new UnsupportedOperationException( "This field is not of message type."); } return messageType; }
@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( fieldDescriptor.getJavaType() == JavaType.FLOAT, "%s is not a float field", fieldDescriptor); } };
public Object getField(FieldDescriptor field) { verifyContainingType(field); Object result = fields.getField(field); if (result == null) { if (field.isRepeated()) { result = Collections.emptyList(); } else if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { result = getDefaultInstance(field.getMessageType()); } else { result = field.getDefaultValue(); } } return result; }
@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( !fieldDescriptor.isRepeated(), "%s is a repeated field; repeated fields cannot be absent, only empty", fieldDescriptor); checkArgument( fieldDescriptor.getContainingType().getFile().getSyntax() != Syntax.PROTO3 || fieldDescriptor.getJavaType() == JavaType.MESSAGE, "%s is a primitive field in a Proto 3 message; it cannot be absent", fieldDescriptor); } },
/** * Returns the field's default value. Valid for all types except for * messages and groups. For all other types, the object returned is of * the same class that would returned by Message.getField(this). */ public Object getDefaultValue() { if (getJavaType() == JavaType.MESSAGE) { throw new UnsupportedOperationException( "FieldDescriptor.getDefaultValue() called on an embedded message " + "field."); } return defaultValue; }
/** * Returns the field's default value. Valid for all types except for * messages and groups. For all other types, the object returned is of * the same class that would returned by Message.getField(this). */ public Object getDefaultValue() { if (getJavaType() == JavaType.MESSAGE) { throw new UnsupportedOperationException( "FieldDescriptor.getDefaultValue() called on an embedded message " + "field."); } return defaultValue; }
@Override @SuppressWarnings("unchecked") public Type getDefaultValue() { if (isRepeated()) { return (Type) Collections.emptyList(); } if (getDescriptor().getJavaType() == FieldDescriptor.JavaType.MESSAGE) { return (Type) messageDefaultInstance; } return (Type) singularFromReflectionType( getDescriptor().getDefaultValue()); } }
/** For embedded message and group fields, gets the field's type. */ public Descriptor getMessageType() { if (getJavaType() != JavaType.MESSAGE) { throw new UnsupportedOperationException( "This field is not of message type."); } return messageType; }
@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( fieldDescriptor.getJavaType() == JavaType.DOUBLE, "%s is not a double field", fieldDescriptor); } },
/** For enum fields, gets the field's type. */ @Override public EnumDescriptor getEnumType() { if (getJavaType() != JavaType.ENUM) { throw new UnsupportedOperationException( "This field is not of enum type."); } return enumType; }
/** Add a message-type extension to the registry by descriptor. */ public void add(final FieldDescriptor type, final Message defaultInstance) { if (type.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( "ExtensionRegistry.add() provided a default instance for a " + "non-message extension."); } add(new ExtensionInfo(type, defaultInstance), Extension.ExtensionType.IMMUTABLE); }
private void populateMapEntry() { for (FieldDescriptor field : type.getFields()) { if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { fields.setField(field, getDefaultInstance(field.getMessageType())); } else { fields.setField(field, field.getDefaultValue()); } } }
/** 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)); }
/** 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."); } ExtensionInfo info = new ExtensionInfo(type, null); add(info, Extension.ExtensionType.IMMUTABLE); add(info, Extension.ExtensionType.MUTABLE); }
@Override public Message.Builder newBuilderForField(FieldDescriptor field) { checkFieldDescriptor(field);; // This method should be called for message fields and in a MapEntry // message only the value field can possibly be a message field. if (field.getNumber() != 2 || field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new RuntimeException( "\"" + field.getFullName() + "\" is not a message value field."); } return ((Message) value).newBuilderForType(); }
/** Add a message-type extension to the registry by descriptor. */ public void add(final FieldDescriptor type, final Message defaultInstance) { if (type.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( "ExtensionRegistry.add() provided a default instance for a " + "non-message extension."); } add(new ExtensionInfo(type, defaultInstance)); }
public Builder newBuilderForField(FieldDescriptor field) { verifyContainingType(field); if (field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( "newBuilderForField is only valid for fields with message type."); } return new Builder(field.getMessageType()); }
@Override public Builder newBuilderForField(FieldDescriptor field) { verifyContainingType(field); if (field.getJavaType() != FieldDescriptor.JavaType.MESSAGE) { throw new IllegalArgumentException( "newBuilderForField is only valid for fields with message type."); } return new Builder(field.getMessageType()); }
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; }