集册 Java实例教程 从src对象复制到dest对象。

从src对象复制到dest对象。

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

723
从src对象复制到dest对象。
// 来 自 时代Java

//package com.nowjava;

import java.lang.reflect.Method;


import java.util.Collection;


public class Main {

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

        Object src = "nowjava.com";

        Object dest = "nowjava.com";

        copyAll(src, dest);

    }
/** 时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提供 **/

    public static final int SET_START = "set".length();


    public static final int IS_START = "is".length();


    public static void copyAll(Object src, Object dest) {

        Class srcClass = src.getClass();

        Class destClass = dest.getClass();


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

        //System.out.println(srcClass);

        //System.out.println(destClass);

        for (Method method : srcClass.getMethods()) {

            //System.out.println(method);

            if (method.getName().equals("getClass")) {

                continue;

            }


            if (isGetter(method)) {

                //System.out.println("is getter: " + method);

                try {

                    //System.out.println("start");


                    //

                    String methodName = getter2Setter(method.getName());


                    //System.out.println(methodName);

                    Class methodParam = method.getReturnType();


                    //System.out.println(methodParam);

                    Method setter = destClass.getMethod(methodName,

                            methodParam);


                    //System.out.println(setter);


                    //

                    Object result = method.invoke(src);


                    //System.out.println(result);

                    if (result == null) {

                        continue;

                    } else if (result instanceof Collection) {

                        if (((Collection) result).isEmpty()) {

                            continue;

                        }

                    } else if (isEmptyArray(result)) {

                        continue;

                    }


                    //

                    setter.invoke(dest, result);

                } catch (Throwable ex) {

                    //ex.printStackTrace();

                    System.err.println(ex);

                }

            }


            //System.out.println(" ////");

        }

    }


    public static boolean isGetter(Method method) {

        String name = method.getName();

        boolean hasNoParam = method.getParameterTypes().length == 0;

        boolean startsWithGet = (name.length() > SET_START)

                && name.startsWith("get");

        boolean startsWithIs = (name.length() > IS_START)

                && name.startsWith("is");


        return hasNoParam && (startsWithGet || startsWithIs);

    }


    public static String getter2Setter(String methodName) {

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

            return "s" + methodName.substring(1);

        } else if (methodName.startsWith("is")) {

            return "set" + methodName.substring(2);

        } else {

            throw new IllegalArgumentException(

                    "method not start with get or is.");

        }

    }


    public static boolean isEmptyArray(Object result) {

        if (result.getClass().isArray()) {

            if (result instanceof byte[]) {

                return (((byte[]) result).length == 0);

            } else if (result instanceof short[]) {

                return (((short[]) result).length == 0);

            } else if (result instanceof int[]) {

                return (((int[]) result).length == 0);

            } else if (result instanceof long[]) {

                return (((long[]) result).length == 0);

            } else if (result instanceof 
展开阅读全文