Java Code Examples for org.apache.commons.beanutils.PropertyUtilsBean#getPropertyDescriptors()

The following examples show how to use org.apache.commons.beanutils.PropertyUtilsBean#getPropertyDescriptors() . 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: CommChange.java    From charging_pile_cloud with MIT License 6 votes vote down vote up
/**
 * 将javabean实体类转为map类型,然后返回一个map类型的值
 * 
 * @param obj
 * @return
 */
public static SortedMap<String, String> beanToMap(Object obj) {
    SortedMap<String, String> params = new TreeMap<String, String>();
    try {
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
        for (int i = 0; i < descriptors.length; i++) {
            String name = descriptors[i].getName();
            if (!"class".equals(name)) {
                params.put(name,
                        (String) (propertyUtilsBean.getNestedProperty(obj, name) == null ? null
                                : propertyUtilsBean.getNestedProperty(obj, name)));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return params;
}
 
Example 2
Source File: BeanConfigurator.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public static void loadBean(HierarchicalConfiguration context, Object beanObject, ConvertUtilsBean converter)   
{
	PropertyUtilsBean beanUtils = new PropertyUtilsBean(); 
	
	PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(beanObject);

	try
	{
		for ( PropertyDescriptor descr : descriptors )
		{
			//check that setter exists
			if ( descr.getWriteMethod() != null )
			{
				String value = context.getString(descr.getName());

                   if(converter.lookup(descr.getPropertyType()) != null) {
                       BeanUtils.setProperty(beanObject, descr.getName(), converter.convert(value, descr.getPropertyType()));
                   }
			}
		}
	}
	catch ( Exception e )
	{
		throw new EPSCommonException(e);
	}
}
 
Example 3
Source File: ServiceStorageHelper.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public static void convertServiceSettingsToMap(Map<String, String> params, IServiceSettings serviceSettings) {
    ConvertUtilsBean converter = new ConvertUtilsBean();
    PropertyUtilsBean beanUtils = new PropertyUtilsBean();
    PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(serviceSettings);

    for (PropertyDescriptor descr : descriptors) {
        //check that setter exists
        try {
            if (descr.getWriteMethod() != null) {
                Object value = BeanUtils.getProperty(serviceSettings, descr.getName());
                params.put(descr.getName(), converter.convert(value));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
 
Example 4
Source File: AbstractSettingsProxy.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public AbstractSettingsProxy(ICommonSettings settings) {
    this.settings = settings;
    PropertyUtilsBean beanUtils = new PropertyUtilsBean();
    PropertyDescriptor[] array = beanUtils.getPropertyDescriptors(this.settings);

    this.descriptors = new HashMap<>();
    for (PropertyDescriptor propertyDescriptor : array) {
        if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
            descriptors.put(propertyDescriptor.getName(), propertyDescriptor);
        }
    }
}
 
Example 5
Source File: BeanConfigurator.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public static void saveBean(HierarchicalConfiguration context, Object beanObject)
{
	ConvertUtilsBean converter = new ConvertUtilsBean();
	
	PropertyUtilsBean beanUtils = new PropertyUtilsBean(); 
	
	PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(beanObject);

	try
	{
		for ( PropertyDescriptor descr : descriptors )
		{
			//check that setter exists
			if ( descr.getWriteMethod() != null )
			{
				Object value = BeanUtils.getProperty(beanObject, descr.getName());
				
				context.setProperty(descr.getName(), converter.convert(value));
			}
		}
	}
	catch ( Exception e )
	{
		throw new EPSCommonException(e);
	}
	
}
 
Example 6
Source File: ServiceStorageHelper.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public static void convertMapToServiceSettings(IServiceSettings serviceSettings, Map<String, String> params) {
    ConvertUtilsBean converter = new ConvertUtilsBean();
    PropertyUtilsBean beanUtils = new PropertyUtilsBean();
    PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(serviceSettings);

    converter.register(new SailfishURIConverter(), SailfishURI.class);
    converter.register(true, false, 0);

    for(PropertyDescriptor descriptor : descriptors) {
        if(descriptor.getWriteMethod() == null) {
            continue;
        }

        String name = descriptor.getName();
        String value = params.get(name);

        if(value == null) {
            continue;
        }

        try {
            BeanUtils.setProperty(serviceSettings, name, converter.convert(value, descriptor.getPropertyType()));
        } catch(Exception e) {
            throw new EPSCommonException(String.format("Failed to set setting '%s' to: %s", name, value), e);
        }
    }
}
 
Example 7
Source File: MetaDataObjectProviderBase.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
protected void createAttributes(T meta) {
	Class<?> implClass = meta.getImplementationClass();
	PropertyUtilsBean utils = BeanUtilsBean.getInstance().getPropertyUtils();
	PropertyDescriptor[] descriptors = utils.getPropertyDescriptors(implClass);
	for (PropertyDescriptor desc : descriptors) {
		if (desc.getReadMethod().getDeclaringClass() != implClass)
			continue; // contained in super type

		createAttribute(meta, desc);
	}
	
	// 
}
 
Example 8
Source File: DatabaseServiceStorage.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
private Query convertFilterToQuery(Session session, Class<?> beanClass, String queryStr, StorageFilter filter, List<SortCriterion> sorting) {

		List<FilterCriterion> list = filter.getCriteria();

		PropertyUtilsBean beanUtils = new PropertyUtilsBean();

		PropertyDescriptor[] properties = beanUtils.getPropertyDescriptors(beanClass);

		Map<String, Class<?>> beanMap = new HashMap<>();
        StringBuilder queryBld = new StringBuilder(queryStr);

        for(PropertyDescriptor descr : properties) {
            beanMap.put(descr.getName(), descr.getPropertyType());
        }

		List<Object> params = new ArrayList<>(list.size());

		for ( int i = 0; i < list.size(); ++i ) {

			FilterCriterion crit = list.get(i);
            if(i != 0) {
                queryBld.append(" and ");
            }

            queryBld.append("msg.").append(crit.getName()).append(" ").append(convertOp(crit.getOper())).append(" :param").append(i).append(" ");

			if (crit.getOper() == Operation.LIKE) {
				params.add(crit.getValue());
			} else {
				params.add(converter.convert(crit.getValue(), beanMap.get(crit.getName())));
			}
		}

        if(sorting != null && !sorting.isEmpty()) {

            queryBld.append(" order by ");

			for ( int i = 0; i < sorting.size(); ++i ) {

                if(i != 0 && i != (sorting.size() - 1)) {
                    queryBld.append(" , ");
                }

				SortCriterion entry = sorting.get(i);

                queryBld.append("msg." + entry.getName());
                queryBld.append(entry.isSortAscending() ? " asc " : " desc ");
			}
		}

		Query query = session.createQuery(queryBld.toString());

		for ( int i = 0; i < list.size(); ++i ) {
			if (list.get(i).getOper() == Operation.LIKE) {
				query.setParameter("param" + i, params.get(i), StringType.INSTANCE);
			} else {
				query.setParameter("param" + i, params.get(i));
			}
		}
		return query;
	}