當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。