下面列出了org.springframework.beans.BeanWrapper#getPropertyTypeDescriptor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
private static Object convertProperty(BeanWrapper parentBean, String property,
ConversionService conversionService, String propertySeparator) {
int dotIndex = property.indexOf(propertySeparator);
if (dotIndex > 0) {
String baseProperty = property.substring(0, dotIndex);
String childProperty = property.substring(dotIndex + propertySeparator.length());
BeanWrapper childBean = new BeanWrapperImpl(parentBean.getPropertyValue(baseProperty));
return convertProperty(childBean, childProperty, conversionService, propertySeparator);
} else {
TypeDescriptor source = parentBean.getPropertyTypeDescriptor(property);
Object propertyValue = parentBean.getPropertyValue(property);
if (source.isAssignableTo(TYPE_STRING)) {
return (String) propertyValue;
} else {
return (String) conversionService.convert(propertyValue, source, TYPE_STRING);
}
}
}
/**
* Obtains the descriptor of the filtered field
*
* @param fieldName
* @param entityType
* @return
*/
public static <T> TypeDescriptor getTypeDescriptor(String fieldName,
Class<T> entityType) {
String fieldNameToFindType = fieldName;
BeanWrapper beanWrapper = getBeanWrapper(entityType);
TypeDescriptor fieldDescriptor = null;
Class<?> propType = null;
// Find recursive the las beanWrapper
if (fieldName.contains(SEPARATOR_FIELDS)) {
String[] fieldNameSplitted = StringUtils.split(fieldName,
SEPARATOR_FIELDS);
for (int i = 0; i < fieldNameSplitted.length - 1; i++) {
propType = beanWrapper.getPropertyType(fieldNameSplitted[i]);
if (propType == null) {
throw new IllegalArgumentException(String.format(
"Property %s not found in %s (request %s.%s)",
fieldNameSplitted[i],
beanWrapper.getWrappedClass(), entityType,
fieldName));
}
beanWrapper = getBeanWrapper(propType);
}
fieldNameToFindType = fieldNameSplitted[fieldNameSplitted.length - 1];
}
fieldDescriptor = beanWrapper
.getPropertyTypeDescriptor(fieldNameToFindType);
return fieldDescriptor;
}
/**
* {@inheritDoc}
*/
@Override
public <T> TypeDescriptor getTypeDescriptor(String fieldName,
Class<T> entityType) {
String fieldNameToFindType = fieldName;
BeanWrapper beanWrapper = getBeanWrapper(entityType);
TypeDescriptor fieldDescriptor = null;
Class<?> propType = null;
// Find recursive the las beanWrapper
if (fieldName.contains(SEPARATOR_FIELDS)) {
String[] fieldNameSplitted = StringUtils.split(fieldName,
SEPARATOR_FIELDS);
for (int i = 0; i < fieldNameSplitted.length - 1; i++) {
propType = beanWrapper.getPropertyType(fieldNameSplitted[i]);
if (propType == null) {
throw new IllegalArgumentException(String.format(
"Property %s not found in %s (request %s.%s)",
fieldNameSplitted[i],
beanWrapper.getWrappedClass(), entityType,
fieldName));
}
beanWrapper = getBeanWrapper(propType);
}
fieldNameToFindType = fieldNameSplitted[fieldNameSplitted.length - 1];
}
fieldDescriptor = beanWrapper
.getPropertyTypeDescriptor(fieldNameToFindType);
return fieldDescriptor;
}