反照拷贝属性
反射拷贝属性
/** * 属性复制类 * * @param arg0 * 源Class * @param arg1 * 目标Class */ public static void copyProperties(Object arg0, Object arg1) { Field[] fields = arg0.getClass().getDeclaredFields(); try { for (Field field : fields) { String getName = "get" + charSwitch(field.getName()); Method getMethod = arg0.getClass().getDeclaredMethod(getName); Object obj = getMethod.invoke(arg0); if (null != obj) { String setName = "set" + charSwitch(field.getName()); Method setMethod = arg1.getClass().getDeclaredMethod( setName, obj.getClass()); setMethod.invoke(arg1, obj); } } } catch (Exception e) { throw new PortalRuntimeException(e.getMessage(), PortalErrorCodes.ERROR_COPYPROPERTIES); } } /** * 属性复制类 * * @param arg0 * 源Class * @param arg1 * 目标Class * @param classes * 要过滤的类型 */ public static void copyProperties(Object arg0, Object arg1,Class...classes) { Field[] fields = arg0.getClass().getDeclaredFields(); try { for (Field field : fields) { String getName = "get" + charSwitch(field.getName()); Method getMethod = arg0.getClass().getDeclaredMethod(getName); Object obj = getMethod.invoke(arg0); if (isClsaa(obj,classes)) { continue; } if (null != obj) { String setName = "set" + charSwitch(field.getName()); Method setMethod = arg1.getClass().getDeclaredMethod( setName, obj.getClass()); setMethod.invoke(arg1, obj); } } } catch (Exception e) { throw new PortalRuntimeException(e.getMessage(), PortalErrorCodes.ERROR_COPYPROPERTIES); } }