Java Code Examples for io.protostuff.compiler.model.Field#getType()

The following examples show how to use io.protostuff.compiler.model.Field#getType() . 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: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * Returns map field value type name.
 */
public static String getMapFieldValueType(Field field) {
    FieldType type = field.getType();
    if (!(type instanceof Message)) {
        throw new IllegalArgumentException(field.toString());
    }
    Message entryType = (Message) type;
    Type valueType = entryType.getField(MAP_ENTRY_VALUE).getType();
    String v;
    if (valueType instanceof ScalarFieldType) {
        ScalarFieldType scalarValueType = (ScalarFieldType) valueType;
        v = ScalarFieldTypeUtil.getWrapperType(scalarValueType);
    } else {
        UserType userType = (UserType) valueType;
        v = UserTypeUtil.getCanonicalName(userType);
    }
    return v;
}
 
Example 2
Source File: OptionsPostProcessor.java    From protostuff-compiler with Apache License 2.0 6 votes vote down vote up
private void checkFieldValue(ProtoContext context, Descriptor descriptor, Field field, DynamicMessage.Value value) {
    String fieldName = field.getName();
    FieldType fieldType = field.getType();
    DynamicMessage.Value.Type valueType = value.getType();
    if (fieldType instanceof ScalarFieldType) {
        ScalarFieldType scalarFieldType = (ScalarFieldType) fieldType;
        if (!isAssignableFrom(scalarFieldType, value)) {
            throw new ParserException(value, "Cannot set option '%s': expected %s value", fieldName, fieldType);
        }
    } else if (fieldType instanceof Enum) {
        Enum anEnum = (Enum) fieldType;
        Set<String> allowedNames = anEnum.getConstantNames();
        if (valueType != DynamicMessage.Value.Type.ENUM
                || !allowedNames.contains(value.getEnumName())) {
            throw new ParserException(value, "Cannot set option '%s': expected enum = %s", fieldName, allowedNames);
        }
    } else if (fieldType instanceof Message) {
        if (valueType != DynamicMessage.Value.Type.MESSAGE) {
            throw new ParserException(value, "Cannot set option '%s': expected message value", fieldName);
        }
        Message message = (Message) fieldType;
        processOptions(context, message, descriptor, value.getMessage());
    } else {
        throw new IllegalStateException("Unknown field type: " + fieldType);
    }
}
 
Example 3
Source File: DeserializerSchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected <T> FieldSchema<T> createScalarField(Field protoField, PropertyDescriptor propertyDescriptor) {
  if (protoField.getType().isEnum()) {
    return EnumsReadSchemas.create(protoField, propertyDescriptor);
  }

  switch ((ScalarFieldType) protoField.getType()) {
    case INT32:
      return Int32ReadSchemas.create(protoField, propertyDescriptor);
    case UINT32:
      return UInt32ReadSchemas.create(protoField, propertyDescriptor);
    case SINT32:
      return SInt32ReadSchemas.create(protoField, propertyDescriptor);
    case FIXED32:
      return Fixed32ReadSchemas.create(protoField, propertyDescriptor);
    case SFIXED32:
      return SFixed32ReadSchemas.create(protoField, propertyDescriptor);
    case INT64:
      return Int64ReadSchemas.create(protoField, propertyDescriptor);
    case UINT64:
      return UInt64ReadSchemas.create(protoField, propertyDescriptor);
    case SINT64:
      return SInt64ReadSchemas.create(protoField, propertyDescriptor);
    case FIXED64:
      return Fixed64ReadSchemas.create(protoField, propertyDescriptor);
    case SFIXED64:
      return SFixed64ReadSchemas.create(protoField, propertyDescriptor);
    case FLOAT:
      return FloatReadSchemas.create(protoField, propertyDescriptor);
    case DOUBLE:
      return DoubleReadSchemas.create(protoField, propertyDescriptor);
    case BOOL:
      return BoolReadSchemas.create(protoField, propertyDescriptor);
    case STRING:
      return StringReadSchemas.create(protoField, propertyDescriptor);
    case BYTES:
      return BytesReadSchemas.create(protoField, propertyDescriptor);
    default:
      throw new IllegalStateException("unknown proto field type: " + protoField.getType());
  }
}
 
Example 4
Source File: ProtoUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
/**
 * all supported type, default to packed
 * @param protoField
 * @return
 */
public static boolean isSupportPacked(Field protoField) {
  if (protoField.getType().isEnum()) {
    return true;
  }

  if (protoField.getType().isScalar()) {
    ScalarFieldType scalarFieldType = (ScalarFieldType) protoField.getType();
    return scalarFieldType != ScalarFieldType.STRING && scalarFieldType != ScalarFieldType.BYTES;
  }

  return false;
}
 
Example 5
Source File: SerializerSchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected <T> FieldSchema<T> createScalarField(Field protoField, PropertyDescriptor propertyDescriptor) {
  if (protoField.getType().isEnum()) {
    return EnumWriteSchemas.create(protoField, propertyDescriptor);
  }

  switch ((ScalarFieldType) protoField.getType()) {
    case INT32:
      return Int32WriteSchemas.create(protoField, propertyDescriptor);
    case UINT32:
      return UInt32WriteSchemas.create(protoField, propertyDescriptor);
    case SINT32:
      return SInt32WriteSchemas.create(protoField, propertyDescriptor);
    case FIXED32:
      return Fixed32WriteSchemas.create(protoField, propertyDescriptor);
    case SFIXED32:
      return SFixed32WriteSchemas.create(protoField, propertyDescriptor);
    case INT64:
      return Int64WriteSchemas.create(protoField, propertyDescriptor);
    case UINT64:
      return UInt64WriteSchemas.create(protoField, propertyDescriptor);
    case SINT64:
      return SInt64WriteSchemas.create(protoField, propertyDescriptor);
    case FIXED64:
      return Fixed64WriteSchemas.create(protoField, propertyDescriptor);
    case SFIXED64:
      return SFixed64WriteSchemas.create(protoField, propertyDescriptor);
    case FLOAT:
      return FloatWriteSchemas.create(protoField, propertyDescriptor);
    case DOUBLE:
      return DoubleWriteSchemas.create(protoField, propertyDescriptor);
    case BOOL:
      return BoolWriteSchemas.create(protoField, propertyDescriptor);
    case STRING:
      return StringWriteSchemas.create(protoField, propertyDescriptor);
    case BYTES:
      return BytesWriteSchemas.create(protoField, propertyDescriptor);
    default:
      throw new IllegalStateException("unknown proto field type: " + protoField.getType());
  }
}
 
Example 6
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Returns map field key type name.
 */
public static String getMapFieldKeyType(Field field) {
    FieldType type = field.getType();
    if (!(type instanceof Message)) {
        throw new IllegalArgumentException(field.toString());
    }
    Message entryType = (Message) type;
    ScalarFieldType keyType = (ScalarFieldType) entryType.getField(MAP_ENTRY_KEY).getType();
    return ScalarFieldTypeUtil.getWrapperType(keyType);
}
 
Example 7
Source File: ProtoToStringGenerator.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private void fieldMapToString(Field field, StringBuilder sb) {
  Message entryMessage = (Message) field.getType();
  Field keyField = entryMessage.getField(1);
  Field valueField = entryMessage.getField(2);

  // map<string, string> name = 1;
  appendLine(sb, "map<%s, %s> %s = %d;", keyField.getTypeName(), valueField.getTypeName(), field.getName(),
      field.getTag());
}
 
Example 8
Source File: MapTest.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
private void checkMap(Message m, String mapFieldName, ScalarFieldType keyType, String valueType, int tag) {
    Field field = m.getField(mapFieldName);
    Message type = (Message) field.getType();
    assertEquals(keyType, type.getField(MAP_ENTRY_KEY).getType());
    assertEquals(valueType, type.getField(MAP_ENTRY_VALUE).getType().getFullyQualifiedName());
    assertEquals(tag, field.getTag());
}
 
Example 9
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
private static String protostuffIoMethodName(Field field, String operation) {
    FieldType type = field.getType();
    if (!(type instanceof ScalarFieldType)) {
        throw new IllegalArgumentException(String.valueOf(type));
    }
    ScalarFieldType fieldType = (ScalarFieldType) type;
    String name = PROTOSTUFF_IO_NAME.get(fieldType);
    if (name == null) {
        throw new IllegalArgumentException(String.valueOf(type));
    }
    return operation + name;
}
 
Example 10
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a java wrapper field type for proto scalar field.
 */
public static String getWrapperFieldType(Field field) {
    FieldType type = field.getType();
    if (type instanceof ScalarFieldType) {
        ScalarFieldType scalarFieldType = (ScalarFieldType) type;
        return ScalarFieldTypeUtil.getWrapperType(scalarFieldType);
    }
    if (type instanceof UserType) {
        UserType userType = (UserType) type;
        return UserTypeUtil.getCanonicalName(userType);
    }
    throw new IllegalArgumentException(field.toString());
}
 
Example 11
Source File: JsonMessageGenerator.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
private String copyDescriptionFromFieldType(Field field) {
    String comments;
    FieldType type = field.getType();
    if (type instanceof Message) {
        comments = ((Message) type).getComments();
    } else if (type instanceof Enum) {
        comments = ((Enum) type).getComments();
    } else {
        comments = "";
    }
    return comments;
}
 
Example 12
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an iterable wrapper for proto repeated field.
 */
public static String getIterableFieldType(Field field) {
    FieldType type = field.getType();
    if (type instanceof ScalarFieldType) {
        ScalarFieldType scalarFieldType = (ScalarFieldType) type;
        return ITERABLE + "<" + ScalarFieldTypeUtil.getWrapperType(scalarFieldType) + ">";
    }
    if (type instanceof UserType) {
        UserType userType = (UserType) type;
        return ITERABLE + "<" + UserTypeUtil.getCanonicalName(userType) + ">";
    }
    throw new IllegalArgumentException(field.toString());
}
 
Example 13
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a java field type for proto repeated field.
 */
public static String getRepeatedFieldType(Field field) {
    FieldType type = field.getType();
    if (type instanceof ScalarFieldType) {
        ScalarFieldType scalarFieldType = (ScalarFieldType) type;
        return LIST + "<" + ScalarFieldTypeUtil.getWrapperType(scalarFieldType) + ">";
    }
    if (type instanceof UserType) {
        UserType userType = (UserType) type;
        return LIST + "<" + UserTypeUtil.getCanonicalName(userType) + ">";
    }
    throw new IllegalArgumentException(field.toString());
}
 
Example 14
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Check if field type is numeric.
 */
public static boolean isNumericType(Field field) {
    FieldType type = field.getType();
    boolean scalar = type instanceof ScalarFieldType;
    return scalar && !(BOOL.equals(type) || STRING.equals(type) || BYTES.equals(type));
}
 
Example 15
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Check if field type used to store value in java is nullable type.
 */
public static boolean isScalarNullableType(Field field) {
    FieldType type = field.getType();
    return STRING.equals(type) || BYTES.equals(type) || type instanceof io.protostuff.compiler.model.Enum;
}
 
Example 16
Source File: MessageFieldUtil.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Check if field type is a message.
 */
public static boolean isMessage(Field field) {
    return field.getType() instanceof Message;
}
 
Example 17
Source File: JsonMessageGenerator.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
private String getMapKeyType(Field field) {
    Message message = (Message) field.getType();
    Field key = message.getField("key");
    return key.getType().getCanonicalName();
}
 
Example 18
Source File: SerializerSchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> FieldSchema<T> createRepeatedSchema(Field protoField, PropertyDescriptor propertyDescriptor) {
  boolean packed = ProtoUtils.isPacked(protoField);
  if (protoField.getType().isEnum()) {
    return packed ? EnumPackedWriteSchemas.create(protoField, propertyDescriptor) :
        EnumNotPackedWriteSchemas.create(protoField, propertyDescriptor);
  }

  if (protoField.getType().isScalar()) {
    switch ((ScalarFieldType) protoField.getType()) {
      case INT32:
        return packed ? Int32PackedWriteSchemas.create(protoField, propertyDescriptor) :
            Int32NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case UINT32:
        return packed ? UInt32PackedWriteSchemas.create(protoField, propertyDescriptor) :
            UInt32NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case SINT32:
        return packed ? SInt32PackedWriteSchemas.create(protoField, propertyDescriptor) :
            SInt32NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case FIXED32:
        return packed ? Fixed32PackedWriteSchemas.create(protoField, propertyDescriptor) :
            Fixed32NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case SFIXED32:
        return packed ? SFixed32PackedWriteSchemas.create(protoField, propertyDescriptor) :
            SFixed32NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case INT64:
        return packed ? Int64PackedWriteSchemas.create(protoField, propertyDescriptor) :
            Int64NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case UINT64:
        return packed ? UInt64PackedWriteSchemas.create(protoField, propertyDescriptor) :
            UInt64NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case SINT64:
        return packed ? SInt64PackedWriteSchemas.create(protoField, propertyDescriptor) :
            SInt64NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case FIXED64:
        return packed ? Fixed64PackedWriteSchemas.create(protoField, propertyDescriptor) :
            Fixed64NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case SFIXED64:
        return packed ? SFixed64PackedWriteSchemas.create(protoField, propertyDescriptor) :
            SFixed64NotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case FLOAT:
        return packed ? FloatPackedWriteSchemas.create(protoField, propertyDescriptor) :
            FloatNotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case DOUBLE:
        return packed ? DoublePackedWriteSchemas.create(protoField, propertyDescriptor) :
            DoubleNotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case BOOL:
        return packed ? BoolPackedWriteSchemas.create(protoField, propertyDescriptor) :
            BoolNotPackedWriteSchemas.create(protoField, propertyDescriptor);
      case STRING:
        return StringsRepeatedWriteSchemas.create(protoField, propertyDescriptor);
      case BYTES:
        return BytesRepeatedWriteSchemas.create(protoField, propertyDescriptor);
    }
  }

  if (ProtoUtils.isAnyField(protoField)) {
    FieldSchema<T> anySchema = new AnySchema<>(protoMapper, protoField, propertyDescriptor);
    return AnyRepeatedWriteSchemas.create(protoField, propertyDescriptor, anySchema);
  }

  if (protoField.getType().isMessage()) {
    JavaType contentType = propertyDescriptor.getJavaType().getContentType();
    if (contentType == null) {
      contentType = ProtoConst.OBJECT_TYPE;
    }
    SchemaEx<Object> contentSchema = getOrCreateMessageSchema((Message) protoField.getType(), contentType);
    if (isWrapProperty((Message) protoField.getType())) {
      return PropertyWrapperRepeatedWriteSchemas.create(protoField, propertyDescriptor, contentSchema);
    }

    return MessagesRepeatedWriteSchemas.create(protoField, propertyDescriptor, contentSchema);
  }

  ProtoUtils.throwNotSupportWrite(protoField, propertyDescriptor.getJavaType().getRawClass());
  return null;
}
 
Example 19
Source File: JsonMessageGenerator.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
private String getMapValueType(Field field) {
    Message message = (Message) field.getType();
    Field value = message.getField("value");
    return value.getType().getCanonicalName();
}
 
Example 20
Source File: DeserializerSchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> FieldSchema<T> createRepeatedSchema(Field protoField, PropertyDescriptor propertyDescriptor) {
  boolean packed = ProtoUtils.isPacked(protoField);
  if (protoField.getType().isEnum()) {
    return packed ? EnumPackedReadSchemas.create(protoField, propertyDescriptor) :
        EnumNotPackedReadSchemas.create(protoField, propertyDescriptor);
  }

  if (protoField.getType().isScalar()) {
    switch ((ScalarFieldType) protoField.getType()) {
      case INT32:
        return packed ? Int32PackedReadSchemas.create(protoField, propertyDescriptor) :
            Int32NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case UINT32:
        return packed ? UInt32PackedReadSchemas.create(protoField, propertyDescriptor) :
            UInt32NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case SINT32:
        return packed ? SInt32PackedReadSchemas.create(protoField, propertyDescriptor) :
            SInt32NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case FIXED32:
        return packed ? Fixed32PackedReadSchemas.create(protoField, propertyDescriptor) :
            Fixed32NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case SFIXED32:
        return packed ? SFixed32PackedReadSchemas.create(protoField, propertyDescriptor) :
            SFixed32NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case INT64:
        return packed ? Int64PackedReadSchemas.create(protoField, propertyDescriptor) :
            Int64NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case UINT64:
        return packed ? UInt64PackedReadSchemas.create(protoField, propertyDescriptor) :
            UInt64NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case SINT64:
        return packed ? SInt64PackedReadSchemas.create(protoField, propertyDescriptor) :
            SInt64NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case FIXED64:
        return packed ? Fixed64PackedReadSchemas.create(protoField, propertyDescriptor) :
            Fixed64NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case SFIXED64:
        return packed ? SFixed64PackedReadSchemas.create(protoField, propertyDescriptor) :
            SFixed64NotPackedReadSchemas.create(protoField, propertyDescriptor);
      case FLOAT:
        return packed ? FloatPackedReadSchemas.create(protoField, propertyDescriptor) :
            FloatNotPackedReadSchemas.create(protoField, propertyDescriptor);
      case DOUBLE:
        return packed ? DoublePackedReadSchemas.create(protoField, propertyDescriptor) :
            DoubleNotPackedReadSchemas.create(protoField, propertyDescriptor);
      case BOOL:
        return packed ? BoolPackedReadSchemas.create(protoField, propertyDescriptor) :
            BoolNotPackedReadSchemas.create(protoField, propertyDescriptor);
      case STRING:
        return StringRepeatedReadSchemas.create(protoField, propertyDescriptor);
      case BYTES:
        return BytesRepeatedReadSchemas.create(protoField, propertyDescriptor);
    }
  }

  if (ProtoUtils.isAnyField(protoField)) {
    AnyEntrySchema anyEntrySchema = new AnyEntrySchema(protoMapper, null);
    return AnyRepeatedReadSchemas.create(protoField, propertyDescriptor, anyEntrySchema);
  }

  if (protoField.getType().isMessage()) {
    JavaType contentType = propertyDescriptor.getJavaType().getContentType();
    if (contentType == null) {
      contentType = ProtoConst.OBJECT_TYPE;
    }
    SchemaEx<Object> contentSchema = getOrCreateMessageSchema((Message) protoField.getType(), contentType);
    if (isWrapProperty((Message) protoField.getType())) {
      return PropertyWrapperRepeatedReadSchemas.create(protoField, propertyDescriptor, contentSchema);
    }

    return MessageRepeatedReadSchemas.create(protoField, propertyDescriptor, contentSchema);
  }
  ProtoUtils.throwNotSupportMerge(protoField, propertyDescriptor.getJavaType());
  return null;
}