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

将Java Bean复制到Java Bean

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

526
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将Java Bean复制到Java Bean


//package com.nowjava;

import java.lang.reflect.Method;/** from N o w J a v a . c o m - 时  代  Java**/


public class Main {

    public static void main(String[] argv) throws Exception {

        Object obj1 = "nowjava.com";

        Object obj2 = "nowjava.com";

        System.out.println(CopyBeanToBean(obj1, obj2));

    }


    public static Object CopyBeanToBean(Object obj1, Object obj2)

            throws Exception {

        Method[] method1 = obj1.getClass().getMethods();

        Method[] method2 = obj2.getClass().getMethods();

        String methodName1;

        String methodFix1;

        String methodName2;

        String methodFix2;
        /* 
        *来 自
         时 代 J a v a 公 众 号 - N o w J a v  a . c o m
        */

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

            methodName1 = method1[i].getName();

            methodFix1 = methodName1.substring(3, methodName1.length());

            if (methodName1.startsWith("get")) {

                for (int j = 0; j < method2.length; j++) {

                    methodName2 = method2[j].getName();

                    methodFix2 = methodName2.substring(3,

                            methodName2.length());

                    if (methodName2.startsWith("set")) {

                        if (methodFix2.equals(methodFix1)) {

                            Object[] objs1 = new Object[0];

                            Object[] objs2 = new Object[1];

                            objs2[0] = method1[i].invoke(obj1, objs1);

                            
展开阅读全文