Пример #1
0
  /**
   * Verifies that the given object is of the correct type to be a valid value for the given field.
   * (For repeated fields, this checks if the object is the right type to be one element of the
   * field.)
   *
   * @throws IllegalArgumentException The value is not of the right type.
   */
  private static void verifyType(final WireFormat.FieldType type, final Object value) {
    if (value == null) {
      throw new NullPointerException();
    }

    boolean isValid = false;
    switch (type.getJavaType()) {
      case INT:
        isValid = value instanceof Integer;
        break;
      case LONG:
        isValid = value instanceof Long;
        break;
      case FLOAT:
        isValid = value instanceof Float;
        break;
      case DOUBLE:
        isValid = value instanceof Double;
        break;
      case BOOLEAN:
        isValid = value instanceof Boolean;
        break;
      case STRING:
        isValid = value instanceof String;
        break;
      case BYTE_STRING:
        isValid = value instanceof ByteString;
        break;
      case ENUM:
        // TODO(kenton):  Caller must do type checking here, I guess.
        isValid = value instanceof Internal.EnumLite;
        break;
      case MESSAGE:
        // TODO(kenton):  Caller must do type checking here, I guess.
        isValid = value instanceof MessageLite;
        break;
    }

    if (!isValid) {
      // TODO(kenton):  When chaining calls to setField(), it can be hard to
      //   tell from the stack trace which exact call failed, since the whole
      //   chain is considered one line of code.  It would be nice to print
      //   more information here, e.g. naming the field.  We used to do that.
      //   But we can't now that FieldSet doesn't use descriptors.  Maybe this
      //   isn't a big deal, though, since it would only really apply when using
      //   reflection and generally people don't chain reflection setters.
      throw new IllegalArgumentException(
          "Wrong object type used with protocol message reflection.");
    }
  }
Пример #2
0
 public WireFormat.JavaType getLiteJavaType() {
   return type.getJavaType();
 }