copy from src object to dest object. - Java Reflection

Java examples for Reflection:Object

Description

copy from src object to dest object.

Demo Code


//package com.java2s;
import java.lang.reflect.Method;

import java.util.Collection;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object src = "java2s.com";
        Object dest = "java2s.com";
        copyAll(src, dest);//from   w  ww.j  a  v a 2s. c om
    }

    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 float[]) {
                return (((float[]) result).length == 0);
            } else if (result instanceof double[]) {
                return (((double[]) result).length == 0);
            } else if (result instanceof char[]) {
                return (((char[]) result).length == 0);
            } else if (result instanceof boolean[]) {
                return (((boolean[]) result).length == 0);
            } else {
                //result instanceof Object[]
                return (((Object[]) result).length == 0);
            }
        }

        return false;
    }
}

Related Tutorials