集册 Java实例教程 将Bean复制到Bean

将Bean复制到Bean

欢马劈雪     最近更新时间:2020-01-02 10:19:05

38
将Bean复制到Bean


//package com.nowjava;/* 来自 nowjava*/

import java.lang.reflect.Array;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.util.ArrayList;


import java.util.List;


public class Main {


    public static List copyBeansToBean(Object beans, Class bean, int size) {

        List list = new ArrayList();

        try {

            if (beans != null) {

                int start = 0;/** 时 代 J a v a 公 众 号 - nowjava.com 提 供 **/

                while (start < size) {

                    Object addobj = Class.forName(bean.getName())

                            .getConstructor(null).newInstance(null);

                    Field[] field = bean.getDeclaredFields();

                    for (int i = 0; i < field.length; i++) {


                        String fieldname = field[i].getName();

                        String setMethodname = new StringBuffer("set")

                                .append(fieldname.substring(0, 1)

                                        .toUpperCase())

                                .append(fieldname.substring(1)).toString();

                        String getMethodname = new StringBuffer("get")

                                .append(fieldname.substring(0, 1)

                                        .toUpperCase())

                                .append(fieldname.substring(1)).toString();

                        try {

                            Method method = beans.getClass().getMethod(

                                    getMethodname, null);

                            Object arr = method.invoke(beans, null);

                            //System.out.println(fieldname+"----"+arr);

                            int tempsize = arr == null ? 0 : Array

                                    .getLength(arr);

                            //System.out.println("tempsize="+tempsize);

                            if (tempsize >= size) {

                                Class[] cla = new Class[1];

                                cla[0] = bean.getDeclaredField(fieldname)

                                        .getType();

                                //System.out.println(cla[0].getName());

                                Object[] paras = new Object[1];

                                paras[0] = Array.get(arr, start);

                                //System.out.println(Array.get(arr,start));

                                bean.getMethod(setMethodname, cla).invoke(

                                        addobj, paras);

                            } else {

                                Class[] cla = new Class[1];

                                cla[0] = bean.getDeclaredField(fieldname)

                                        .getType();

                                //System.out.println(cla[0].getName());

                                Object[] paras = new Object[1];

                                paras[0] = null;

                                //System.out.println(Array.get(arr,start));

                                bean.getMethod(setMethodname, cla).invoke(

                                        addobj, paras);

                            }

                        } catch (Exception ex) {

                            Class[] cla = new Class[1];

                            cla[0] = bean.getDeclaredField(fieldname)

                                    .getType();

                            //System.out.println(cla[0].getName());

                            Object[] paras = new Object[1];

                            paras[0] = null;

                            //System.out.println(Array.get(arr,start));

                            bean.getMethod(setMethodname, cla).invoke(

                                    addobj, paras);

                        }


                    }

                    list.add(addobj);

                    start++;

                }

                //System.out.println("list size="+start);

            }


        } catch (Exception e) {

            //            log.error("BeansList,?bean", e);

        }

        //System.out.println("list size="+list.size());

        return list;

    }


    public static List copyBeansToBean(Object beans, List bean) {

        List list = bean;

        int size = list.size();

        try {

            if (beans != null) {

                int start = 0;

                while (start < size) {

                    Object addobj = list.get(start);

                    Class beanclass = addobj.getClass();

                    Field[] field = beanclass.getDeclaredFields();

                    for (int i = 0; i < field.length; i++) {

                        try {

                            String fieldname = field[i].getName();

                            String setMethodname = new StringBuffer("set")

                                    .append(fieldname.substring(0, 1)

                                            .toUpperCase())

                                    .append(fieldname.substring(1))

                                    .toString();

                            String getMethodname = new StringBuffer("get")

                                    .append(fieldname.substring(0, 1)

                                            .toUpperCase())

                                    .append(fieldname.substring(1))

                                    .toString();

                            Method method = beans.getClass().getMethod(

                                    getMethodname, null);

                            Object arr = method.invoke(beans, null);

                            //System.out.println(fieldname+"----"+arr);

                            int tempsize = arr == null ? 0 : Array

                                    .getLength(arr);

                            //System.out.println("tempsize="+tempsize);

                            
展开阅读全文