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

📄 beananalyzer.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.utilities;

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

import org.speedframework.exception.SpeedFrameworkException;

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

import java.beans.IntrospectionException;

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

import java.util.Hashtable;

/**
 * @author victorching
 */
public class BeanAnalyzer
 {

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

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

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param vo
     *
     * @return
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static String[] getGetterProperty(Class vo)
            throws IntrospectionException, InstantiationException, IllegalAccessException, InvocationTargetException,
                   NoSuchMethodException {
        String[] propertyName = (String[]) getterPropertyCache.get(vo);

        if (propertyName == null) {
            Method[] methods_ = getGetterMethods(vo);

            propertyName = new String[methods_.length];

            Object obj = vo.newInstance();

            for (int i = 0; i < methods_.length; i++) {
                propertyName[i] = getProperty(obj, methods_[i]);
            }

            getterPropertyCache.put(vo, propertyName);
        }

        return propertyName;
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param vo
     * @param columnName
     *
     * @return
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws SpeedFrameworkException
     */
    public static String getPropertyName(Class vo, String columnName)
            throws IntrospectionException, InstantiationException, IllegalAccessException, InvocationTargetException,
                   NoSuchMethodException, SpeedFrameworkException {
        return findProperty(getGetterProperty(vo), columnName);
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param vo
     *
     * @return
     *
     * @throws IntrospectionException
     */
    public static Method[] getGetterMethods(Class vo) throws IntrospectionException {
        Method[] getMethods = (Method[]) methodCache.get(vo);
        Method[] newMethods = null;

        if (getMethods == null) {
            if (vo.getSuperclass().getName().equals("java.lang.Object")) {
                newMethods = getGetterMethod(vo.getDeclaredMethods());
            } else {
                Method[] temp  = null;
                Class    child = vo;

                do {
                    temp  = (Method[]) copyAll(temp, child.getDeclaredMethods());
                    child = child.getSuperclass();
                } while (!child.getName().equals("java.lang.Object"));

                newMethods = getGetterMethod(temp);
            }

            methodCache.put(vo, newMethods);
            getMethods = newMethods;
        }

        return getMethods;
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param propertyNames
     * @param columnName
     *
     * @return
     */
    public static String findProperty(String[] propertyNames, String columnName) {
        String propertyName = null;

        for (int j = 0; j < propertyNames.length; j++) {
            if (propertyNames[j].toLowerCase().equals(columnName)) {
                propertyName = propertyNames[j];

                break;
            }
        }

        return propertyName;
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param array
     * @param method
     *
     * @return
     */
    public static Method[] addGetterMethod(Method[] array, Method method) {
        Class    type    = ((array != null)
                            ? array.getClass()
                            : ((method != null)
                               ? method.getClass()
                               : Object.class));
        Method[] method_ = (Method[]) copyArray(array, type);

        method_[method_.length - 1] = method;

        return method_;
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param methods
     *
     * @return
     */
    public static Method[] getGetterMethod(Method[] methods) {
        Method[] methods_ = new Method[] {};

        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().startsWith("get")) {
                methods_ = addGetterMethod(methods_, methods[i]);
            }
        }

        return methods_;
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param array
     * @param componentType
     *
     * @return
     */
    public static Object copyArray(Object array, Class componentType) {
        if (array != null) {
            int    arrayLength = Array.getLength(array);
            Object newArray    = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);

            System.arraycopy(array, 0, newArray, 0, arrayLength);

            return newArray;
        }

        return Array.newInstance(componentType, 1);
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param vo
     * @param method
     *
     * @return
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws SpeedFrameworkException
     */
    public static String getProperty(Object vo, Method method)
            throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException,
                   SpeedFrameworkException {
        String nameOne = method.getName().substring(3, method.getName().length());
        char[] chars_  = nameOne.toCharArray();
        int    temp    = (int) chars_[0];

        if ((temp <= 90) && (temp >= 65)) {
            chars_[0] = (char) (temp + 32);
        }

        String nameTwo = String.valueOf(chars_);

        if (PropertiesUtil.getPropertyDescriptor(vo, nameOne) != null) {
            return nameOne;
        } else if (PropertiesUtil.getPropertyDescriptor(vo, nameTwo) != null) {
            return nameTwo;
        } else {
            throw new SpeedFrameworkException("Property not found");
        }
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param array
     *
     * @return
     */
    public static Object[] clone(Object[] array) {
        if (array == null) {
            return null;
        }

        return (Object[]) array.clone();
    }

    /**
     * 方法描述信息,
     * 描述方法是做什么的,
     * 如何调用,最好给出调用代码示例。
     *
     * @param src
     * @param dest
     *
     * @return
     */
    public static Object[] copyAll(Object[] src, Object[] dest) {
        if (src == null) {
            return clone(dest);
        } else if (dest == null) {
            return clone(src);
        }

        Object[] newArray = (Object[]) Array.newInstance(src.getClass().getComponentType(), src.length + dest.length);

        System.arraycopy(src, 0, newArray, 0, src.length);
        System.arraycopy(dest, 0, newArray, src.length, dest.length);

        return newArray;
    }
}

⌨️ 快捷键说明

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