11
zhouwei
2019-07-12 143f7be25ff19896e70ffc486999a64a3bc3b76f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.safeluck.aaej.app.config;
 
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.Assert;
 
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
 
public final class BeanUtil extends PropertyUtilsBean implements BeanFactoryPostProcessor {
    private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境
 
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanUtil.beanFactory = beanFactory;
    }
 
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }
 
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) beanFactory.getBean(clz);
        return result;
    }
 
    /**
     * getParameterizedType方法慨述:获取父类的泛型类型 <br>
     * 创 建 人:伍乾伦 <br>
     * 创建时间:2015年6月24日 下午2:18:58 <br>
     * 修 改 人:(修改了该文件,请填上修改人的名字) <br>
     * 修改日期:(请填上修改该文件时的日期) <br>
     * 
     * @param clazz <br>
     * @param index <br>
     * @return Class<T>
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> getParameterizedType(Class<?> clazz, int index) {
        Type parameterizedType = clazz.getGenericSuperclass();
        if (!(parameterizedType instanceof ParameterizedType)) {
            parameterizedType = clazz.getSuperclass().getGenericSuperclass();
        }
        if (!(parameterizedType instanceof ParameterizedType)) {
            return null;
        }
        Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
        if (actualTypeArguments == null || actualTypeArguments.length == 0) {
            return null;
        }
        
        if (actualTypeArguments[0] instanceof ParameterizedType) {
            parameterizedType = actualTypeArguments[0];
            return (Class<T>) ((ParameterizedType) parameterizedType).getRawType();
        }
        return (Class<T>) actualTypeArguments[0];
    }
 
    /**
     * 概 述: (浅)拷贝对象属性到目标对象,忽略部分属性的拷贝 <br/>
     * 创 建 人:wuqianlun <br/>
     * 创建时间:2016年4月5日下午7:25:53<br/>
     * 修 改 人:wuqianlun<br/>
     * 修改日期:2016年4月5日下午7:25:53<br/>
     *
     * @param from
     * @param to
     * @param ignores
     */
    public static void copyIgnoreProperties(Object from, Object to, String... ignores) {
        Assert.notNull(from, "Source must not be null");
        Assert.notNull(to, "Target must not be null");
 
        PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(to.getClass());
        List<String> ignoreList = Arrays.asList(ignores);
 
        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod == null || ignoreList.contains(writeMethod.getName())) {
                continue;
            }
 
            PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(from.getClass(), targetPd.getName());
            if (sourcePd == null) {
                continue;
            }
 
            Method readMethod = sourcePd.getReadMethod();
            if (!(readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType()))) {
                continue;
            }
 
            try {
                // 性能考虑?
                if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                    readMethod.setAccessible(true);
                }
                Object value = readMethod.invoke(from);
                // 空字符串,作为NULL值处理
                if (value instanceof String && StringUtils.isBlank(value.toString())) {
                    value = null;
                }
 
                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                    writeMethod.setAccessible(true);
                }
                writeMethod.invoke(to, value);
 
            } catch (Throwable ex) {
                throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex);
            }
 
        }
 
    }
 
    /**
     * 概 述: (浅)拷贝对象属性到目标对象,仅仅拷贝设置的属性 <br/>
     * 创 建 人:wuqianlun <br/>
     * 创建时间:2016年4月5日下午7:26:04<br/>
     * 修 改 人:wuqianlun<br/>
     * 修改日期:2016年4月5日下午7:26:04<br/>
     *
     * @param from
     * @param to
     * @param includes
     */
    public static void copyIncludeProperties(Object from, Object to, String... includes) {
        Assert.notNull(from, "Source must not be null");
        Assert.notNull(to, "Target must not be null");
        Assert.notNull(includes, "includeProperties must not be null");
        if (includes.length == 0) {
            return;
        }
 
        PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(to.getClass());
        List<String> includeList = Arrays.asList(includes);
 
        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (!(writeMethod != null && includeList.contains(targetPd.getName()))) {
                continue;
            }
 
            PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils.getPropertyDescriptor(from.getClass(), targetPd.getName());
            if (sourcePd == null) {
                continue;
            }
 
            Method readMethod = sourcePd.getReadMethod();
            if (!(readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType()))) {
                continue;
            }
 
            try {
                // 性能考虑?
                if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                    readMethod.setAccessible(true);
                }
                Object value = readMethod.invoke(from);
                // 空字符串,作为NULL值处理
                if (value instanceof String && StringUtils.isBlank(value.toString())) {
                    value = null;
                }
 
                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                    writeMethod.setAccessible(true);
                }
                writeMethod.invoke(to, value);
            } catch (Throwable ex) {
                throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex);
            }
        }
    }
}