public void setIndexedProperty()

in src/main/java/org/apache/commons/beanutils2/PropertyUtilsBean.java [1220:1292]


    public void setIndexedProperty(final Object bean, final String name, final int index, final Object value)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Objects.requireNonNull(bean, "bean");
        if (name == null || name.isEmpty()) {
            if (bean.getClass().isArray()) {
                Array.set(bean, index, value);
                return;
            }
            if (bean instanceof List) {
                final List<Object> list = toObjectList(bean);
                list.set(index, value);
                return;
            }
        }
        Objects.requireNonNull(name, "name");
        // Handle DynaBean instances specially
        if (bean instanceof DynaBean) {
            final DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
            if (descriptor == null) {
                throw new NoSuchMethodException("Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
            }
            ((DynaBean) bean).set(name, index, value);
            return;
        }

        // Retrieve the property descriptor for the specified property
        final PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
        if (descriptor == null) {
            throw new NoSuchMethodException("Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
        }

        // Call the indexed setter method if there is one
        if (descriptor instanceof IndexedPropertyDescriptor) {
            Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod();
            writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
            if (writeMethod != null) {
                try {
                    if (LOG.isTraceEnabled()) {
                        final String valueClassName = value == null ? "<null>" : value.getClass().getName();
                        LOG.trace("setSimpleProperty: Invoking method " + writeMethod + " with index=" + index + ", value=" + value + " (class "
                                + valueClassName + ")");
                    }
                    invokeMethod(writeMethod, bean, Integer.valueOf(index), value);
                } catch (final InvocationTargetException e) {
                    if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                        throw (IndexOutOfBoundsException) e.getTargetException();
                    }
                    throw e;
                }
                return;
            }
        }

        // Otherwise, the underlying property must be an array or a list
        final Method readMethod = getReadMethod(bean.getClass(), descriptor);
        if (readMethod == null) {
            throw new NoSuchMethodException("Property '" + name + "' has no getter method on bean class '" + bean.getClass() + "'");
        }

        // Call the property getter to get the array or list
        final Object array = invokeMethod(readMethod, bean, BeanUtils.EMPTY_OBJECT_ARRAY);
        if (!array.getClass().isArray()) {
            if (!(array instanceof List)) {
                throw new IllegalArgumentException("Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
            }
            // Modify the specified value in the List
            final List<Object> list = toObjectList(array);
            list.set(index, value);
        } else {
            // Modify the specified value in the array
            Array.set(array, index, value);
        }
    }