List of usage examples for org.springframework.core.convert TypeDescriptor isAssignableTo
public boolean isAssignableTo(TypeDescriptor typeDescriptor)
From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) { return targetType.isAssignableTo(BOOLEAN_TYPE) && (sourceType.isAssignableTo(NUMBER_TYPE) || sourceType.isCollection() || sourceType.isArray()) || decorated.canConvert(sourceType, targetType); }
From source file:cz.jirutka.validator.spring.support.RelaxedBooleanTypeConverterDecorator.java
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.isAssignableTo(BOOLEAN_TYPE)) { if (value instanceof Number) { return ((Number) value).intValue() != 0; }/*from ww w .j ava 2 s . c om*/ if (sourceType.isCollection()) { return !((Collection) value).isEmpty(); } if (sourceType.isArray()) { return ((Object[]) value).length != 0; } return value; } else { return decorated.convertValue(value, sourceType, targetType); } }
From source file:org.springframework.core.convert.support.GenericConversionService.java
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { assertNotNull(sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug(//from w w w .j a v a2 s . c o m "Converting value " + StylerUtils.style(source) + " of " + sourceType + " to " + targetType); } if (sourceType == TypeDescriptor.NULL) { Assert.isTrue(source == null, "The value must be null if sourceType == TypeDescriptor.NULL"); Object result = convertNullSource(sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug("Converted to " + StylerUtils.style(result)); } return result; } if (targetType == TypeDescriptor.NULL) { logger.debug("Converted to null"); return null; } Assert.isTrue(source == null || sourceType.getObjectType().isInstance(source)); GenericConverter converter = getConverter(sourceType, targetType); if (converter == null) { if (source == null || sourceType.isAssignableTo(targetType)) { logger.debug("No converter found - returning assignable source object as-is"); return source; } else { throw new ConverterNotFoundException(sourceType, targetType); } } Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType); if (logger.isDebugEnabled()) { logger.debug("Converted to " + StylerUtils.style(result)); } return result; }
From source file:org.springframework.core.convert.support.GenericConversionService.java
/** * Return the default converter if no converter is found for the given sourceType/targetType pair. * Returns a NO_OP Converter if the sourceType is assignable to the targetType. * Returns <code>null</code> otherwise, indicating no suitable converter could be found. * Subclasses may override./* w ww .ja v a 2s . c o m*/ * @param sourceType the source type to convert from * @param targetType the target type to convert to * @return the default generic converter that will perform the conversion */ protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.isAssignableTo(targetType)) { logger.trace("Matched default NO_OP_CONVERTER"); return NO_OP_CONVERTER; } else { return null; } }