自己写了个copyProperties方法,虽没apache与spring的强大,但功能自己够用就好,而且不用引入第三方库。
package bean;
import java.lang.reflect.Method;
/**
* @version 1.0
* @author fireinjava
* @data Dec 11, 2009
* @描述: 存放字段及其要用到的对应get(来自sourceBean)与set(来自targetBean)方法
*/
public class PropertyMethod {
private String propertyName;
private Method methodRead;
private Method methodWrite;
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public Method getMethodRead() {
return methodRead;
}
public void setMethodRead(Method methodRead) {
this.methodRead = methodRead;
}
public Method getMethodWrite() {
return methodWrite;
}
public void setMethodWrite(Method methodWrite) {
this.methodWrite = methodWrite;
}
}
package bean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @version 1.0
* @author fireinjava
* @data Dec 11, 2009
* @描述:拷贝Bean(适用于标准JavaBean,且拷贝的属性类型必须一致)
*/
public class BeanUtils {
public static void copyProperties(Object source, Object target) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
copyProperties(source, target, null);
}
/**
* 当某个属性不必复制或两个Bean属性名称一样且类型不一样时用(得另外设值)
*/
public static void copyProperties(Object source, Object target, String[] ignoreProperties)
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
if (source == null)
throw new IllegalArgumentException("Source must not be null");
else if (target == null)
throw new IllegalArgumentException("Target must not be null");
List<String> ignoreList = null;
if (ignoreProperties != null)
ignoreList = Arrays.asList(ignoreProperties);
List<PropertyMethod> listPm = new ArrayList<PropertyMethod>();
Map<String, Method> mapMethodWrite = new HashMap<String, Method>();
Map<String, Method> mapMethodRead = new HashMap<String, Method>();
Method[] methodTarget = target.getClass().getMethods();
for (Method mt : methodTarget)
if (mt.getName().startsWith("set")) {
String propertyName = mt.getName().substring(3, 4).toLowerCase() + mt.getName().substring(4);
if (ignoreList == null || !ignoreList.contains(propertyName))
mapMethodWrite.put(propertyName, mt);
}
Method[] methodSource = source.getClass().getMethods();
for (Method ms : methodSource)
if (ms.getName().startsWith("get"))
mapMethodRead.put(ms.getName().substring(3, 4).toLowerCase() + ms.getName().substring(4), ms);
else if (ms.getName().startsWith("is"))
mapMethodRead.put(ms.getName().substring(2, 3).toLowerCase() + ms.getName().substring(3), ms);
Iterator<String> iter = mapMethodWrite.keySet().iterator();
while (iter.hasNext()) {
String propertyName = iter.next();
if (mapMethodRead.get(propertyName) != null) {
PropertyMethod pm = new PropertyMethod();
pm.setPropertyName(propertyName);
pm.setMethodRead(mapMethodRead.get(propertyName));
pm.setMethodWrite(mapMethodWrite.get(propertyName));
listPm.add(pm);
}
}
for (PropertyMethod pm : listPm) {
Object[] obj = new Object[1];
obj[0] = pm.getMethodRead().invoke(source, new Object[0]);
pm.getMethodWrite().invoke(target, obj);
}
}
}
其中List<PropertyMethod> listPm = new ArrayList<PropertyMethod>();用来存放完整的属性及属性Get/set方法,不借助 PropertyMethod而直接用两个Map也可。
另外两种copyProperties:
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
//source中的Timestamp类型属性值不可为null 否则会报错
org.springframework.beans.BeanUtils.copyProperties(source, target);
本文介绍了一种自定义的Bean拷贝方法,该方法不依赖于第三方库,通过反射机制实现了源Bean到目标Bean的属性复制。适用于属性类型相同的JavaBean对象。
529

被折叠的 条评论
为什么被折叠?



