copy Beans To Bean - Java Reflection

Java examples for Reflection:Java Bean

Description

copy Beans To Bean

Demo Code


//package com.java2s;
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 {/* w ww . ja  v a2s  .  com*/
            if (beans != null) {
                int start = 0;
                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);
                            if (tempsize == size) {
                                Class[] cla = new Class[1];
                                cla[0] = beanclass.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));
                                beanclass.getMethod(setMethodname, cla)
                                        .invoke(addobj, paras);
                            }
                        } catch (Exception ex) {
                        }

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

        } catch (Exception e) {
            //            log.error("BeansList,?bean", e);
        }
        //System.out.println("list size="+list.size());
        return list;
    }
}

Related Tutorials