Java Code Examples for org.springframework.beans.BeanWrapper#getPropertyTypeDescriptor()

The following examples show how to use org.springframework.beans.BeanWrapper#getPropertyTypeDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConvertedDatatablesData.java    From springlets with Apache License 2.0 6 votes vote down vote up
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;
  }
 
Example 2
Source File: ConvertedDatatablesData.java    From springlets with Apache License 2.0 6 votes vote down vote up
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);
    }
  }
}
 
Example 3
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@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;
}