diff --git a/src/com/platform/utils/BeanCopy.java b/src/com/platform/utils/BeanCopy.java index 29ca3cc7..f6cee9fc 100644 --- a/src/com/platform/utils/BeanCopy.java +++ b/src/com/platform/utils/BeanCopy.java @@ -6,10 +6,15 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.log4j.Logger; + import com.fasterxml.jackson.databind.PropertyNamingStrategy.UpperCamelCaseStrategy; public class BeanCopy { + @SuppressWarnings("static-access") + public static Logger log = Configs.DAILY_ROLLING_LOGGER.getLogger(BeanCopy.class); + /** 复制属性(子类父类等继承关系不可用) * @param source 源对象 * @param dst 目标对象 @@ -53,6 +58,35 @@ public class BeanCopy { } + public static void copyField(Object src, Object dst,String... filter) { + List filters = Arrays.asList(filter); + List fields = new ArrayList(); + Class classobj = src.getClass(); + if (null != classobj) { + Field[] fs = classobj.getDeclaredFields(); + for (Field field : fs) { + field.setAccessible(true); + fields.add(field); + } + fields = findFatherField(classobj, fields); + } + for (Field field : fields) { + if (filters.contains(field.getName())) { + continue; + } + // dst obj's property set value + try { + field.set(dst, field.get(src)); + } catch (SecurityException e) { + log.error(field.getName() + " is unsafe : SecurityException"); + } catch (IllegalArgumentException e) { + log.error(field.getName() + " is inexist or null or OutOfRange : ArgumentException "); + } catch (IllegalAccessException e) { + log.error(field.getName() + " Access is false : AccessException"); + } + } + } + private static void fatherCopyChild(Object father, Object dst, String... params) throws Exception{ List filter = new ArrayList(); filter.addAll(Arrays.asList(params)); @@ -74,6 +108,21 @@ public class BeanCopy { } } + private static List findFatherField(Class classObj, List fields) { + Class classz = classObj.getSuperclass(); + if (null != classz) { + Field[] field_array = classz.getDeclaredFields(); + if (field_array.length > 0) { + for (Field field : field_array) { + field.setAccessible(true); + fields.add(field); + } + findFatherField(classz,fields); + } + } + return fields; + } + private static String upperHeadChar(String in) { String head = in.substring(0,1); return head.toUpperCase()+in.substring(1, in.length());