public void setMappedProperty()

in src/main/java/org/apache/commons/beanutils2/PropertyUtilsBean.java [1753:1827]


    public void setMappedProperty(final Object bean, final String name,
                                         final String key, final Object value)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        if (bean == null) {
            throw new IllegalArgumentException("No bean specified");
        }
        if (name == null) {
            throw new IllegalArgumentException("No name specified for bean class '" +
                    bean.getClass() + "'");
        }
        if (key == null) {
            throw new IllegalArgumentException("No key specified for property '" +
                    name + "' on bean class '" + bean.getClass() + "'");
        }

        // 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, key, 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() + "'");
        }

        if (descriptor instanceof MappedPropertyDescriptor) {
            // Call the keyed setter method if there is one
            Method mappedWriteMethod =
                    ((MappedPropertyDescriptor) descriptor).
                    getMappedWriteMethod();
            mappedWriteMethod = MethodUtils.getAccessibleMethod(bean.getClass(), mappedWriteMethod);
            if (mappedWriteMethod == null) {
                throw new NoSuchMethodException
                    ("Property '" + name + "' has no mapped setter method" +
                     "on bean class '" + bean.getClass() + "'");
            }
            final Object[] params = new Object[2];
            params[0] = key;
            params[1] = value;
            if (LOG.isTraceEnabled()) {
                final String valueClassName =
                    value == null ? "<null>" : value.getClass().getName();
                LOG.trace("setSimpleProperty: Invoking method "
                          + mappedWriteMethod + " with key=" + key
                          + ", value=" + value
                          + " (class " + valueClassName +")");
            }
            invokeMethod(mappedWriteMethod, bean, params);
        } else {
          /* means that the result has to be retrieved from a map */
          final Method readMethod = getReadMethod(bean.getClass(), descriptor);
          if (readMethod == null) {
            throw new NoSuchMethodException("Property '" + name +
                    "' has no mapped getter method on bean class '" +
                    bean.getClass() + "'");
          }
        final Object invokeResult = invokeMethod(readMethod, bean, BeanUtils.EMPTY_OBJECT_ARRAY);
        /* test and fetch from the map */
        if (invokeResult instanceof java.util.Map) {
          final java.util.Map<String, Object> map = toPropertyMap(invokeResult);
          map.put(key, value);
        }
        }
    }