本文整理汇总了Java中org.springframework.core.convert.ConversionService.canConvert方法的典型用法代码示例。如果您正苦于以下问题:Java ConversionService.canConvert方法的具体用法?Java ConversionService.canConvert怎么用?Java ConversionService.canConvert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.core.convert.ConversionService
的用法示例。
在下文中一共展示了ConversionService.canConvert方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: canConvertElements
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService) {
if (targetElementType == null) {
// yes
return true;
}
if (sourceElementType == null) {
// maybe
return true;
}
if (conversionService.canConvert(sourceElementType, targetElementType)) {
// yes
return true;
}
else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
// maybe;
return true;
}
else {
// no;
return false;
}
}
示例2: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link Converter} that can perform the conversion.
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param methodParam the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(methodParam);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
}
}
return null;
}
示例3: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
示例4: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
*
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param parameter the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
示例5: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>The default implementation converts only if there a registered
* {@link Converter} that can perform the conversion.
* @param sourceValue the source value to create the model attribute from
* @param attributeName the name of the attribute, never {@code null}
* @param parameter the method parameter
* @param binderFactory for creating WebDataBinder instance
* @param request the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue,
String attributeName,
MethodParameter parameter,
WebDataBinderFactory binderFactory,
NativeWebRequest request) throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(parameter);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
}
}
return null;
}
示例6: convert
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
private static Map<String, Object> convert(Object value, ConversionService conversionService) {
BeanWrapper bean = new BeanWrapperImpl(value);
PropertyDescriptor[] properties = bean.getPropertyDescriptors();
Map<String, Object> convertedValue = new HashMap<>(properties.length);
for (int i = 0; i < properties.length; i++) {
String name = properties[i].getName();
Object propertyValue = bean.getPropertyValue(name);
if (propertyValue != null
&& conversionService.canConvert(propertyValue.getClass(), String.class)) {
TypeDescriptor source = bean.getPropertyTypeDescriptor(name);
String convertedPropertyValue =
(String) conversionService.convert(propertyValue, source, TYPE_STRING);
convertedValue.put(name, convertedPropertyValue);
}
}
return convertedValue;
}
示例7: canConvert
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
@Override
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (ConversionService conversionService : conversionServices) {
if (conversionService.canConvert(sourceType, targetType)) {
return true;
}
}
return false;
}
示例8: convert
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
for (ConversionService conversionService : allButLast) {
if (conversionService.canConvert(sourceType, targetType)) {
return conversionService.convert(source, sourceType, targetType);
}
}
return last.convert(source, sourceType, targetType);
}
示例9: ConvertingPropertyEditorAdapter
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Create a new ConvertingPropertyEditorAdapter for a given
* {@link org.springframework.core.convert.ConversionService}
* and the given target type.
* @param conversionService the ConversionService to delegate to
* @param targetDescriptor the target type to convert to
*/
public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
Assert.notNull(conversionService, "ConversionService must not be null");
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
this.conversionService = conversionService;
this.targetDescriptor = targetDescriptor;
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
示例10: convertTo
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
// do not attempt to convert Camel types
if (type.getCanonicalName().startsWith("org.apache")) {
return null;
}
for (ConversionService conversionService : conversionServices) {
if (conversionService.canConvert(value.getClass(), type)) {
return conversionService.convert(value, type);
}
}
return null;
}
示例11: convertFieldValueToString
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Convert a field value to string
*
* @param datePatterns
* @param dateFormatters
* @param conversionService
* @param entityBean
* @param entity
* @param fieldName
* @param unescapedFieldName
* @return
*/
private static <T> String convertFieldValueToString(
Map<String, Object> datePatterns,
Map<String, SimpleDateFormat> dateFormatters,
ConversionService conversionService, BeanWrapperImpl entityBean,
T entity, String fieldName, String unescapedFieldName) {
try {
Object value = null;
TypeDescriptor fieldDesc = entityBean
.getPropertyTypeDescriptor(unescapedFieldName);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
value = entityBean.getPropertyValue(unescapedFieldName);
if (value == null) {
return "";
}
// For dates
if (Date.class.isAssignableFrom(value.getClass())
|| Calendar.class.isAssignableFrom(value.getClass())) {
SimpleDateFormat formatter = getDateFormatter(datePatterns,
dateFormatters, entityBean.getWrappedClass(),
unescapedFieldName);
if (formatter != null) {
if (Calendar.class.isAssignableFrom(value.getClass())) {
// Gets Date instance as SimpleDateFormat
// doesn't works with Calendar
value = ((Calendar) value).getTime();
}
return formatter.format(value);
}
}
String stringValue;
// Try to use conversion service (uses field descrition
// to handle field format annotations)
if (conversionService.canConvert(fieldDesc, strDesc)) {
stringValue = (String) conversionService.convert(value,
fieldDesc, strDesc);
if (stringValue == null) {
stringValue = "";
}
}
else {
stringValue = ObjectUtils.getDisplayString(value);
}
return stringValue;
}
catch (Exception ex) {
LOGGER.error(String.format(
"Error getting value of property [%s] in bean %s [%s]",
unescapedFieldName,
entity.getClass().getSimpleName(),
org.apache.commons.lang3.ObjectUtils.firstNonNull(
entity.toString(), "{unknow}")), ex);
return "";
}
}
示例12: createAttributeFromRequestValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Create a model attribute from a String request value (e.g. URI template
* variable, request parameter) using type conversion.
* <p>
* The default implementation converts only if there a registered
* {@link Converter} that can perform the conversion.
*
* @param sourceValue
* the source value to create the model attribute from
* @param attributeName
* the name of the attribute, never {@code null}
* @param methodParam
* the method parameter
* @param binderFactory
* for creating WebDataBinder instance
* @param request
* the current request
* @return the created model attribute, or {@code null}
* @throws Exception
*/
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
throws Exception {
DataBinder binder = binderFactory.createBinder(request, null, attributeName);
ConversionService conversionService = binder.getConversionService();
if (conversionService != null) {
TypeDescriptor source = TypeDescriptor.valueOf(String.class);
TypeDescriptor target = new TypeDescriptor(methodParam);
if (conversionService.canConvert(source, target)) {
return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
}
}
return null;
}