diff --git a/src/com/platform/utils/BeanCopy.java b/src/com/platform/utils/BeanCopy.java index 802e5e0d..f6cee9fc 100644 --- a/src/com/platform/utils/BeanCopy.java +++ b/src/com/platform/utils/BeanCopy.java @@ -1,7 +1,6 @@ package com.platform.utils; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -9,8 +8,11 @@ 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); /** 复制属性(子类父类等继承关系不可用) @@ -23,7 +25,7 @@ public class BeanCopy { * @throws NoSuchFieldException * @throws SecurityException */ - public static void copyBean(Object source, Object dst, String... params){ + public static void copyBean(Object source, Object dst, String... params) throws Exception{ if (dst.getClass().getSuperclass() == source.getClass()) { fatherCopyChild(source, dst, params); } @@ -43,24 +45,49 @@ public class BeanCopy { if (sourceFieldName.contains(name)) { field.setAccessible(true); Object value; - Method m; - try { - m = source.getClass().getMethod("get"+upperHeadChar(name)); - value = m.invoke(source); - // Field fie = source.getClass().getDeclaredField(name); - // fie.setAccessible(true); - // value = fie.get(source); - if (null != value) - field.set(dst, value); - } catch (NoSuchMethodException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { - log.error(e.getStackTrace()); - } + Method m = source.getClass().getMethod("get"+upperHeadChar(name)); + value = m.invoke(source); +// Field fie = source.getClass().getDeclaredField(name); +// fie.setAccessible(true); +// value = fie.get(source); + if (null != value) { + field.set(dst, value); + } } } } - private static void fatherCopyChild(Object father, Object dst, String... params){ + 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)); if (dst.getClass().getSuperclass() == father.getClass()) { @@ -74,15 +101,26 @@ public class BeanCopy { } f.setAccessible(true); Class type = f.getType(); - try { - Method m = faClass.getMethod("get"+upperHeadChar(f.getName())); - Object obj = m.invoke(father); - f.set(dst, obj); - } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - log.error(e.getStackTrace()); + Method m = faClass.getMethod("get"+upperHeadChar(f.getName())); + Object obj = m.invoke(father); + f.set(dst, obj); + } + } + } + + 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) {