Java tutorial
//package com.java2s; import java.lang.reflect.Field; public class Main { public static void objectCopy(Object from, Object to) throws Exception { if (from.getClass() != to.getClass()) { throw new IllegalArgumentException("[objectCopy]The left and right must be same class"); } Class<?> clz = from.getClass(); Field[] fs = clz.getDeclaredFields(); for (int i = 0; i < fs.length; i++) { Field field = fs[i]; field.setAccessible(true); Object value = field.get(from); field.set(to, value); } } }