001package net.gdface.utils;
002
003import java.beans.PropertyDescriptor;
004import java.lang.reflect.InvocationTargetException;
005import java.lang.reflect.Method;
006import java.util.HashMap;
007import java.util.Map;
008
009import org.apache.commons.beanutils.BeanUtilsBean;
010import org.apache.commons.beanutils.PropertyUtilsBean;
011
012public class BeanPropertyUtils {
013
014        private static final boolean hasReadMethod(PropertyDescriptor propertyDescriptor) {
015                Method m = propertyDescriptor.getReadMethod();
016                // 过滤掉Object中的get方法
017                if (m != null && m.getDeclaringClass() != Object.class) {
018                        return true;
019                }
020                return false;
021        }
022
023        private static final boolean hasWriteMethod(PropertyDescriptor propertyDescriptor) {
024                Method m = propertyDescriptor.getWriteMethod();
025                // 过滤掉Object中的get方法
026                if (m != null && m.getDeclaringClass() != Object.class) {
027                        return true;
028                }
029                return false;
030        }
031
032        /**
033         * 获取beanClass中所有具有指定读写类型(rw)的属性
034         * @param beanClass
035         * @param rw 属性类型标记 <br>
036         *                                      <li>0 所有属性</li>
037         *                                      <li>1 读属性</li>
038         *                                      <li>2 写属性</li>
039         *                                      <li>3 读写属性</li>
040         * @param lenient 是否为宽容模式---允许返回类型不为void的setter方法
041         * @return 属性名与PropertyDescriptor映射的Map对象
042         */
043        public static final Map<String, PropertyDescriptor> getProperties(Class<?> beanClass, int rw,boolean lenient) {
044                try {
045                        Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
046                        if (beanClass != null) {
047                                BeanUtilsBean beanUtils = BeanUtilsBean.getInstance();
048                                PropertyUtilsBean propertyUtils = beanUtils.getPropertyUtils();
049                                PropertyDescriptor[] origDescriptors = propertyUtils.getPropertyDescriptors(beanClass);
050                                Boolean put;
051                                for (PropertyDescriptor pd : origDescriptors) {
052                                        if(lenient){
053                                                pd = LenientDecoratorOfDescriptor.toDecorator(pd);
054                                        }
055                                        put = false;
056                                        switch (rw &= 3) {
057                                        case 0:
058                                                put = true;
059                                                break;
060                                        case 1:
061                                                put = hasWriteMethod(pd);
062                                                break;
063                                        case 2:
064                                                put = hasReadMethod(pd);
065                                                break;
066                                        case 3:
067                                                put = hasReadMethod(pd) && hasWriteMethod(pd);
068                                                break;
069                                        }
070                                        if (put) {
071                                                properties.put(pd.getName(), pd);
072                                        }
073                                }
074                        }
075                        return properties;
076                } catch (Exception e) {
077                        throw new RuntimeException(e);
078                }
079        }
080        /**
081         * 获取beanClass中所有具有指定读写类型(rw)的属性
082         * @param beanClass
083         * @param rw 属性类型标记 <br>
084         *                                      <li>0 所有属性</li>
085         *                                      <li>1 读属性</li>
086         *                                      <li>2 写属性</li>
087         *                                      <li>3 读写属性</li>
088         * @return 属性名与PropertyDescriptor映射的Map对象
089         */
090        public static final Map<String, PropertyDescriptor> getProperties(Class<?> beanClass, int rw) {
091                return getProperties(beanClass, rw, false);
092        }
093        public static final <T>T copy(T from,T to){
094                if(null==from||null==to)
095                        throw new NullPointerException();
096                PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
097                try {
098                        propertyUtils.copyProperties(to, from);
099                } catch (IllegalAccessException e) {
100                        throw new RuntimeException(e);
101                }catch (InvocationTargetException e) {
102                        throw new RuntimeException(e);
103                }catch (NoSuchMethodException e) {
104                        throw new RuntimeException(e);
105                }
106                return to;
107        }
108}