美文网首页
BeanCopyUtils

BeanCopyUtils

作者: __简单点__ | 来源:发表于2020-12-03 11:58 被阅读0次

    public class BeanCopyUtils {

    /**

        * 拷贝对象

        * @param source

        * @param classType

        * @return

        */

        public static E copy(T source, Class classType) {

    if (source ==null) {

    return null;

            }

    E targetInstance =null;

            try {

    targetInstance = classType.newInstance();

            }catch (InstantiationException e) {

    throw new RuntimeException(e);

            }catch (IllegalAccessException e) {

    throw new RuntimeException(e);

            }

    BeanUtils.copyProperties(source, targetInstance);

            return targetInstance;

        }

    /**

        * 拷贝集合

        * @param sourceList

        * @param classType

        * @return

        */

        public static ListcopyList(List sourceList, Class classType) {

    if (sourceList ==null) {

    return null;

            }

    List result =new ArrayList();

            int size = sourceList.size();

            for (int i =0; i < size; i++) {

    result.add(copy(sourceList.get(i), classType));

            }

    return result;

        }

    }

    相关文章

      网友评论

          本文标题:BeanCopyUtils

          本文链接:https://www.haomeiwen.com/subject/dlqnmhtx.html