62,614
社区成员
发帖
与我相关
我的任务
分享
package net.javayuan.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @(#)VO2PO.java Jan 23, 2008 9:00:55 AM
*
* @author Yuan
* VO到PO的属性复制工具
*/
public class VO2PO {
private static final VO2PO INSTANCE = new VO2PO();
private VO2PO(){
//
}
public static VO2PO getInstance(){
return INSTANCE;
}
/**
* 复制属性
* @param vo 源对象
* @param po 目标对象
* */
public void copyProperties(Object vo, Object po){
Class<?> voClass = vo.getClass();
Class<?> poClass = po.getClass();
Method[] methods = voClass.getDeclaredMethods();
for (Method method : methods){
String methodName = method.getName();
String fieldName = "";
String setterName = "";
if (methodName.startsWith("get")){
fieldName = methodName.substring(3,4).toLowerCase() + methodName.substring(4);
setterName = "set" + methodName.substring(3);
}
if (methodName.startsWith("is")){
fieldName = methodName.substring(2,3).toLowerCase() + methodName.substring(3);
setterName = "set" + methodName.substring(2);
}
if (!"".equals(fieldName)){
try {
Object o = method.invoke(vo, new Object[]{});
if (o != null){
Class<?> fieldType = poClass.getDeclaredField(fieldName).getType();
Method setter = poClass.getDeclaredMethod(setterName, fieldType);
setter.invoke(po, o);
}
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (NoSuchFieldException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args){
Bean bean1 = new Bean();
bean1.setBooleanValue(true);
bean1.setStringValue(null);
bean1.setIntValue(321);
Bean bean2 = new Bean();
bean2.setBooleanValue(false);
bean2.setStringValue("bean2");
bean2.setIntValue(123);
VO2PO.getInstance().copyProperties(bean1, bean2);
System.out.println(bean2.getIntValue());
System.out.println(bean2.getStringValue());
System.out.println(bean2.isBooleanValue());
}
}
class Bean{
private int intValue;
private String stringValue;
private boolean booleanValue;
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public String getStringValue() {
return stringValue;
}
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
public boolean isBooleanValue() {
return booleanValue;
}
public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
}
public static <V, P extends V> void aMethod(V vo, P po);
/**
* Object1 和 Object2 分别是你的V 父类 和P父类
*/
public <V extends Object1,P extends Object2> void copyProperties(V vo, P po){
//action......
}
public static void copyBeanNotNullProperty(Object dest, Object orig) throws IllegalAccessException,
InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
if (orig instanceof Map) {
throw new IllegalArgumentException("No support map");
}
/* if (orig is a standard JavaBean) */
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(orig, name);
if (value != null) {
BeanUtils.copyProperty(dest, name, value);
}
} catch (NoSuchMethodException e) {
}
}
}
}