|
1 |
| package photospace.beans; |
|
2 |
| |
|
3 |
| import java.beans.*; |
|
4 |
| import java.util.*; |
|
5 |
| import org.springframework.beans.*; |
|
6 |
| |
|
7 |
| public class Beans |
|
8 |
| { |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
2
| public static void merge(Object from, Object to)
|
|
16 |
| { |
|
17 |
2
| copy(from, to, false);
|
|
18 |
| } |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
1
| public static void copy(Object from, Object to)
|
|
26 |
| { |
|
27 |
1
| copy(from, to, true);
|
|
28 |
| } |
|
29 |
| |
|
30 |
3
| private static void copy(Object from, Object to, boolean overwrite)
|
|
31 |
| { |
|
32 |
3
| BeanWrapper fromBean = new BeanWrapperImpl(from);
|
|
33 |
3
| BeanWrapper toBean = new BeanWrapperImpl(to);
|
|
34 |
3
| for (Iterator i = getProperties(from).iterator(); i.hasNext();)
|
|
35 |
| { |
|
36 |
15
| String property = (String) i.next();
|
|
37 |
3
| if (!fromBean.isReadableProperty(property)) continue;
|
|
38 |
6
| if (!toBean.isWritableProperty(property)) continue;
|
|
39 |
| |
|
40 |
6
| Object oldValue = toBean.getPropertyValue(property);
|
|
41 |
6
| if (overwrite || oldValue == null || "".equals(oldValue))
|
|
42 |
| { |
|
43 |
4
| toBean.setPropertyValue(property, fromBean.getPropertyValue(property));
|
|
44 |
| } |
|
45 |
| } |
|
46 |
| } |
|
47 |
| |
|
48 |
3
| public static Collection getProperties(Object obj)
|
|
49 |
| { |
|
50 |
3
| BeanWrapper bean = new BeanWrapperImpl(obj);
|
|
51 |
3
| PropertyDescriptor[] properties = bean.getPropertyDescriptors();
|
|
52 |
3
| List names = new ArrayList();
|
|
53 |
3
| for (int i = 0; i < properties.length; i++)
|
|
54 |
| { |
|
55 |
15
| names.add(properties[i].getName());
|
|
56 |
| } |
|
57 |
3
| return names;
|
|
58 |
| } |
|
59 |
| } |