当前位置: 首页>>代码示例>>Java>>正文


Java ConversionService.canConvert方法代码示例

本文整理汇总了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;
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:ConversionUtils.java

示例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;
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:29,代码来源:ServletModelAttributeMethodProcessor.java

示例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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:17,代码来源:NamespaceModelAttributeMethodProcessor.java

示例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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:31,代码来源:FormModelMethodArgumentResolver.java

示例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;
}
 
开发者ID:thinking-github,项目名称:nbone,代码行数:30,代码来源:FormModelMethodArgumentResolver.java

示例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;
  }
 
开发者ID:DISID,项目名称:springlets,代码行数:21,代码来源:ConvertedDatatablesData.java

示例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;
}
 
开发者ID:innodev-au,项目名称:wmboost-data,代码行数:10,代码来源:OverlayedConversionService.java

示例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);
}
 
开发者ID:innodev-au,项目名称:wmboost-data,代码行数:11,代码来源:OverlayedConversionService.java

示例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));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:ConvertingPropertyEditorAdapter.java

示例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;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:15,代码来源:SpringTypeConverter.java

示例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 "";
    }
}
 
开发者ID:gvSIGAssociation,项目名称:gvnix1,代码行数:68,代码来源:DatatablesUtils.java

示例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;
}
 
开发者ID:xiangxik,项目名称:java-platform,代码行数:36,代码来源:EntityModelAttributeMethodProcessor.java


注:本文中的org.springframework.core.convert.ConversionService.canConvert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。