Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { public static Object mergedObject(Object oldObject, Object newObject) throws Exception { Class<?> cls = newObject.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); String strGet = "get" + method; Method methodGet = cls.getDeclaredMethod(strGet); Object object = methodGet.invoke(newObject); if (object != null) { String strSet = "set" + method; Method methodSet = cls.getDeclaredMethod(strSet, clsType); Object objValue = typeConversion(clsType, object.toString()); methodSet.invoke(oldObject, objValue); } } return oldObject; } public static Object typeConversion(Class<?> cls, String str) { Object obj = null; String nameType = cls.getSimpleName(); if ("Integer".equals(nameType)) { obj = Integer.valueOf(str); } if ("String".equals(nameType)) { obj = str; } if ("Float".equals(nameType)) { obj = Float.valueOf(str); } if ("Double".equals(nameType)) { obj = Double.valueOf(str); } if ("Boolean".equals(nameType)) { obj = Boolean.valueOf(str); } if ("Long".equals(nameType)) { obj = Long.valueOf(str); } if ("Short".equals(nameType)) { obj = Short.valueOf(str); } if ("Character".equals(nameType)) { obj = str.charAt(1); } return obj; } }