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

📄 lazydynabean.java

📁 apache beanutils开源项目源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *     * @exception IllegalArgumentException if this is not an existing property     *  name for our DynaClass and the MutableDynaClass is restricted     * @exception ConversionException if the specified value cannot be     *  converted to the type required for this property     * @exception NullPointerException if an attempt is made to set a     *  primitive property to null     */    public void set(String name, Object value) {        // If the property doesn't exist, then add it        if (!isDynaProperty(name)) {            if (dynaClass.isRestricted()) {                throw new IllegalArgumentException                    ("Invalid property name '" + name + "' (DynaClass is restricted)");            }            if (value == null) {                dynaClass.add(name);            } else {                dynaClass.add(name, value.getClass());            }        }        DynaProperty descriptor = dynaClass.getDynaProperty(name);        if (value == null) {            if (descriptor.getType().isPrimitive()) {                throw new NullPointerException                        ("Primitive value for '" + name + "'");            }        } else if (!isAssignable(descriptor.getType(), value.getClass())) {            throw new ConversionException                    ("Cannot assign value of type '" +                    value.getClass().getName() +                    "' to property '" + name + "' of type '" +                    descriptor.getType().getName() + "'");        }        // Set the property's value        values.put(name, value);    }    /**     * Set the value of an indexed property with the specified name.     *     * @param name Name of the property whose value is to be set     * @param index Index of the property to be set     * @param value Value to which this property is to be set     *     * @exception ConversionException if the specified value cannot be     *  converted to the type required for this property     * @exception IllegalArgumentException if there is no property     *  of the specified name     * @exception IllegalArgumentException if the specified property     *  exists, but is not indexed     * @exception IndexOutOfBoundsException if the specified index     *  is outside the range of the underlying property     */    public void set(String name, int index, Object value) {        // If its not a property, then create default indexed property        if (!isDynaProperty(name)) {            set(name, defaultIndexedProperty(name));        }        // Get the indexed property        Object indexedProperty = get(name);        // Check that the property is indexed        if (!dynaClass.getDynaProperty(name).isIndexed()) {            throw new IllegalArgumentException                ("Non-indexed property for '" + name + "[" + index + "]'"                            + dynaClass.getDynaProperty(name).getType().getName());        }        // Grow indexed property to appropriate size        indexedProperty = growIndexedProperty(name, indexedProperty, index);        // Set the value in an array        if (indexedProperty.getClass().isArray()) {            Array.set(indexedProperty, index, value);        } else if (indexedProperty instanceof List) {            ((List)indexedProperty).set(index, value);        } else {            throw new IllegalArgumentException                ("Non-indexed property for '" + name + "[" + index + "]' "                            + indexedProperty.getClass().getName());        }    }    /**     * Set the value of a mapped property with the specified name.     *     * @param name Name of the property whose value is to be set     * @param key Key of the property to be set     * @param value Value to which this property is to be set     *     * @exception ConversionException if the specified value cannot be     *  converted to the type required for this property     * @exception IllegalArgumentException if there is no property     *  of the specified name     * @exception IllegalArgumentException if the specified property     *  exists, but is not mapped     */    public void set(String name, String key, Object value) {        // If the 'mapped' property doesn't exist, then add it        if (!isDynaProperty(name)) {            set(name, defaultMappedProperty(name));        }        // Get the mapped property        Object mappedProperty = get(name);        // Check that the property is mapped        if (!dynaClass.getDynaProperty(name).isMapped()) {            throw new IllegalArgumentException                ("Non-mapped property for '" + name + "(" + key + ")'"                            + dynaClass.getDynaProperty(name).getType().getName());        }        // Set the value in the Map        ((Map)mappedProperty).put(key, value);    }    // ------------------- protected Methods ----------------------------------    protected Object growIndexedProperty(String name, Object indexedProperty, int index) {        // Grow a List to the appropriate size        if (indexedProperty instanceof List) {            List list = (List)indexedProperty;            while (index >= list.size()) {                list.add(null);            }        }        // Grow an Array to the appropriate size        if ((indexedProperty.getClass().isArray())) {            int length = Array.getLength(indexedProperty);            if (index >= length) {                Class componentType = indexedProperty.getClass().getComponentType();                Object newArray = Array.newInstance(componentType, (index + 1));                System.arraycopy(indexedProperty, 0, newArray, 0, length);                indexedProperty = newArray;                set(name, indexedProperty);                int newLength = Array.getLength(indexedProperty);                for (int i = length; i < newLength; i++) {                    Array.set(indexedProperty, i, createProperty(name+"["+i+"]", componentType));                }            }        }        return indexedProperty;    }    /**     * Create a new Instance of a Property     */    protected Object createProperty(String name, Class type) {        // Create Lists, arrays or DynaBeans        if (type.isArray() || List.class.isAssignableFrom(type)) {            return createIndexedProperty(name, type);        }        if (Map.class.isAssignableFrom(type)) {            return createMappedProperty(name, type);        }        if (DynaBean.class.isAssignableFrom(type)) {            return createDynaBeanProperty(name, type);        }        if (type.isPrimitive()) {            return createPrimitiveProperty(name, type);        }        if (Number.class.isAssignableFrom(type)) {            return createNumberProperty(name, type);        }        return createOtherProperty(name, type);    }    /**     * Create a new Instance of an 'Indexed' Property     */    protected Object createIndexedProperty(String name, Class type) {        // Create the indexed object        Object indexedProperty = null;        if (type == null) {            indexedProperty = defaultIndexedProperty(name);        } else if (type.isArray()) {            indexedProperty = Array.newInstance(type.getComponentType(), 0);        } else if (List.class.isAssignableFrom(type)) {            if (type.isInterface()) {                indexedProperty = defaultIndexedProperty(name);            } else {                try {                    indexedProperty = type.newInstance();                }                catch (Exception ex) {                    throw new IllegalArgumentException                        ("Error instantiating indexed property of type '" +                                   type.getName() + "' for '" + name + "' " + ex);                }            }        } else {            throw new IllegalArgumentException                    ("Non-indexed property of type '" + type.getName() + "' for '" + name + "'");        }        return indexedProperty;    }    /**     * Create a new Instance of a 'Mapped' Property     */    protected Object createMappedProperty(String name, Class type) {        // Create the mapped object        Object mappedProperty = null;        if (type == null) {            mappedProperty = defaultMappedProperty(name);        } else if (type.isInterface()) {            mappedProperty = defaultMappedProperty(name);        } else if (Map.class.isAssignableFrom(type)) {            try {                mappedProperty = type.newInstance();            }            catch (Exception ex) {                throw new IllegalArgumentException                    ("Error instantiating mapped property of type '" + type.getName() + "' for '" + name + "' " + ex);            }        } else {            throw new IllegalArgumentException                    ("Non-mapped property of type '" + type.getName() + "' for '" + name + "'");        }        return mappedProperty;    }    /**     * Create a new Instance of a 'Mapped' Property     */    protected Object createDynaBeanProperty(String name, Class type) {        try {            return type.newInstance();        }        catch (Exception ex) {            if (logger.isWarnEnabled()) {                logger.warn("Error instantiating DynaBean property of type '" + type.getName() + "' for '" + name + "' " + ex);            }            return null;        }    }    /**     * Create a new Instance of a 'Primitive' Property     */    protected Object createPrimitiveProperty(String name, Class type) {        if (type == Boolean.TYPE) {            return Boolean.FALSE;        } else if (type == Integer.TYPE) {            return Integer_ZERO;        } else if (type == Long.TYPE) {            return Long_ZERO;        } else if (type == Double.TYPE) {            return Double_ZERO;        } else if (type == Float.TYPE) {            return Float_ZERO;        } else if (type == Byte.TYPE) {            return Byte_ZERO;        } else if (type == Short.TYPE) {            return Short_ZERO;        } else if (type == Character.TYPE) {            return Character_SPACE;        } else {            return null;        }    }    /**     * Create a new Instance of a 'Primitive' Property     */    protected Object createNumberProperty(String name, Class type) {        return null;    }    /**     * Create a new Instance of a 'Mapped' Property     */    protected Object createOtherProperty(String name, Class type) {        if (type == String.class || type == Boolean.class ||            type == Character.class || Date.class.isAssignableFrom(type)) {            return null;        }        try {            return type.newInstance();        }        catch (Exception ex) {            if (logger.isWarnEnabled()) {                logger.warn("Error instantiating property of type '" + type.getName() + "' for '" + name + "' " + ex);            }            return null;        }    }    /**     * <p>Creates a new <code>ArrayList</code> for an 'indexed' property     *    which doesn't exist.</p>     *     * <p>This method shouls be overriden if an alternative <code>List</code>     *    or <code>Array</code> implementation is required for 'indexed' properties.</p>     *     * @param name Name of the 'indexed property.     */    protected Object defaultIndexedProperty(String name) {        return new ArrayList();    }    /**     * <p>Creates a new <code>HashMap</code> for a 'mapped' property     *    which doesn't exist.</p>     *     * <p>This method can be overriden if an alternative <code>Map</code>     *    implementation is required for 'mapped' properties.</p>     *     * @param name Name of the 'mapped property.     */    protected Map defaultMappedProperty(String name) {        return new HashMap();    }    /**     * Indicates if there is a property with the specified name.     */    protected boolean isDynaProperty(String name) {        if (name == null) {            throw new IllegalArgumentException("No property name specified");        }        // Handle LazyDynaClasses        if (dynaClass instanceof LazyDynaClass) {            return ((LazyDynaClass)dynaClass).isDynaProperty(name);        }        // Handle other MutableDynaClass        return dynaClass.getDynaProperty(name) == null ? false : true;    }    /**     * Is an object of the source class assignable to the destination class?     *     * @param dest Destination class     * @param source Source class     */    protected boolean isAssignable(Class dest, Class source) {        if (dest.isAssignableFrom(source) ||                ((dest == Boolean.TYPE) && (source == Boolean.class)) ||                ((dest == Byte.TYPE) && (source == Byte.class)) ||                ((dest == Character.TYPE) && (source == Character.class)) ||                ((dest == Double.TYPE) && (source == Double.class)) ||                ((dest == Float.TYPE) && (source == Float.class)) ||                ((dest == Integer.TYPE) && (source == Integer.class)) ||                ((dest == Long.TYPE) && (source == Long.class)) ||                ((dest == Short.TYPE) && (source == Short.class))) {            return (true);        } else {            return (false);        }    }    /**     * <p>Creates a new instance of the <code>Map</code>.</p>     */    protected Map newMap() {        return new HashMap();    }}

⌨️ 快捷键说明

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