您的位置:首页 > 其它

类和类之间属性复制,必须存在get和set方法的属性才可以复制

2015-09-11 10:38 375 查看
package com.hjzx.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ModelCopy {

/**
* 类和类之间属性复制,必须存在get和set方法的属性才可以复制
* @param source 源对象
* @param target 目标对象
* @return
*/
public static void copy(Object source,Object target){
Field[] fields = source.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
copyField(source,target,fields[i]);
}
}

private static void copyField(Object source,Object target,Field field){
String fieldName = null;
String getFieldName = null;
String setFieldName = null;
Object value = null;
try{
fieldName = field.getName();
String mFieldName = fieldName.replaceFirst(fieldName.substring(0,1), fieldName.substring(0,1).toUpperCase());
getFieldName = "get" + mFieldName;
Method getMethod = source.getClass().getMethod(getFieldName, new Class[] {});
value = getMethod.invoke(source, new Object[] {});
if(value == null){
return;
}
setFieldName = "set" + mFieldName;
Method setMethod = getMethod(target,setFieldName);
if(setMethod == null){
return;
}
setMethod.invoke(target,  new Object[] {value});
}catch(Exception e){
}
}

private static Method getMethod(Object target,String setFieldName){
Method method = null;
Method[] methods = target.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
method = methods[i];
if(method.getName().equals(setFieldName)){
return method;
}
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: