Example usage for org.apache.commons.beanutils PropertyUtils MAPPED_DELIM

List of usage examples for org.apache.commons.beanutils PropertyUtils MAPPED_DELIM

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils MAPPED_DELIM.

Prototype

char MAPPED_DELIM

To view the source code for org.apache.commons.beanutils PropertyUtils MAPPED_DELIM.

Click Source Link

Document

The delimiter that preceeds the key of a mapped property.

Usage

From source file:nl.strohalm.cyclos.utils.binding.CustomBeanUtilsBean.java

@Override
public void setProperty(final Object bean, String name, final Object value)
        throws IllegalAccessException, InvocationTargetException {

    // Resolve any nested expression to get the actual target bean
    Object target = bean;//from  www. j  a  v  a  2 s .  co m
    final int delim = findLastNestedIndex(name);
    if (delim >= 0) {
        try {
            target = getPropertyUtils().getProperty(bean, name.substring(0, delim));
        } catch (final NoSuchMethodException e) {
            return; // Skip this property setter
        }
        name = name.substring(delim + 1);
    }

    // Declare local variables we will require
    String propName = null; // Simple name of target property
    Class<?> type = null; // Java type of target property
    int index = -1; // Indexed subscript value (if any)
    String key = null; // Mapped key value (if any)

    // Calculate the property name, index, and key values
    propName = name;
    final int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
    if (i >= 0) {
        final int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
        try {
            index = Integer.parseInt(propName.substring(i + 1, k));
        } catch (final NumberFormatException e) {
            // Ignore
        }
        propName = propName.substring(0, i);
    }
    final int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
    if (j >= 0) {
        final int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
        try {
            key = propName.substring(j + 1, k);
        } catch (final IndexOutOfBoundsException e) {
            // Ignore
        }
        propName = propName.substring(0, j);
    }

    // Calculate the property type
    if (target instanceof DynaBean) {
        final DynaClass dynaClass = ((DynaBean) target).getDynaClass();
        final DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
        if (dynaProperty == null) {
            return; // Skip this property setter
        }
        type = dynaProperty.getType();
        if (type.isArray() || Collection.class.isAssignableFrom(type)) {
            type = Object[].class;
        }
    } else {
        PropertyDescriptor descriptor = null;
        try {
            descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
            if (descriptor == null) {
                return; // Skip this property setter
            }
        } catch (final NoSuchMethodException e) {
            return; // Skip this property setter
        }
        if (descriptor instanceof MappedPropertyDescriptor) {
            if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) {
                return; // Read-only, skip this property setter
            }
            type = ((MappedPropertyDescriptor) descriptor).getMappedPropertyType();

            /**
             * Overriden behaviour ------------------- When a type is Object on a mapped property, retrieve the value to check if it's an array
             */
            if (Object.class.equals(type)) {
                try {
                    final Object retrieved = getPropertyUtils().getMappedProperty(target, propName, key);
                    if (retrieved != null) {
                        final Class<?> retrievedType = retrieved.getClass();
                        if (retrievedType.isArray() || Collection.class.isAssignableFrom(retrievedType)) {
                            type = Object[].class;
                        }
                    }
                } catch (final NoSuchMethodException e) {
                    throw new PropertyException(target, propName + "(" + key + ")");
                }
            }
        } else if (descriptor instanceof IndexedPropertyDescriptor) {
            if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
                return; // Read-only, skip this property setter
            }
            type = ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType();
        } else {
            if (descriptor.getWriteMethod() == null) {
                return; // Read-only, skip this property setter
            }
            type = descriptor.getPropertyType();
        }
    }

    /**
     * Overriden behaviour ------------------- When a type is Map on a mapped property, retrieve the value to check if it's an array
     */
    if (Map.class.isAssignableFrom(type) && StringUtils.isNotEmpty(key)) {
        try {
            final Map<?, ?> map = (Map<?, ?>) getPropertyUtils().getProperty(target, propName);
            final Object retrieved = map.get(key);
            if (retrieved != null) {
                final Class<?> retrievedType = retrieved.getClass();
                if (retrievedType.isArray() || Collection.class.isAssignableFrom(retrievedType)) {
                    type = Object[].class;
                }
            }
        } catch (final NoSuchMethodException e) {
            throw new PropertyException(target, propName + "(" + key + ")");
        }
    }

    // Convert the specified value to the required type
    Object newValue = null;
    if (type.isArray() && (index < 0)) { // Scalar value into array
        if (value == null) {
            final String values[] = new String[1];
            values[0] = (String) value;
            newValue = getConvertUtils().convert(values, type);
        } else if (value instanceof String) {
            final String values[] = new String[1];
            values[0] = (String) value;
            newValue = getConvertUtils().convert(values, type);
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert((String[]) value, type);
        } else {
            newValue = value;
        }
    } else if (type.isArray()) { // Indexed value into array
        if (value instanceof String) {
            newValue = getConvertUtils().convert((String) value, type.getComponentType());
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert(((String[]) value)[0], type.getComponentType());
        } else {
            newValue = value;
        }
    } else { // Value into scalar
        if ((value instanceof String) || (value == null)) {
            newValue = getConvertUtils().convert((String) value, type);
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert(((String[]) value)[0], type);
        } else if (getConvertUtils().lookup(value.getClass()) != null) {
            newValue = getConvertUtils().convert(value.toString(), type);
        } else {
            newValue = value;
        }
    }

    // Invoke the setter method
    try {
        if (index >= 0) {
            getPropertyUtils().setIndexedProperty(target, propName, index, newValue);
        } else if (key != null) {
            getPropertyUtils().setMappedProperty(target, propName, key, newValue);
        } else {
            getPropertyUtils().setProperty(target, propName, newValue);
        }
    } catch (final NoSuchMethodException e) {
        throw new InvocationTargetException(e, "Cannot set " + propName);
    }

}

From source file:nl.strohalm.cyclos.utils.binding.CustomBeanUtilsBean.java

private int findLastNestedIndex(final String expression) {
    // walk back from the end to the start
    // and find the first index that
    int bracketCount = 0;
    for (int i = expression.length() - 1; i >= 0; i--) {
        final char at = expression.charAt(i);
        switch (at) {
        case PropertyUtils.NESTED_DELIM:
            if (bracketCount < 1) {
                return i;
            }/*from   ww w.j a  v  a2  s .  c om*/
            break;

        case PropertyUtils.MAPPED_DELIM:
        case PropertyUtils.INDEXED_DELIM:
            // not bothered which
            --bracketCount;
            break;

        case PropertyUtils.MAPPED_DELIM2:
        case PropertyUtils.INDEXED_DELIM2:
            // not bothered which
            ++bracketCount;
            break;
        }
    }
    // can't find any
    return -1;
}

From source file:org.displaytag.util.LookupUtil.java

/**
 * <p>//from   w  ww  .j  a va 2s  .  c  o  m
 * Returns the value of a property in the given bean.
 * </p>
 * <p>
 * This method is a modificated version from commons-beanutils PropertyUtils.getProperty(). It allows intermediate
 * nulls in expression without throwing exception (es. it doesn't throw an exception for the property
 * <code>object.date.time</code> if <code>date</code> is null)
 * </p>
 * @param bean javabean
 * @param name name of the property to read from the javabean
 * @return Object
 * @throws ObjectLookupException for errors while retrieving a property in the bean
 */
public static Object getBeanProperty(Object bean, String name) throws ObjectLookupException {

    if (log.isDebugEnabled()) {
        log.debug("getProperty [" + name + "] on bean " + bean);
    }

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    Object evalBean = bean;
    String evalName = name;

    try {

        int indexOfINDEXEDDELIM;
        int indexOfMAPPEDDELIM;
        int indexOfMAPPEDDELIM2;
        int indexOfNESTEDDELIM;
        while (true) {

            indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM);
            indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM);
            indexOfMAPPEDDELIM2 = evalName.indexOf(PropertyUtils.MAPPED_DELIM2);
            if (indexOfMAPPEDDELIM2 >= 0 && indexOfMAPPEDDELIM >= 0
                    && (indexOfNESTEDDELIM < 0 || indexOfNESTEDDELIM > indexOfMAPPEDDELIM)) {
                indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM, indexOfMAPPEDDELIM2);
            } else {
                indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM);
            }
            if (indexOfNESTEDDELIM < 0) {
                break;
            }
            String next = evalName.substring(0, indexOfNESTEDDELIM);
            indexOfINDEXEDDELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
            indexOfMAPPEDDELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
            if (evalBean instanceof Map) {
                evalBean = ((Map) evalBean).get(next);
            } else if (indexOfMAPPEDDELIM >= 0) {

                evalBean = PropertyUtils.getMappedProperty(evalBean, next);

            } else if (indexOfINDEXEDDELIM >= 0) {
                evalBean = PropertyUtils.getIndexedProperty(evalBean, next);
            } else {
                evalBean = PropertyUtils.getSimpleProperty(evalBean, next);
            }

            if (evalBean == null) {
                log.debug("Null property value for '" + evalName.substring(0, indexOfNESTEDDELIM) + "'");
                return null;
            }
            evalName = evalName.substring(indexOfNESTEDDELIM + 1);

        }

        indexOfINDEXEDDELIM = evalName.indexOf(PropertyUtils.INDEXED_DELIM);
        indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM);

        if (evalBean instanceof Map) {
            evalBean = ((Map) evalBean).get(evalName);
        } else if (indexOfMAPPEDDELIM >= 0) {
            evalBean = PropertyUtils.getMappedProperty(evalBean, evalName);
        } else if (indexOfINDEXEDDELIM >= 0) {
            evalBean = PropertyUtils.getIndexedProperty(evalBean, evalName);
        } else {
            evalBean = PropertyUtils.getSimpleProperty(evalBean, evalName);
        }
    } catch (IllegalAccessException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    }

    catch (InvocationTargetException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    } catch (NoSuchMethodException e) {
        throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e);
    }

    return evalBean;

}

From source file:org.gameye.psp.image.utils.TBeanUtilsBean.java

public void copyProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {

    // Trace logging (if enabled)
    if (log.isTraceEnabled()) {
        StringBuffer sb = new StringBuffer("  copyProperty(");
        sb.append(bean);/*from w ww. j  ava2 s  . c  o  m*/
        sb.append(", ");
        sb.append(name);
        sb.append(", ");
        if (value == null) {
            sb.append("<NULL>");
        } else if (value instanceof String) {
            sb.append((String) value);
        } else if (value instanceof String[]) {
            String values[] = (String[]) value;
            sb.append('[');
            for (int i = 0; i < values.length; i++) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(values[i]);
            }
            sb.append(']');
        } else {
            sb.append(value.toString());
        }
        sb.append(')');
        log.trace(sb.toString());
    }

    // Resolve any nested expression to get the actual target bean
    Object target = bean;
    int delim = name.lastIndexOf(PropertyUtils.NESTED_DELIM);
    if (delim >= 0) {
        try {
            target = getPropertyUtils().getProperty(bean, name.substring(0, delim));
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        name = name.substring(delim + 1);
        if (log.isTraceEnabled()) {
            log.trace("    Target bean = " + target);
            log.trace("    Target name = " + name);
        }
    }

    // Declare local variables we will require
    String propName = null; // Simple name of target property
    Class type = null; // Java type of target property
    int index = -1; // Indexed subscript value (if any)
    String key = null; // Mapped key value (if any)

    // Calculate the target property name, index, and key values
    propName = name;
    int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
    if (i >= 0) {
        int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
        try {
            index = Integer.parseInt(propName.substring(i + 1, k));
        } catch (NumberFormatException e) {
            ;
        }
        propName = propName.substring(0, i);
    }
    int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
    if (j >= 0) {
        int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
        try {
            key = propName.substring(j + 1, k);
        } catch (IndexOutOfBoundsException e) {
            ;
        }
        propName = propName.substring(0, j);
    }

    // Calculate the target property type
    if (target instanceof DynaBean) {
        DynaClass dynaClass = ((DynaBean) target).getDynaClass();
        DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
        if (dynaProperty == null) {
            return; // Skip this property setter
        }
        type = dynaProperty.getType();
    } else {
        PropertyDescriptor descriptor = null;
        try {
            descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
            if (descriptor == null) {
                return; // Skip this property setter
            }
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        type = descriptor.getPropertyType();
        if (type == null) {
            // Most likely an indexed setter on a POJB only
            if (log.isTraceEnabled()) {
                log.trace("    target type for property '" + propName + "' is null, so skipping ths setter");
            }
            return;
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("    target propName=" + propName + ", type=" + type + ", index=" + index + ", key=" + key);
    }

    // Convert the specified value to the required type and store it
    if (index >= 0) { // Destination must be indexed
        Converter converter = getConvertUtils().lookup(type.getComponentType());
        if (converter != null) {
            log.trace("        USING CONVERTER " + converter);
            value = converter.convert(type, value);
        }
        try {
            getPropertyUtils().setIndexedProperty(target, propName, index, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    } else if (key != null) { // Destination must be mapped
        // Maps do not know what the preferred data type is,
        // so perform no conversions at all
        // FIXME - should we create or support a TypedMap?
        try {
            getPropertyUtils().setMappedProperty(target, propName, key, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    } else { // Destination must be simple
        Converter converter = getConvertUtils().lookup(type);
        if (converter != null) {
            log.trace("        USING CONVERTER " + converter);
            value = converter.convert(type, value);
        }
        try {
            getPropertyUtils().setSimpleProperty(target, propName, value);
        } catch (NoSuchMethodException e) {
            throw new InvocationTargetException(e, "Cannot set " + propName);
        }
    }

}

From source file:org.gameye.psp.image.utils.TBeanUtilsBean.java

/**
 * <p>Set the specified property value, performing type conversions as
 * required to conform to the type of the destination property.</p>
 *
 * <p>If the property is read only then the method returns 
 * without throwing an exception.</p>
 *
 * <p>If <code>null</code> is passed into a property expecting a primitive value,
 * then this will be converted as if it were a <code>null</code> string.</p>
 *
 * <p><strong>WARNING</strong> - The logic of this method is customized
 * to meet the needs of <code>populate()</code>, and is probably not what
 * you want for general property copying with type conversion.  For that
 * purpose, check out the <code>copyProperty()</code> method instead.</p>
 *
 * <p><strong>WARNING</strong> - PLEASE do not modify the behavior of this
 * method without consulting with the Struts developer community.  There
 * are some subtleties to its functionality that are not documented in the
 * Javadoc description above, yet are vital to the way that Struts utilizes
 * this method.</p>//from w  w w.j  a  va 2  s .c  om
 *
 * @param bean Bean on which setting is to be performed
 * @param name Property name (can be nested/indexed/mapped/combo)
 * @param value Value to be set
 *
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception
 */
public void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {

    // Trace logging (if enabled)
    if (log.isTraceEnabled()) {
        StringBuffer sb = new StringBuffer("  setProperty(");
        sb.append(bean);
        sb.append(", ");
        sb.append(name);
        sb.append(", ");
        if (value == null) {
            sb.append("<NULL>");
        } else if (value instanceof String) {
            sb.append((String) value);
        } else if (value instanceof String[]) {
            String values[] = (String[]) value;
            sb.append('[');
            for (int i = 0; i < values.length; i++) {
                if (i > 0) {
                    sb.append(',');
                }
                sb.append(values[i]);
            }
            sb.append(']');
        } else {
            sb.append(value.toString());
        }
        sb.append(')');
        log.trace(sb.toString());
    }

    // Resolve any nested expression to get the actual target bean
    Object target = bean;
    int delim = findLastNestedIndex(name);
    if (delim >= 0) {
        try {
            target = getPropertyUtils().getProperty(bean, name.substring(0, delim));
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        name = name.substring(delim + 1);
        if (log.isTraceEnabled()) {
            log.trace("    Target bean = " + target);
            log.trace("    Target name = " + name);
        }
    }

    // Declare local variables we will require
    String propName = null; // Simple name of target property
    Class type = null; // Java type of target property
    int index = -1; // Indexed subscript value (if any)
    String key = null; // Mapped key value (if any)

    // Calculate the property name, index, and key values
    propName = name;
    int i = propName.indexOf(PropertyUtils.INDEXED_DELIM);
    if (i >= 0) {
        int k = propName.indexOf(PropertyUtils.INDEXED_DELIM2);
        try {
            index = Integer.parseInt(propName.substring(i + 1, k));
        } catch (NumberFormatException e) {
            ;
        }
        propName = propName.substring(0, i);
    }
    int j = propName.indexOf(PropertyUtils.MAPPED_DELIM);
    if (j >= 0) {
        int k = propName.indexOf(PropertyUtils.MAPPED_DELIM2);
        try {
            key = propName.substring(j + 1, k);
        } catch (IndexOutOfBoundsException e) {
            ;
        }
        propName = propName.substring(0, j);
    }

    // Calculate the property type
    if (target instanceof DynaBean) {
        DynaClass dynaClass = ((DynaBean) target).getDynaClass();
        DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
        if (dynaProperty == null) {
            return; // Skip this property setter
        }
        type = dynaProperty.getType();
    } else {
        PropertyDescriptor descriptor = null;
        try {
            descriptor = getPropertyUtils().getPropertyDescriptor(target, name);
            if (descriptor == null) {
                return; // Skip this property setter
            }
        } catch (NoSuchMethodException e) {
            return; // Skip this property setter
        }
        if (descriptor instanceof MappedPropertyDescriptor) {
            if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping read-only property");
                }
                return; // Read-only, skip this property setter
            }
            type = ((MappedPropertyDescriptor) descriptor).getMappedPropertyType();
        } else if (descriptor instanceof IndexedPropertyDescriptor) {
            if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping read-only property");
                }
                return; // Read-only, skip this property setter
            }
            type = ((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType();
        } else {
            if (descriptor.getWriteMethod() == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping read-only property");
                }
                return; // Read-only, skip this property setter
            }
            type = descriptor.getPropertyType();
        }
    }

    // Convert the specified value to the required type
    Object newValue = null;
    if (type.isArray() && (index < 0)) { // Scalar value into array
        if (value == null) {
            String values[] = new String[1];
            values[0] = (String) value;
            newValue = getConvertUtils().convert((String[]) values, type);
        } else if (value instanceof String) {
            String values[] = new String[1];
            values[0] = (String) value;
            newValue = getConvertUtils().convert((String[]) values, type);
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert((String[]) value, type);
        } else {
            newValue = value;
        }
    } else if (type.isArray()) { // Indexed value into array
        if (value instanceof String) {
            newValue = getConvertUtils().convert((String) value, type.getComponentType());
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert(((String[]) value)[0], type.getComponentType());
        } else {
            newValue = value;
        }
    } else { // Value into scalar
        if ((value instanceof String) || (value == null)) {
            newValue = getConvertUtils().convert((String) value, type);
        } else if (value instanceof String[]) {
            newValue = getConvertUtils().convert(((String[]) value)[0], type);
        } else if (getConvertUtils().lookup(value.getClass()) != null) {
            newValue = getConvertUtils().convert(value.toString(), type);
        } else {
            newValue = value;
        }
    }

    // Invoke the setter method
    try {
        if (index >= 0) {
            getPropertyUtils().setIndexedProperty(target, propName, index, newValue);
        } else if (key != null) {
            getPropertyUtils().setMappedProperty(target, propName, key, newValue);
        } else {
            getPropertyUtils().setProperty(target, propName, newValue);
        }
    } catch (NoSuchMethodException e) {
        throw new InvocationTargetException(e, "Cannot set " + propName);
    }

}

From source file:org.gameye.psp.image.utils.TBeanUtilsBean.java

private int findLastNestedIndex(String expression) {
    // walk back from the end to the start 
    // and find the first index that 
    int bracketCount = 0;
    for (int i = expression.length() - 1; i >= 0; i--) {
        char at = expression.charAt(i);
        switch (at) {
        case PropertyUtils.NESTED_DELIM:
            if (bracketCount < 1) {
                return i;
            }/*w  ww .  j a v a2s  . c om*/
            break;

        case PropertyUtils.MAPPED_DELIM:
        case PropertyUtils.INDEXED_DELIM:
            // not bothered which
            --bracketCount;
            break;

        case PropertyUtils.MAPPED_DELIM2:
        case PropertyUtils.INDEXED_DELIM2:
            // not bothered which
            ++bracketCount;
            break;
        }
    }
    // can't find any
    return -1;
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * begin Kuali Foundation modification//from  w  w  w .  jav  a 2 s  . com
 * Set the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.
 *
 * @param bean Bean whose property is to be modified
 * @param name Possibly nested name of the property to be modified
 * @param value Value to which the property is to be set
 *
 * @exception IllegalAccessException if the caller does not have access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a property returns null
 * @exception InvocationTargetException if the property accessor method throws an exception
 * @exception NoSuchMethodException if an accessor method for this propety cannot be found
 * end Kuali Foundation modification
 */
public void setNestedProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name + ", value = " + value);
        return;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    Object propBean = null;
    int indexOfINDEXED_DELIM = -1;
    int indexOfMAPPED_DELIM = -1;
    while (true) {
        int delim = name.indexOf(PropertyUtils.NESTED_DELIM);
        if (delim < 0) {
            break;
        }
        String next = name.substring(0, delim);
        indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
        indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
        if (bean instanceof Map) {
            propBean = ((Map) bean).get(next);
        } else if (indexOfMAPPED_DELIM >= 0) {
            propBean = getMappedProperty(bean, next);
        } else if (indexOfINDEXED_DELIM >= 0) {
            propBean = getIndexedProperty(bean, next);
        } else {
            propBean = getSimpleProperty(bean, next);
        }
        if (ObjectUtils.isNull(propBean)) {
            Class propertyType = getPropertyType(bean, next);
            if (propertyType != null) {
                Object newInstance = ObjectUtils.createNewObjectFromClass(propertyType);
                setSimpleProperty(bean, next, newInstance);
                propBean = getSimpleProperty(bean, next);
            }
        }
        bean = propBean;
        name = name.substring(delim + 1);
    }

    indexOfINDEXED_DELIM = name.indexOf(PropertyUtils.INDEXED_DELIM);
    indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM);

    if (bean instanceof Map) {
        // check to see if the class has a standard property
        PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
        if (descriptor == null) {
            // no - then put the value into the map
            ((Map) bean).put(name, value);
        } else {
            // yes - use that instead
            setSimpleProperty(bean, name, value);
        }
    } else if (indexOfMAPPED_DELIM >= 0) {
        setMappedProperty(bean, name, value);
    } else if (indexOfINDEXED_DELIM >= 0) {
        setIndexedProperty(bean, name, value);
    } else {
        setSimpleProperty(bean, name, value);
    }
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * <p>/*from w w w.j  av a 2  s  .c  o  m*/
 * Retrieve the property descriptor for the specified property of the specified bean, or return <code>null</code> if there is
 * no such descriptor. This method resolves indexed and nested property references in the same manner as other methods in this
 * class, except that if the last (or only) name element is indexed, the descriptor for the last resolved property itself is
 * returned.
 * </p>
 *
 * <p>
 * <strong>FIXME </strong>- Does not work with DynaBeans.
 * </p>
 *
 * @param bean Bean for which a property descriptor is requested
 * @param name Possibly indexed and/or nested name of the property for which a property descriptor is requested
 *
 * @exception IllegalAccessException if the caller does not have access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or <code>name</code> is null
 * @exception IllegalArgumentException if a nested reference to a property returns null
 * @exception InvocationTargetException if the property accessor method throws an exception
 * @exception NoSuchMethodException if an accessor method for this propety cannot be found
 */
public PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name);
        return null;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }
    try {
        // Resolve nested references
        Object propBean = null;
        while (true) {
            int delim = findNextNestedIndex(name);
            //int delim = name.indexOf(PropertyUtils.NESTED_DELIM);
            if (delim < 0) {
                break;
            }
            String next = name.substring(0, delim);
            int indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);
            int indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);
            if (indexOfMAPPED_DELIM >= 0
                    && (indexOfINDEXED_DELIM < 0 || indexOfMAPPED_DELIM < indexOfINDEXED_DELIM)) {
                propBean = getMappedProperty(bean, next);
            } else {
                if (indexOfINDEXED_DELIM >= 0) {
                    propBean = getIndexedProperty(bean, next);
                } else {
                    propBean = getSimpleProperty(bean, next);
                }
            }
            if (ObjectUtils.isNull(propBean)) {
                Class propertyType = getPropertyType(bean, next);
                if (propertyType != null) {
                    Object newInstance = ObjectUtils.createNewObjectFromClass(propertyType);
                    setSimpleProperty(bean, next, newInstance);
                    propBean = getSimpleProperty(bean, next);
                }
            }
            bean = propBean;
            name = name.substring(delim + 1);
        }

        // Remove any subscript from the final name value
        int left = name.indexOf(PropertyUtils.INDEXED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }
        left = name.indexOf(PropertyUtils.MAPPED_DELIM);
        if (left >= 0) {
            name = name.substring(0, left);
        }

        // Look up and return this property from our cache
        // creating and adding it to the cache if not found.
        if ((bean == null) || (name == null)) {
            return (null);
        }

        PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
        if (descriptors != null) {

            for (int i = 0; i < descriptors.length; i++) {
                if (name.equals(descriptors[i].getName()))
                    return (descriptors[i]);
            }
        }

        PropertyDescriptor result = null;
        FastHashMap mappedDescriptors = getMappedPropertyDescriptors(bean);
        if (mappedDescriptors == null) {
            mappedDescriptors = new FastHashMap();
            mappedDescriptors.setFast(true);
        }
        result = (PropertyDescriptor) mappedDescriptors.get(name);
        if (result == null) {
            // not found, try to create it
            try {
                result = new MappedPropertyDescriptor(name, bean.getClass());
            } catch (IntrospectionException ie) {
            }
            if (result != null) {
                mappedDescriptors.put(name, result);
            }
        }

        return result;
    } catch (RuntimeException ex) {
        LOG.error("Unable to get property descriptor for " + bean.getClass().getName() + " . " + name + "\n"
                + ex.getClass().getName() + ": " + ex.getMessage());
        throw ex;
    }
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

private int findNextNestedIndex(String expression) {
    // walk back from the end to the start
    // and find the first index that
    int bracketCount = 0;
    for (int i = 0, size = expression.length(); i < size; i++) {
        char at = expression.charAt(i);
        switch (at) {
        case PropertyUtils.NESTED_DELIM:
            if (bracketCount < 1) {
                return i;
            }//from w  w  w.ja  va 2s .c o  m
            break;

        case PropertyUtils.MAPPED_DELIM:
        case PropertyUtils.INDEXED_DELIM:
            // not bothered which
            ++bracketCount;
            break;

        case PropertyUtils.MAPPED_DELIM2:
        case PropertyUtils.INDEXED_DELIM2:
            // not bothered which
            --bracketCount;
            break;
        }
    }
    // can't find any
    return -1;
}

From source file:org.kuali.rice.kns.web.struts.form.pojo.PojoPropertyUtilsBean.java

/**
 * Set the value of the specified simple property of the specified bean,
 * with no type conversions./*from  w w w .j  ava  2s. com*/
 *
 * @param bean Bean whose property is to be modified
 * @param name Name of the property to be modified
 * @param value Value to which the property should be set
 *
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or
 *  <code>name</code> is null
 * @exception IllegalArgumentException if the property name is
 *  nested or indexed
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception
 * @exception NoSuchMethodException if an accessor method for this
 *  propety cannot be found
 */
public void setSimpleProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("No bean specified, name = " + name + ", value = " + value);
        return;
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified");
    }

    // Validate the syntax of the property name
    if (name.indexOf(PropertyUtils.NESTED_DELIM) >= 0) {
        throw new IllegalArgumentException("Nested property names are not allowed");
    } else if (name.indexOf(PropertyUtils.INDEXED_DELIM) >= 0) {
        throw new IllegalArgumentException("Indexed property names are not allowed");
    } else if (name.indexOf(PropertyUtils.MAPPED_DELIM) >= 0) {
        throw new IllegalArgumentException("Mapped property names are not allowed");
    }

    // Retrieve the property setter method for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null) {
        throw new NoSuchMethodException("Unknown property '" + name + "'");
    }
    Method writeMethod = getWriteMethod(descriptor);
    if (writeMethod == null) {
        //throw new NoSuchMethodException("Property '" + name + "' has no setter method");
        LOG.warn("Bean: " + bean.getClass().getName() + ", Property '" + name + "' has no setter method");
        return;
    }

    // Call the property setter method
    Object values[] = new Object[1];
    values[0] = value;
    if (LOG.isDebugEnabled()) {
        String valueClassName = value == null ? "<null>" : value.getClass().getName();
        LOG.debug("setSimpleProperty: Invoking method " + writeMethod + " with value " + value + " (class "
                + valueClassName + ")");
    }

    invokeMethod(writeMethod, bean, values);

}