Java Code Examples for com.google.protobuf.Descriptors.FieldDescriptor#getJavaType()
The following examples show how to use
com.google.protobuf.Descriptors.FieldDescriptor#getJavaType() .
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 android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** * Convert from the type used by the reflection accessors to the type used * by native accessors. E.g., for enums, the reflection accessors use * EnumValueDescriptors but the native accessors use the generated enum * type. */ @SuppressWarnings("unchecked") private Object fromReflectionType(final Object value) { FieldDescriptor descriptor = getDescriptor(); if (descriptor.isRepeated()) { if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE || descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) { // Must convert the whole list. final List result = new ArrayList(); for (final Object element : (List) value) { result.add(singularFromReflectionType(element)); } return result; } else { return value; } } else { return singularFromReflectionType(value); } }
Example 2
Source File: GeneratedMessage.java From travelguide with Apache License 2.0 | 6 votes |
/** Get the value of an extension. */ //@Override (Java 1.6 override semantics, but we must support 1.5) public final <Type> Type getExtension( final GeneratedExtension<MessageType, Type> extension) { verifyExtensionContainingType(extension); FieldDescriptor descriptor = extension.getDescriptor(); final Object value = extensions.getField(descriptor); if (value == null) { if (descriptor.isRepeated()) { return (Type) Collections.emptyList(); } else if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { return (Type) extension.getMessageDefaultInstance(); } else { return (Type) extension.fromReflectionType( descriptor.getDefaultValue()); } } else { return (Type) extension.fromReflectionType(value); } }
Example 3
Source File: GeneratedMessage.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
@Override public Object getField(final FieldDescriptor field) { if (field.isExtension()) { verifyContainingType(field); final Object value = extensions.getField(field); if (value == null) { if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { // Lacking an ExtensionRegistry, we have no way to determine the // extension's real type, so we return a DynamicMessage. return DynamicMessage.getDefaultInstance(field.getMessageType()); } else { return field.getDefaultValue(); } } else { return value; } } else { return super.getField(field); } }
Example 4
Source File: JsonJacksonFormat.java From jigsaw-payment with Apache License 2.0 | 6 votes |
private void handleValue(JsonParser parser, ExtensionRegistry extensionRegistry, Message.Builder builder, FieldDescriptor field, ExtensionRegistry.ExtensionInfo extension, boolean unknown) throws IOException { Object value = null; if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { value = handleObject(parser, extensionRegistry, builder, field, extension, unknown); } else { value = handlePrimitive(parser, field); } if (value != null) { if (field.isRepeated()) { builder.addRepeatedField(field, value); } else { builder.setField(field, value); } } }
Example 5
Source File: GeneratedMessage.java From android-chromium with BSD 2-Clause "Simplified" License | 6 votes |
/** Get the value of an extension. */ //@Override (Java 1.6 override semantics, but we must support 1.5) @SuppressWarnings("unchecked") public final <Type> Type getExtension( final GeneratedExtension<MessageType, Type> extension) { verifyExtensionContainingType(extension); FieldDescriptor descriptor = extension.getDescriptor(); final Object value = extensions.getField(descriptor); if (value == null) { if (descriptor.isRepeated()) { return (Type) Collections.emptyList(); } else if (descriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { return (Type) extension.getMessageDefaultInstance(); } else { return (Type) extension.fromReflectionType( descriptor.getDefaultValue()); } } else { return (Type) extension.fromReflectionType(value); } }
Example 6
Source File: GeneratedMessage.java From travelguide with Apache License 2.0 | 6 votes |
/** * Convert from the type used by the native accessors to the type used * by reflection accessors. E.g., for enums, the reflection accessors use * EnumValueDescriptors but the native accessors use the generated enum * type. */ @SuppressWarnings("unchecked") private Object toReflectionType(final Object value) { FieldDescriptor descriptor = getDescriptor(); if (descriptor.isRepeated()) { if (descriptor.getJavaType() == FieldDescriptor.JavaType.ENUM) { // Must convert the whole list. final List result = new ArrayList(); for (final Object element : (List) value) { result.add(singularToReflectionType(element)); } return result; } else { return value; } } else { return singularToReflectionType(value); } }
Example 7
Source File: GeneratedMessage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }
Example 8
Source File: GeneratedMessage.java From 365browser with Apache License 2.0 | 5 votes |
@Override public boolean isInitialized() { for (final FieldDescriptor field : getDescriptorForType().getFields()) { // Check that all required fields are present. if (field.isRequired()) { if (!hasField(field)) { return false; } } // Check that embedded messages are initialized. if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (field.isRepeated()) { @SuppressWarnings("unchecked") final List<Message> messageList = (List<Message>) getField(field); for (final Message element : messageList) { if (!element.isInitialized()) { return false; } } } else { if (hasField(field) && !((Message) getField(field)).isInitialized()) { return false; } } } } return true; }
Example 9
Source File: DynamicMessage.java From play-store-api with GNU General Public License v3.0 | 5 votes |
@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()); }
Example 10
Source File: ExtensionRegistry.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
/** 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)); }
Example 11
Source File: DynamicMessage.java From 365browser with Apache License 2.0 | 5 votes |
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()); }
Example 12
Source File: DoParse.java From curiostack with MIT License | 5 votes |
/** * 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 13
Source File: DynamicMessage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
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; }
Example 14
Source File: DynamicMessage.java From android-chromium with BSD 2-Clause "Simplified" License | 5 votes |
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()); }
Example 15
Source File: MapEntry.java From jprotobuf with Apache License 2.0 | 5 votes |
@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(); }
Example 16
Source File: DynamicMessage.java From 365browser with Apache License 2.0 | 5 votes |
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; }
Example 17
Source File: ProtobufJsonFormat.java From tajo with Apache License 2.0 | 4 votes |
private void printSingleField(FieldDescriptor field, Object value, JsonGenerator generator) throws IOException { if (field.isExtension()) { generator.print("\""); // We special-case MessageSet elements for compatibility with proto1. if (field.getContainingType().getOptions().getMessageSetWireFormat() && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional()) // object equality && (field.getExtensionScope() == field.getMessageType())) { generator.print(field.getMessageType().getFullName()); } else { generator.print(field.getFullName()); } generator.print("\""); } else { generator.print("\""); if (field.getType() == FieldDescriptor.Type.GROUP) { // Groups must be serialized with their original capitalization. generator.print(field.getMessageType().getName()); } else { generator.print(field.getName()); } generator.print("\""); } // Done with the name, on to the value if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { generator.print(": "); generator.indent(); } else { generator.print(": "); } if (field.isRepeated()) { // Repeated field. Print each element. generator.print("["); for (Iterator<?> iter = ((List<?>) value).iterator(); iter.hasNext();) { printFieldValue(field, iter.next(), generator); if (iter.hasNext()) { generator.print(","); } } generator.print("]"); } else { printFieldValue(field, value, generator); if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { generator.outdent(); } } }
Example 18
Source File: MessageSerializer.java From jackson-datatype-protobuf with Apache License 2.0 | 4 votes |
private static boolean supportsFieldPresence(FieldDescriptor field) { // messages still support field presence in proto3 return field.getJavaType() == JavaType.MESSAGE; }
Example 19
Source File: FieldMasks.java From google-ads-java with Apache License 2.0 | 4 votes |
private static void compare( FieldMask.Builder mask, String currentField, Message original, Message modified) { Descriptor descriptor = original.getDescriptorForType(); for (FieldDescriptor field : descriptor.getFields()) { String fieldName = getFieldName(currentField, field); Object originalValue = original.getField(field); Object modifiedValue = modified.getField(field); if (field.isRepeated()) { if (!Objects.equals(originalValue, modifiedValue)) { mask.addPaths(fieldName); } } else { switch (field.getJavaType()) { case MESSAGE: // Because getField never returns null, we use hasField to distinguish null // from empty message when getType() == MESSAGE if (original.hasField(field) != modified.hasField(field) || !Objects.equals(originalValue, modifiedValue)) { if (isWrapperType(field.getMessageType())) { // For wrapper types, just emit the field name. mask.addPaths(fieldName); } else if (!modified.hasField(field)) { // Just emit the deleted field name mask.addPaths(fieldName); } else { // Recursively compare to find different values compare(mask, fieldName, (Message) originalValue, (Message) modifiedValue); } } break; case INT: case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case STRING: case BYTE_STRING: case ENUM: // Handle all java types except MESSAGE if (!Objects.equals(originalValue, modifiedValue)) { mask.addPaths(fieldName); } break; default: throw new IllegalArgumentException( "Unexpected java type " + field.getJavaType() + " encountered for field " + fieldName); } } } }
Example 20
Source File: TextFormat.java From play-store-api with GNU General Public License v3.0 | 4 votes |
private void printSingleField(final FieldDescriptor field, final Object value, final TextGenerator generator) throws IOException { if (field.isExtension()) { generator.print("["); // We special-case MessageSet elements for compatibility with proto1. if (field.getContainingType().getOptions().getMessageSetWireFormat() && (field.getType() == FieldDescriptor.Type.MESSAGE) && (field.isOptional()) // object equality && (field.getExtensionScope() == field.getMessageType())) { generator.print(field.getMessageType().getFullName()); } else { generator.print(field.getFullName()); } generator.print("]"); } else { if (field.getType() == FieldDescriptor.Type.GROUP) { // Groups must be serialized with their original capitalization. generator.print(field.getMessageType().getName()); } else { generator.print(field.getName()); } } if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (singleLineMode) { generator.print(" { "); } else { generator.print(" {\n"); generator.indent(); } } else { generator.print(": "); } printFieldValue(field, value, generator); if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) { if (singleLineMode) { generator.print("} "); } else { generator.outdent(); generator.print("}\n"); } } else { if (singleLineMode) { generator.print(" "); } else { generator.print("\n"); } } }