com.google.protobuf.Descriptors.FieldDescriptor#getJavaType ( )源码实例Demo

下面列出了com.google.protobuf.Descriptors.FieldDescriptor#getJavaType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android-chromium   文件: GeneratedMessage.java
@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);
  }
}
 
源代码2 项目: jigsaw-payment   文件: JsonJacksonFormat.java
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);
        }
    }
}
 
源代码3 项目: android-chromium   文件: GeneratedMessage.java
/** 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);
  }
}
 
源代码4 项目: travelguide   文件: GeneratedMessage.java
/**
 * 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);
  }
}
 
源代码5 项目: travelguide   文件: GeneratedMessage.java
/** 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);
  }
}
 
源代码6 项目: android-chromium   文件: GeneratedMessage.java
/**
 * 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);
  }
}
 
源代码7 项目: 365browser   文件: DynamicMessage.java
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;
}
 
源代码8 项目: android-chromium   文件: GeneratedMessage.java
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;
}
 
源代码9 项目: jprotobuf   文件: MapEntry.java
@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();
}
 
源代码10 项目: android-chromium   文件: DynamicMessage.java
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());
}
 
源代码11 项目: android-chromium   文件: DynamicMessage.java
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;
}
 
源代码12 项目: curiostack   文件: DoParse.java
/**
 * 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;
}
 
源代码13 项目: 365browser   文件: DynamicMessage.java
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());
}
 
源代码14 项目: android-chromium   文件: ExtensionRegistry.java
/** 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));
}
 
源代码15 项目: play-store-api   文件: DynamicMessage.java
@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());
}
 
源代码16 项目: 365browser   文件: GeneratedMessage.java
@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;
}
 
源代码17 项目: google-ads-java   文件: FieldMasks.java
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);
      }
    }
  }
}
 
private static boolean supportsFieldPresence(FieldDescriptor field) {
  // messages still support field presence in proto3
  return field.getJavaType() == JavaType.MESSAGE;
}
 
源代码19 项目: tajo   文件: ProtobufJsonFormat.java
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();
    }
  }
}
 
源代码20 项目: play-store-api   文件: TextFormat.java
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");
    }
  }
}