KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > bean > BeanCopy


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.bean;
4
5 import jodd.introspector.ClassDescriptor;
6 import jodd.introspector.DefaultIntrospector;
7
8 /**
9  * Various bean copy methods.
10  */

11 public class BeanCopy {
12
13     // ---------------------------------------------------------------- misc
14

15     /**
16      * Copies properties of one bean to another. It iterates all getXXX() methods,
17      * reads values and populates destination bean through setXXX().
18      *
19      * @param source source bean, one to read properties from
20      * @param destination destination bean, to write properties to
21      * @param supressSecurity
22      */

23     public static void copyProperties(Object JavaDoc source, Object JavaDoc destination, boolean supressSecurity) {
24         ClassDescriptor cdSrc = DefaultIntrospector.lookup(source.getClass());
25         ClassDescriptor cdDest = DefaultIntrospector.lookup(destination.getClass());
26
27         String JavaDoc[] mdata = cdSrc.getAllBeanGetterNames(supressSecurity);
28         for (int i = 0; i < mdata.length; i++) {
29             String JavaDoc name = mdata[i];
30
31             if (cdDest.getBeanSetter(name, supressSecurity) == null) {
32                 return;
33             }
34             try {
35                 Object JavaDoc value = BeanUtil.getProperty(source, name);
36                 BeanUtil.setProperty(destination, name, value);
37             } catch (BeanException bex) {
38                 // ignore
39
}
40         }
41     }
42
43     /**
44      * Copies only public properties.
45      * @param source
46      * @param destination
47      * @see #copyProperties(Object, Object, boolean)
48      */

49     public static void copyProperties(Object JavaDoc source, Object JavaDoc destination) {
50         copyProperties(source, destination, false);
51     }
52 }
53
Popular Tags