Example usage for org.apache.commons.beanutils PropertyUtilsBean getNestedProperty

List of usage examples for org.apache.commons.beanutils PropertyUtilsBean getNestedProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtilsBean getNestedProperty.

Prototype

public Object getNestedProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.

Usage

From source file:com.eryansky.core.excelTools.ExcelUtils.java

/**
 * JavaBeanMap//from  w  ww  . java2 s.  c om
 * @param obj
 * @return
 */
public static Map<String, Object> beanToMap(Object obj) {
    Map<String, Object> params = new HashMap<String, Object>(0);
    try {
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
        for (int i = 0; i < descriptors.length; i++) {
            String name = descriptors[i].getName();
            if (!StringUtils.equals(name, "class")) {
                params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return params;
}

From source file:org.javaweb.utils.ClassUtils.java

/**
 * ?java?Map/*  www. j  ava2 s .  c o m*/
 *
 * @param obj
 * @return
 */
public static Map<String, Object> serializeClassToMap(Object obj) {
    Map<String, Object> params = new HashMap<String, Object>(0);

    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, propertyUtilsBean.getNestedProperty(obj, name));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return params;
}

From source file:org.javaweb.utils.FreemarkerUtils.java

/**
 * ?java?FreemarkerTemplateModelMap//from  ww  w  . jav a 2  s  .com
 *
 * @param clazz
 * @return
 */
public static Map<String, TemplateModel> classToTemplateModelMap(Object clazz,
        Map<String, TemplateModel> paramsMap) throws IllegalAccessException, NoSuchMethodException,
        InvocationTargetException, TemplateModelException {
    PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
    PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(clazz);

    for (int i = 0; i < descriptors.length; i++) {
        String name = descriptors[i].getName();

        if (!"class".equals(name)) {
            Object obj = propertyUtilsBean.getNestedProperty(clazz, name);
            TemplateModel model = DirectiveUtils.getDefaultObjectWrapper().wrap(obj);
            paramsMap.put(name, model);
        }
    }

    return paramsMap;
}

From source file:org.jdbcquery.Statement.java

/**
 * Sets all of the bean properties into the prepared statement using the bean property name as the parameter
 * name./*from w  ww .j a v a2  s . c o m*/
 *
 * @param aJavaBean
 *            the java bean to use
 * @return the statement (for method chaining)
 */
public Statement setBean(Object aJavaBean) {

    final PropertyUtilsBean propertyUtils = new PropertyUtilsBean();

    try {
        for (int i = 0; i < this.getParameters().size(); i++) {
            if (propertyUtils.isReadable(aJavaBean, this.getParameters().get(i))) {
                final Object value = propertyUtils.getNestedProperty(aJavaBean, this.getParameters().get(i));
                this.getPreparedStatement().setObject(i + 1, value);
            }
        }
    } catch (final SQLException e) {
        this.getConnection().cleanUp();
        throw new DaoException("Error", e);
    } catch (final IllegalAccessException e) {
        throw new DaoException("Error getting bean properties from " + aJavaBean, e);
    } catch (final InvocationTargetException e) {
        throw new DaoException("Error getting bean properties from " + aJavaBean, e);
    } catch (final NoSuchMethodException e) {
        throw new DaoException("Error getting bean properties from " + aJavaBean, e);
    }
    return this;
}

From source file:org.opencms.workplace.CmsWidgetDialogParameter.java

/**
 * Create a new Widget parameter based on a given object's property.<p>
 * //from   w w  w.  j  a v  a2s . c  o m
 * @param base the base object to map the parameter to / from
 * @param property the base object property to map the parameter to / from
 * @param htmlName the form id name to use in the generated HTML
 * @param defaultValue the default value to use for this parameter
 * @param dialogPage the dialog page to use the widget on
 * @param widget the widget used for this paramete
 * @param minOccurs the required minimum numer of occurences of this parameter
 * @param maxOccurs the maximum allowed numer of occurences of this parameter
 */
public CmsWidgetDialogParameter(Object base, String property, String htmlName, String defaultValue,
        String dialogPage, I_CmsWidget widget, int minOccurs, int maxOccurs) {

    if (htmlName == null) {
        htmlName = property;
    }

    if ((base instanceof List) || (base instanceof SortedMap)) {

        // this is a list, use custom list mappings
        init(null, defaultValue, htmlName, widget, dialogPage, 0, MAX_OCCURENCES, 0);

        m_baseObject = null;
        m_baseObjectProperty = null;
        m_baseCollection = base;

    } else {

        // generic object:use reflection to map object properties
        init(null, defaultValue, htmlName, widget, dialogPage, minOccurs, maxOccurs, 0);

        m_baseObject = base;
        m_baseObjectProperty = property;
        m_baseCollection = null;

        PropertyUtilsBean bean = new PropertyUtilsBean();

        Object value = null;
        // make sure the base object has the requested property
        if (!bean.isReadable(m_baseObject, m_baseObjectProperty)
                || !bean.isWriteable(m_baseObject, m_baseObjectProperty)) {
            try {
                // check if this is a mapped property
                value = bean.getMappedProperty(m_baseObject, m_baseObjectProperty);
            } catch (Exception e) {
                throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NO_PROPERTY_2,
                        base.getClass().getName(), property));
            }
        }

        try {
            if (value == null) {
                // may have been read already as a mapped property
                value = bean.getNestedProperty(m_baseObject, m_baseObjectProperty);
            }
        } catch (Exception e) {
            throw new CmsRuntimeException(
                    Messages.get().container(Messages.ERR_PROPERTY_READ_2, property, base.getClass().getName()),
                    e);
        }

        if (value != null) {
            if ((value instanceof List) || (value instanceof SortedMap)) {
                m_baseCollection = value;
                m_minOccurs = 0;
                m_maxOccurs = MAX_OCCURENCES;
            } else {
                m_defaultValue = String.valueOf(value);
                m_value = m_defaultValue;
                if ((m_minOccurs == 0) && !m_value.equals(defaultValue)) {
                    // if value is different from default ensure this widget is displayed
                    m_minOccurs = 1;
                }
            }
        }
    }
}