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

📄 propertiesutil.java

📁 羽量级数据持久层开发框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.speedframework.utilities;

//~--- JDK imports ------------------------------------------------------------

import java.beans.*;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Hashtable;

/**
 *  类描述信息,描述类的主要职责和用处。
 *
 *
 * @version    $LastChangedRevision: 1945 $, 2007.09.29 at 02:14:12 CST
 * @author     <a href="mailto:falcon8848@gmail.com">piginzoo </a>
 */
public class PropertiesUtil
 {

    /** 属性描述信息 */
    private static Hashtable descriptorsCache = new Hashtable();

    /**
     * Copy property values from the "origin" bean to the "destination" bean
     * for all cases where the property names are the same (even though the
     * actual getter and setter methods might have been customized via
     * <code>BeanInfo</code> classes).  No conversions are performed on the
     * actual property values -- it is assumed that the values retrieved from
     * the origin bean are assignment-compatible with the types expected by
     * the destination bean.
     *
     * @param dest Destination bean whose properties are modified
     * @param orig Origin bean whose properties are retrieved
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static void copyProperties(Object dest, Object orig)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        PropertyDescriptor[] origDescriptors = getPropertyDescriptors(orig);

        for (int i = 0; i < origDescriptors.length; i++) {
            String name = origDescriptors[i].getName();

            if (name.toLowerCase().equals("class") || name.toLowerCase().equals("serialversionuid")) {
                continue;
            }

            if (getPropertyDescriptor(dest, name) != null) {
                Object value = getSimpleProperty(orig, name);

                setSimpleProperty(dest, name, value);
            }
        }
    }

    /**
     * Return the value of the specified indexed property of the specified
     * bean, with no type conversions.  The zero-relative index of the
     * required value must be included (in square brackets) as a suffix to
     * the property name, or <code>IllegalArgumentException</code> will be
     * thrown.
     *
     * @param bean Bean whose property is to be extracted
     * @param name <code>propertyname[index]</code> of the property value
     *             to be extracted
     *
     * @return
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static Object getIndexedProperty(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

        // Identify the index of the requested individual property
        int left  = name.indexOf("[");
        int right = name.indexOf("]");

        if ((left < 0) || (right <= left)) {
            throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
        }

        if (right > (name.length() - 1)) {
            throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
        }

        int index = -1;

        try {
            String subscript = name.substring(left + 1, right);

            index = Integer.parseInt(subscript);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid indexed property '" + name + "'");
        }

        name = name.substring(0, left);

        // Request the specified indexed property value
        return (getIndexedProperty(bean, name, index));
    }

    /**
     * Return the value of the specified indexed property of the specified
     * bean, with no type conversions.
     *
     * @param bean  Bean whose property is to be extracted
     * @param name  Simple property name of the property value to be extracted
     * @param index Index of the property value to be extracted
     *
     * @return
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static Object getIndexedProperty(Object bean, String name, int index)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

        // 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 getter method if there is one
        if (descriptor instanceof IndexedPropertyDescriptor) {
            Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();

            if (readMethod != null) {
                Object[] subscript = new Object[1];

                subscript[0] = new Integer(index);

                return (readMethod.invoke(bean, subscript));
            }
        }

        // Otherwise, the underlying property must be an array
        Method readMethod = descriptor.getReadMethod();

        if (readMethod == null) {
            throw new NoSuchMethodException("Property '" + name + "' has no getter method");
        }

        // Call the property getter and return the value
        Object value = readMethod.invoke(bean, new Object[0]);

        if (!value.getClass().isArray()) {
            throw new IllegalArgumentException("Property '" + name + "' is not indexed");
        }

        return (Array.get(value, index));
    }

    /**
     * Return 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 extracted
     * @param name Possibly nested name of the property to be extracted
     *
     * @return
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static Object getNestedProperty(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        while (true) {
            int period = name.indexOf(".");

            if (period < 0) {
                break;
            }

            String next = name.substring(0, period);

            if (next.indexOf("[") >= 0) {
                bean = getIndexedProperty(bean, next);
            } else {
                bean = getSimpleProperty(bean, next);
            }

            if (bean == null) {
                throw new IllegalArgumentException("Null property value for '" + name.substring(0, period) + "'");
            }

            name = name.substring(period + 1);
        }

        if (name.indexOf("[") >= 0) {
            return (getIndexedProperty(bean, name));
        } else {
            return (getSimpleProperty(bean, name));
        }
    }

    /**
     * Return 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 extracted
     * @param name Possibly indexed and/or nested name of the property
     *             to be extracted
     *
     * @return
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static Object getProperty(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        return (getNestedProperty(bean, name));
    }

    /**
     * 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 property itself is returned.
     *
     * @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
     *
     * @return
     * @throws IllegalAccessException    if the caller does not have
     *                                   access to the property accessor method
     * @throws InvocationTargetException if the property accessor method
     *                                   throws an exception
     * @throws NoSuchMethodException     if an accessor method for this
     *                                   propety cannot be found
     */
    public static PropertyDescriptor getPropertyDescriptor(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

        // Resolve nested references
        while (true) {
            int period = name.indexOf(".");

            if (period < 0) {
                break;
            }

            String next = name.substring(0, period);

            if (next.indexOf("[") >= 0) {
                bean = getIndexedProperty(bean, next);
            } else {
                bean = getSimpleProperty(bean, next);
            }

            if (bean == null) {
                throw new IllegalArgumentException("Null property value for '" + name.substring(0, period) + "'");
            }

            name = name.substring(period + 1);
        }

        // Remove any subscript from the final name value
        int left = name.indexOf("[");

        if (left >= 0) {
            name = name.substring(0, left);

            // Look up and return this property from our cache
        }

        if ((bean == null) || (name == null)) {
            return (null);
        }

        PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);

        if (descriptors == null) {
            return (null);
        }

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

        return (null);
    }

    /**
     * Retrieve the property descriptors for the specified bean, introspecting
     * and caching them the first time a particular bean class is encountered.
     *
     * @param bean Bean for which property descriptors are requested
     *
     * @return
     */
    public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
        if (bean == null) {
            return (new PropertyDescriptor[0]);
        }

        // Look up any cached descriptors for this bean class
        String               beanClassName = bean.getClass().getName();
        PropertyDescriptor[] descriptors   = (PropertyDescriptor[]) descriptorsCache.get(beanClassName);

        if (descriptors != null) {
            return (descriptors);
        }

        // Introspect the bean and cache the generated descriptors
        BeanInfo beanInfo = null;

⌨️ 快捷键说明

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