import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
public class TransUtil {
public static void transObjectType(Object srcObject, Class classType,
Object tragetObject) {
if (classType != null) {
Class to = tragetObject.getClass();
Method[] sm = classType.getMethods();
for (Method stemp : sm) {
String smethodName = stemp.getName();
Class sParams[] = stemp.getParameterTypes();
Class sRtnType = stemp.getReturnType();
if (smethodName != null && smethodName.startsWith("get")
&& sParams != null && sRtnType != null) {
try {
Method tm = to.getDeclaredMethod("set"
+ smethodName
.substring(3, smethodName.length()),
sRtnType);
if (tm != null) {
Object setValue = stemp.invoke(srcObject, null);
if (setValue != null) {
tm.invoke(tragetObject, setValue);
}
}
} catch (NoSuchMethodException eee) {
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else {
Class superClass = classType.getSuperclass();
transObjectType(srcObject, superClass, tragetObject);
}
}
public static Object transObjectForObjectType(Object srcObject,
Class tragetObject) {
Object to = null;
try {
to = Class.forName(tragetObject.getName()).newInstance();
transObjectType(srcObject, srcObject.getClass(), to);
} catch (Exception e) {
e.printStackTrace();
}
return to;
}
public static void transObject(Object srcObject, Object tragetObject) {
transObjectType(srcObject, srcObject.getClass(), tragetObject);
}
/**
* 把原目标的属性值,按照傳的類的類型,分別赋值给的目标对象(屬性)里
*
* @param srcObject
* @param classType
* @param tragetObject
* @throws Exception
*/
public static void transObjectForFieldType(Object srcObject,
Class classType, Object tragetObject) throws Exception {
if (classType != null) {
Class to = tragetObject.getClass();
Field[] sf = classType.getDeclaredFields();
Field[] tf = to.getDeclaredFields();
for (Field stemp : sf) {
stemp.setAccessible(true);
for (Field ttemp : tf) {
if (stemp != null && ttemp != null
&& stemp.getName().equals(ttemp.getName())
&& stemp.getType().equals(ttemp.getType())) {
ttemp.setAccessible(true);
ttemp.set(tragetObject, stemp.get(srcObject));
}
}
}
Class superClass = classType.getSuperclass();
transObjectForFieldType(srcObject, superClass, tragetObject);
}
}
/**
* 把原目标的属性(包括父类的)值赋给,目标对象(相同的)属性里
*
* @param srcObject
* @param tragetObject
* @throws Exception
*/
public static void transObjectForField(Object srcObject, Object tragetObject)
throws Exception {
transObjectForFieldType(srcObject, srcObject.getClass(), tragetObject);
}
/**
* 把原目标的属性(包括父类的)值赋给,按照目标对象新创建的目标对象,并(相同的)属性里
*
* @param srcObject
* @param tragetObject
* @throws Exception
*/
public static Object transObjectForFieldType(Object srcObject,
Class tragetObject) throws Exception {
Object to = null;
try {
to = Class.forName(tragetObject.getName()).newInstance();
transObjectForFieldType(srcObject, srcObject.getClass(), to);
} catch (Exception e) {
e.printStackTrace();
}
return to;
}
public static void main(String[] args) throws Exception {
ObjTestBean o1 = new ObjTestBean();
o1.setGroupName("ssss");
o1.setName("2323xds");
o1.setNewTime("sssdf0998");
o1.setPo("666666666");
TestBean tb = new TestBean();
tb.setId("1213");
tb.setName("23434");
tb.setPo("src");
tb.setNewTime("sdddsddsd0099");
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
System.out.println("-----------111------------");
//当然这个是最简单的做法
BeanUtils.copyProperties(tb, o1);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
System.out.println("-----------222-------------");
Object oo = TransUtil.transObjectForFieldType(o1, TestBean.class);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + oo);
System.out.println("------------------------");
TransUtil.transObject(o1, tb);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
}
}
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
public class TransUtil {
public static void transObjectType(Object srcObject, Class classType,
Object tragetObject) {
if (classType != null) {
Class to = tragetObject.getClass();
Method[] sm = classType.getMethods();
for (Method stemp : sm) {
String smethodName = stemp.getName();
Class sParams[] = stemp.getParameterTypes();
Class sRtnType = stemp.getReturnType();
if (smethodName != null && smethodName.startsWith("get")
&& sParams != null && sRtnType != null) {
try {
Method tm = to.getDeclaredMethod("set"
+ smethodName
.substring(3, smethodName.length()),
sRtnType);
if (tm != null) {
Object setValue = stemp.invoke(srcObject, null);
if (setValue != null) {
tm.invoke(tragetObject, setValue);
}
}
} catch (NoSuchMethodException eee) {
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else {
Class superClass = classType.getSuperclass();
transObjectType(srcObject, superClass, tragetObject);
}
}
public static Object transObjectForObjectType(Object srcObject,
Class tragetObject) {
Object to = null;
try {
to = Class.forName(tragetObject.getName()).newInstance();
transObjectType(srcObject, srcObject.getClass(), to);
} catch (Exception e) {
e.printStackTrace();
}
return to;
}
public static void transObject(Object srcObject, Object tragetObject) {
transObjectType(srcObject, srcObject.getClass(), tragetObject);
}
/**
* 把原目标的属性值,按照傳的類的類型,分別赋值给的目标对象(屬性)里
*
* @param srcObject
* @param classType
* @param tragetObject
* @throws Exception
*/
public static void transObjectForFieldType(Object srcObject,
Class classType, Object tragetObject) throws Exception {
if (classType != null) {
Class to = tragetObject.getClass();
Field[] sf = classType.getDeclaredFields();
Field[] tf = to.getDeclaredFields();
for (Field stemp : sf) {
stemp.setAccessible(true);
for (Field ttemp : tf) {
if (stemp != null && ttemp != null
&& stemp.getName().equals(ttemp.getName())
&& stemp.getType().equals(ttemp.getType())) {
ttemp.setAccessible(true);
ttemp.set(tragetObject, stemp.get(srcObject));
}
}
}
Class superClass = classType.getSuperclass();
transObjectForFieldType(srcObject, superClass, tragetObject);
}
}
/**
* 把原目标的属性(包括父类的)值赋给,目标对象(相同的)属性里
*
* @param srcObject
* @param tragetObject
* @throws Exception
*/
public static void transObjectForField(Object srcObject, Object tragetObject)
throws Exception {
transObjectForFieldType(srcObject, srcObject.getClass(), tragetObject);
}
/**
* 把原目标的属性(包括父类的)值赋给,按照目标对象新创建的目标对象,并(相同的)属性里
*
* @param srcObject
* @param tragetObject
* @throws Exception
*/
public static Object transObjectForFieldType(Object srcObject,
Class tragetObject) throws Exception {
Object to = null;
try {
to = Class.forName(tragetObject.getName()).newInstance();
transObjectForFieldType(srcObject, srcObject.getClass(), to);
} catch (Exception e) {
e.printStackTrace();
}
return to;
}
public static void main(String[] args) throws Exception {
ObjTestBean o1 = new ObjTestBean();
o1.setGroupName("ssss");
o1.setName("2323xds");
o1.setNewTime("sssdf0998");
o1.setPo("666666666");
TestBean tb = new TestBean();
tb.setId("1213");
tb.setName("23434");
tb.setPo("src");
tb.setNewTime("sdddsddsd0099");
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
System.out.println("-----------111------------");
//当然这个是最简单的做法
BeanUtils.copyProperties(tb, o1);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
System.out.println("-----------222-------------");
Object oo = TransUtil.transObjectForFieldType(o1, TestBean.class);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + oo);
System.out.println("------------------------");
TransUtil.transObject(o1, tb);
System.out.println("ObjTestBean:" + o1);
System.out.println("TestBean:" + tb);
}
}