My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package com.vineetmanohar.util;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;

/**
* @author Vineet Manohar
*/
public class BeanPropertyCopyUtil {
/**
* copies properties from one object to another
*
* @param src
* the source object
* @param dest
* the destination object
* @param properties
* a list of property names that are to be copied. Each value has
* the format "srcProperty destProperty". For example,
* "name fullName" indicates that you want to copy the src.name
* value to dest.fullName. If both the srcProperty and
* destProperty property have the same name, you can omit the
* destProperty. For example, "name" indicates that you want to
* copy src.name to dest.name.
*
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static void copyProperties(Object src, Object dest,
String... properties) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
for (String property : properties) {
String[] arr = property.split(" ");
String srcProperty;
String destProperty;
if (arr.length == 2) {
srcProperty = arr[0];
destProperty = arr[1];
} else {
srcProperty = property;
destProperty = property;
}
BeanUtils.setProperty(dest, destProperty, BeanUtils.getProperty(
src, srcProperty));
}
}
}

Change log

r15 by vineet.manohar on Aug 30, 2010   Diff
used to copy and map properties from
source object to target object
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1534 bytes, 48 lines
Powered by Google Project Hosting