本文整理汇总了Java中org.springframework.core.convert.ConversionService.convert方法的典型用法代码示例。如果您正苦于以下问题:Java ConversionService.convert方法的具体用法?Java ConversionService.convert怎么用?Java ConversionService.convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.core.convert.ConversionService
的用法示例。
在下文中一共展示了ConversionService.convert方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getArguments
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
protected Object[] getArguments(Step step, StepImplementation implementation) {
Method method = implementation.getMethod();
Parameter[] parameters = method.getParameters();
Object[] arguments = new Object[parameters.length];
if (parameters.length > 0) {
String text = step.getText();
Pattern pattern = implementation.getPattern();
Matcher matcher = pattern.matcher(text);
checkState(matcher.find(),
"unable to locate substitution parameters for pattern %s with input %s", pattern.pattern(), text);
int groupCount = matcher.groupCount();
ConversionService conversionService = SpringPreProcessor.getBean(ConversionService.class);
for (int i = 0; i < groupCount; i++) {
String parameterAsString = matcher.group(i + 1);
Parameter parameter = parameters[i];
Class<?> parameterType = parameter.getType();
Object converted = conversionService.convert(parameterAsString, parameterType);
arguments[i] = converted;
}
}
return arguments;
}
示例2: normaliseValueForPut
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
public static Object normaliseValueForPut(Object value, ConversionService conversionService) {
if (value instanceof Document) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData.class));
}
else if (value instanceof Document[]) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData[].class));
}
else if (value instanceof Iterable<?>) {
if (CollectionUtil.areAllElementsOfType((Collection<?>) value, Document.class)) {
return conversionService.convert(value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(IData[].class));
}
else {
return value;
}
}
else {
return value;
}
}
示例3: isValidValueFor
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Check if a string is valid for a type <br/>
* If conversion service is not provided try to check by apache commons
* utilities. <b>TODO</b> in this (no-conversionService) case just
* implemented for numerics
*
* @param string
* @param typeDescriptor
* @param conversionService (optional)
* @return
*/
private static boolean isValidValueFor(String string,
TypeDescriptor typeDescriptor, ConversionService conversionService) {
if (conversionService != null) {
try {
conversionService.convert(string, STRING_TYPE_DESCRIPTOR,
typeDescriptor);
}
catch (ConversionException e) {
return false;
}
return true;
}
else {
Class<?> fieldType = typeDescriptor.getType();
if (Number.class.isAssignableFrom(fieldType)
|| NUMBER_PRIMITIVES.contains(fieldType)) {
return NumberUtils.isNumber(string);
}
// TODO implement other types
return true;
}
}
示例4: 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;
}
示例5: convertProperty
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
private static Object convertProperty(BeanWrapper parentBean, String property,
ConversionService conversionService, Map<String, Object> parentValue) {
TypeDescriptor source = parentBean.getPropertyTypeDescriptor(property);
Object propertyValue = parentBean.getPropertyValue(property);
int dotIndex = property.indexOf('.');
if (dotIndex > 0) {
String baseProperty = property.substring(0, dotIndex);
String childProperty = property.substring(dotIndex + 1);
Map<String, Object> childValue = getChildValue(parentValue, baseProperty, childProperty);
BeanWrapper childBean = new BeanWrapperImpl(parentBean.getPropertyValue(baseProperty));
Object convertedProperty =
convertProperty(childBean, childProperty, conversionService, childValue);
childValue.put(childProperty, convertedProperty);
return childValue;
} else {
String convertedPropertyValue =
(String) conversionService.convert(propertyValue, source, TYPE_STRING);
return convertedPropertyValue;
}
}
示例6: convertValueToDurationIfPossible
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
private String convertValueToDurationIfPossible(final String value) {
try {
final ConversionService service = applicationContext.getEnvironment().getConversionService();
final Duration dur = service.convert(value, Duration.class);
if (dur != null) {
return String.valueOf(dur.toMillis());
}
} catch (final ConversionFailedException e) {
LOGGER.trace(e.getMessage());
}
return null;
}
示例7: 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);
}
示例8: formatUriValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* 把参数格式化成字符串用于拼接url
*
* @param cs
* @param sourceType
* @param value
* @return dummy
*/
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
if (value == null) {
return null;
} else if (value instanceof String) {
return (String) value;
} else if (cs != null) {
return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR);
} else {
return value.toString();
}
}
示例9: formatUriValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
if (value == null) {
return null;
}
else if (value instanceof String) {
return (String) value;
}
else if (cs != null) {
return (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR);
}
else {
return value.toString();
}
}
示例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: formatUriValue
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
protected String formatUriValue(ConversionService cs, TypeDescriptor sourceType, Object value) {
return (cs != null ? (String) cs.convert(value, sourceType, STRING_TYPE_DESCRIPTOR) : null);
}
示例12: 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 "";
}
}
示例13: createNumberExpression
import org.springframework.core.convert.ConversionService; //导入方法依赖的package包/类
/**
* Return where clause expression for number properties by casting it to
* string before check its value.
* <p/>
* Querydsl Expr:
* {@code entityPath.fieldName.stringValue() like ('%' + searchStr + '%')}
* Database operation:
* {@code str(entity.fieldName) like ('%' + searchStr + '%')}
* <p/>
* Like operation is case sensitive.
*
* @param entityPath Full path to entity and associations. For example:
* {@code Pet} , {@code Pet.owner}
* @param fieldName Property name in the given entity path. For example:
* {@code weight} in {@code Pet} entity, {@code age} in
* {@code Pet.owner} entity.
* @param searchStr the value to find, may be null
* @return PredicateOperation
*/
public static <T, N extends java.lang.Number & java.lang.Comparable<?>> BooleanExpression createNumberExpression(
PathBuilder<T> entityPath, String fieldName, Class<N> fieldType,
TypeDescriptor descriptor, String searchStr,
ConversionService conversionService) {
if (StringUtils.isBlank(searchStr)) {
return null;
}
NumberPath<N> numberExpression = entityPath.getNumber(fieldName,
fieldType);
BooleanExpression expression = null;
if (conversionService != null) {
try {
Object number = conversionService.convert(searchStr,
STRING_TYPE_DESCRIPTOR, descriptor);
if (number == null) {
expression = numberExpression.stringValue().like(
"%".concat(searchStr).concat("%"));
}
else {
String toSearch = number.toString();
if (number instanceof BigDecimal
&& ((BigDecimal) number).scale() > 1) {
// For bigDecimal trim 0 in decimal part
toSearch = StringUtils.stripEnd(toSearch, "0");
if (StringUtils.endsWith(toSearch, ".")) {
// prevent "#." strings
toSearch = toSearch.concat("0");
}
}
expression = numberExpression.stringValue().like(
"%".concat(toSearch).concat("%"));
}
}
catch (ConversionException e) {
expression = numberExpression.stringValue().like(
"%".concat(searchStr).concat("%"));
}
}
else {
expression = numberExpression.stringValue().like(
"%".concat(searchStr).concat("%"));
}
return expression;
}