下面列出了org.springframework.core.convert.TypeDescriptor#valueOf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
String text = (String) source;
if (!StringUtils.hasText(text)) {
return null;
}
Object result;
try {
result = this.parser.parse(text, LocaleContextHolder.getLocale());
}
catch (ParseException ex) {
throw new IllegalArgumentException("Unable to parse '" + text + "'", ex);
}
if (result == null) {
throw new IllegalStateException("Parsers are not allowed to return null");
}
TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass());
if (!resultType.isAssignableTo(targetType)) {
result = this.conversionService.convert(result, resultType, targetType);
}
return result;
}
/**
* Formats the field value based on registered PropertyEditors.
* @see #getCustomEditor
*/
@Override
protected Object formatFieldValue(String field, Object value) {
String fixedField = fixedField(field);
// Try custom editor...
PropertyEditor customEditor = getCustomEditor(fixedField);
if (customEditor != null) {
customEditor.setValue(value);
String textValue = customEditor.getAsText();
// If the PropertyEditor returned null, there is no appropriate
// text representation for this value: only use it if non-null.
if (textValue != null) {
return textValue;
}
}
if (this.conversionService != null) {
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
}
/**
* Formats the field value based on registered PropertyEditors.
* @see #getCustomEditor
*/
@Override
protected Object formatFieldValue(String field, Object value) {
String fixedField = fixedField(field);
// Try custom editor...
PropertyEditor customEditor = getCustomEditor(fixedField);
if (customEditor != null) {
customEditor.setValue(value);
String textValue = customEditor.getAsText();
// If the PropertyEditor returned null, there is no appropriate
// text representation for this value: only use it if non-null.
if (textValue != null) {
return textValue;
}
}
if (this.conversionService != null) {
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
}
@Test
public void convertNullAnnotatedStringToString() throws Exception {
String source = null;
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString"));
TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
conversionService.convert(source, sourceType, targetType);
}
/**
* Convert operand value to string using registered converter or using
* {@code toString} method.
* @param value typed value to be converted
* @param state expression state
* @return {@code TypedValue} instance converted to {@code String}
*/
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
return String.valueOf(typeConverter.convertValue(value.getValue(),
value.getTypeDescriptor(), typeDescriptor));
}
return String.valueOf(value.getValue());
}
@Test
public void emptyListToArray() {
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<>();
TypeDescriptor sourceType = TypeDescriptor.forObject(list);
TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class);
assertTrue(conversionService.canConvert(sourceType, targetType));
assertEquals(0, ((String[]) conversionService.convert(list, sourceType, targetType)).length);
}
public byte[] convert(Map<Object, Object> map) {
try {
return mapper.writeValueAsBytes(map);
} catch (IOException e) {
throw new ConversionFailedException(
TypeDescriptor.valueOf(Map.class),
TypeDescriptor.valueOf(byte[].class),
map, e);
}
}
/**
* Convert operand value to string using registered converter or using
* {@code toString} method.
* @param value typed value to be converted
* @param state expression state
* @return {@code TypedValue} instance converted to {@code String}
*/
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
return String.valueOf(typeConverter.convertValue(value.getValue(),
value.getTypeDescriptor(), typeDescriptor));
}
return String.valueOf(value.getValue());
}
/**
* Convert operand value to string using registered converter or using
* {@code toString} method.
* @param value typed value to be converted
* @param state expression state
* @return {@code TypedValue} instance converted to {@code String}
*/
private static String convertTypedValueToString(TypedValue value, ExpressionState state) {
TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class);
if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) {
return String.valueOf(typeConverter.convertValue(value.getValue(),
value.getTypeDescriptor(), typeDescriptor));
}
return String.valueOf(value.getValue());
}
private void extendMapIfNecessary(BeanWrapper wrapper, RelaxedDataBinder.BeanPath path, int index) {
String name = path.prefix(index);
TypeDescriptor parent = wrapper.getPropertyTypeDescriptor(name);
if (parent == null) {
return;
}
TypeDescriptor descriptor = parent.getMapValueTypeDescriptor();
if (descriptor == null) {
descriptor = TypeDescriptor.valueOf(Object.class);
}
if (!descriptor.isMap() && !descriptor.isCollection() && !descriptor.getType().equals(Object.class)) {
return;
}
String extensionName = path.prefix(index + 1);
if (wrapper.isReadableProperty(extensionName)) {
Object currentValue = wrapper.getPropertyValue(extensionName);
if ((descriptor.isCollection() && currentValue instanceof Collection)
|| (!descriptor.isCollection() && currentValue instanceof Map)) {
return;
}
}
Object extend = new LinkedHashMap<String, Object>();
if (descriptor.isCollection()) {
extend = new ArrayList<Object>();
}
if (descriptor.getType().equals(Object.class) && path.isLastNode(index)) {
extend = BLANK;
}
wrapper.setPropertyValue(extensionName, extend);
}
@Override
public SalespointIdentifier convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Class<?> targetClass = targetType.getType();
try {
Constructor<?> constructor = targetClass.getDeclaredConstructor(String.class);
return (SalespointIdentifier) BeanUtils.instantiateClass(constructor, source);
} catch (NoSuchMethodException | SecurityException o_O) {
throw new ConversionFailedException(TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetClass), source,
o_O);
}
}
/**
* Converts the given {@link Object} into {@link String JSON}.
*
* @param source {@link Object} to convert into {@link String JSON}.
* @return {@link String JSON} generated from the given {@link Object} using Jackson's {@link ObjectMapper}.
* @throws IllegalArgumentException if {@link Object source} is {@literal null}.
* @throws ConversionFailedException if a {@link JsonProcessingException} is thrown or another error occurs
* while trying to convert the given {@link Object} to {@link String JSON}.
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see #convertObjectToJson(Object)
*/
@Override
public @NonNull String convert(@NonNull Object source) {
Assert.notNull(source, "Source object to convert must not be null");
try {
return convertObjectToJson(source);
}
catch (JsonProcessingException cause) {
throw new ConversionFailedException(TypeDescriptor.forObject(source), TypeDescriptor.valueOf(String.class),
source, cause);
}
}
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
return TypeDescriptor.valueOf(String.class);
}
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
throws EvaluationException {
return TypeDescriptor.valueOf(String.class);
}
@Override
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
return TypeDescriptor.valueOf(String.class);
}
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
throws EvaluationException {
return TypeDescriptor.valueOf(String.class);
}
@Override
public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.valueOf(String.class);
}
@Override
public TypeDescriptor getValueTypeDescriptor() {
return TypeDescriptor.valueOf(String.class);
}
/**
* Returns the type being converted. By default the type is resolved using
* the generic arguments of the class.
*/
protected TypeDescriptor getType() {
return TypeDescriptor.valueOf(resolveTypeArguments()[0]);
}
/**
* Returns the websocket message type. By default the type is resolved using
* the generic arguments of the class.
*/
protected TypeDescriptor getMessageType() {
return TypeDescriptor.valueOf(resolveTypeArguments()[1]);
}