Java Code Examples for org.springframework.core.convert.TypeDescriptor#valueOf()

The following examples show how to use org.springframework.core.convert.TypeDescriptor#valueOf() . 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: AbstractPropertyBindingResult.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: AbstractPropertyBindingResult.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: OpPlus.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 5
Source File: JacksonObjectToJsonConverter.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example 6
Source File: SalespointIdentifierConverter.java    From salespoint with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 7
Source File: RelaxedDataBinder.java    From canal with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: OpPlus.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example 9
Source File: OpPlus.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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());
}
 
Example 10
Source File: MapToJsonByteArrayConverter.java    From spring-integration-zmq with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 11
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 12
Source File: GenericConversionServiceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 13
Source File: CompositeStringExpression.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
		throws EvaluationException {

	return TypeDescriptor.valueOf(String.class);
}
 
Example 14
Source File: CompositeStringExpression.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
	return TypeDescriptor.valueOf(String.class);
}
 
Example 15
Source File: CompositeStringExpression.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
	return TypeDescriptor.valueOf(String.class);
}
 
Example 16
Source File: CompositeStringExpression.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
		throws EvaluationException {

	return TypeDescriptor.valueOf(String.class);
}
 
Example 17
Source File: LiteralExpression.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor() {
	return TypeDescriptor.valueOf(String.class);
}
 
Example 18
Source File: CompositeStringExpression.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TypeDescriptor getValueTypeDescriptor() {
	return TypeDescriptor.valueOf(String.class);
}
 
Example 19
Source File: ConvertingEncoderDecoderSupport.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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]);
}
 
Example 20
Source File: ConvertingEncoderDecoderSupport.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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]);
}