⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyutilsbean.java

📁 apache beanutils开源项目源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     *     * @exception ArrayIndexOutOfBoundsException if the specified index     *  is outside the valid range for the underlying array     * @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 InvocationTargetException if the property accessor method     *  throws an exception     * @exception NoSuchMethodException if an accessor method for this     *  propety cannot be found     */    public void setIndexedProperty(Object bean, String name,                                          int index, Object value)            throws IllegalAccessException, InvocationTargetException,            NoSuchMethodException {        if (bean == null) {            throw new IllegalArgumentException("No bean specified");        }        if (name == null) {            throw new IllegalArgumentException("No name specified");        }        // Handle DynaBean instances specially        if (bean instanceof DynaBean) {            DynaProperty descriptor =                    ((DynaBean) bean).getDynaClass().getDynaProperty(name);            if (descriptor == null) {                throw new NoSuchMethodException("Unknown property '" +                        name + "'");            }            ((DynaBean) bean).set(name, index, value);            return;        }        // Retrieve the property descriptor for the specified property        PropertyDescriptor descriptor =                getPropertyDescriptor(bean, name);        if (descriptor == null) {            throw new NoSuchMethodException("Unknown property '" +                    name + "'");        }        // Call the indexed setter method if there is one        if (descriptor instanceof IndexedPropertyDescriptor) {            Method writeMethod = ((IndexedPropertyDescriptor) descriptor).                    getIndexedWriteMethod();            if (writeMethod != null) {                Object subscript[] = new Object[2];                subscript[0] = new Integer(index);                subscript[1] = value;                try {                    if (log.isTraceEnabled()) {                        String valueClassName =                            value == null ? "<null>"                                           : value.getClass().getName();                        log.trace("setSimpleProperty: Invoking method "                                  + writeMethod +" with index=" + index                                  + ", value=" + value                                  + " (class " + valueClassName+ ")");                    }                    invokeMethod(writeMethod, bean, subscript);                } catch (InvocationTargetException e) {                    if (e.getTargetException() instanceof                            ArrayIndexOutOfBoundsException) {                        throw (ArrayIndexOutOfBoundsException)                                e.getTargetException();                    } else {                        throw e;                    }                }                return;            }        }        // Otherwise, the underlying property must be an array or a list        Method readMethod = descriptor.getReadMethod();        if (readMethod == null) {            throw new NoSuchMethodException("Property '" + name +                    "' has no getter method");        }        // Call the property getter to get the array or list        Object array = invokeMethod(readMethod, bean, new Object[0]);        if (!array.getClass().isArray()) {            if (array instanceof List) {                // Modify the specified value in the List                ((List) array).set(index, value);            } else {                throw new IllegalArgumentException("Property '" + name +                        "' is not indexed");            }        } else {            // Modify the specified value in the array            Array.set(array, index, value);        }    }    /**     * Set the value of the specified mapped property of the     * specified bean, with no type conversions.  The key of the     * value to set must be included (in brackets) as a suffix to     * the property name, or <code>IllegalArgumentException</code> will be     * thrown.     *     * @param bean Bean whose property is to be set     * @param name <code>propertyname(key)</code> of the property value     *  to be set     * @param value The property 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     * @exception NoSuchMethodException if an accessor method for this     *  propety cannot be found     */    public void setMappedProperty(Object bean, String name,                                         Object value)            throws IllegalAccessException, InvocationTargetException,            NoSuchMethodException {        if (bean == null) {            throw new IllegalArgumentException("No bean specified");        }        if (name == null) {            throw new IllegalArgumentException("No name specified");        }        // Identify the index of the requested individual property        int delim = name.indexOf(PropertyUtils.MAPPED_DELIM);        int delim2 = name.indexOf(PropertyUtils.MAPPED_DELIM2);        if ((delim < 0) || (delim2 <= delim)) {            throw new IllegalArgumentException                    ("Invalid mapped property '" + name + "'");        }        // Isolate the name and the key        String key = name.substring(delim + 1, delim2);        name = name.substring(0, delim);        // Request the specified indexed property value        setMappedProperty(bean, name, key, value);    }    /**     * Set the value of the specified mapped property of the specified     * bean, with no type conversions.     *     * @param bean Bean whose property is to be set     * @param name Mapped property name of the property value to be set     * @param key Key of the property value to be set     * @param value The property 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     * @exception NoSuchMethodException if an accessor method for this     *  propety cannot be found     */    public void setMappedProperty(Object bean, String name,                                         String key, Object value)            throws IllegalAccessException, InvocationTargetException,            NoSuchMethodException {        if (bean == null) {            throw new IllegalArgumentException("No bean specified");        }        if (name == null) {            throw new IllegalArgumentException("No name specified");        }        if (key == null) {            throw new IllegalArgumentException("No key specified");        }        // Handle DynaBean instances specially        if (bean instanceof DynaBean) {            DynaProperty descriptor =                    ((DynaBean) bean).getDynaClass().getDynaProperty(name);            if (descriptor == null) {                throw new NoSuchMethodException("Unknown property '" +                        name + "'");            }            ((DynaBean) bean).set(name, key, value);            return;        }        // Retrieve the property descriptor for the specified property        PropertyDescriptor descriptor =                getPropertyDescriptor(bean, name);        if (descriptor == null) {            throw new NoSuchMethodException("Unknown property '" +                    name + "'");        }        if (descriptor instanceof MappedPropertyDescriptor) {            // Call the keyed setter method if there is one            Method mappedWriteMethod =                    ((MappedPropertyDescriptor) descriptor).                    getMappedWriteMethod();            if (mappedWriteMethod != null) {                Object params[] = new Object[2];                params[0] = key;                params[1] = value;                if (log.isTraceEnabled()) {                    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 {                throw new NoSuchMethodException                        ("Property '" + name +                        "' has no mapped setter method");            }        } else {          /* means that the result has to be retrieved from a map */          Method readMethod = descriptor.getReadMethod();          if (readMethod != null) {            Object invokeResult = invokeMethod(readMethod, bean, new Object[0]);            /* test and fetch from the map */            if (invokeResult instanceof java.util.Map) {              ((java.util.Map)invokeResult).put(key, value);            }          } else {            throw new NoSuchMethodException("Property '" + name +                    "' has no mapped getter method");          }        }    }    /**     * 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     */    public void setNestedProperty(Object bean,                                         String name, Object value)            throws IllegalAccessException, InvocationTargetException,            NoSuchMethodException {        if (bean == null) {            throw new IllegalArgumentException("No bean specified");        }        if (name == null) {            throw new IllegalArgumentException("No name specified");        }        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) {                bean = ((Map) bean).get(next);            } else if (indexOfMAPPED_DELIM >= 0) {                bean = getMappedProperty(bean, next);            } else if (indexOfINDEXED_DELIM >= 0) {                bean = getIndexedProperty(bean, next);            } else {                bean = getSimpleProperty(bean, next);            }            if (bean == null) {                throw new IllegalArgumentException                        ("Null property value for '" +                        name.substring(0, delim) + "'");            }            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);        }    }    /**     * Set the value of the specified property of the specified bean,     * no matter which property reference format is used, with no     * type conversions.     *     * @param bean Bean whose property is to be modified     * @param n

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -