java开发中常用工具的封装——对象属性复制

  在Java开发中,总是会碰到一些类似功能的重复使用,
其实这就可以封装成为一个工具类。
在后台接收前端传输过来的参数时,接收对象和实际操作对象往往会不一致,如果传输过来的参数都是自己手动set到实际对象中去的话, 效率会很低,怎么办呢?
其实,我们可以封装这样一个方法,利用反射对复制对象和非复制对象做一个属性的复制操作,这样便可以达到我们所需要的效果
这是小编最近封装的一个对象复制工具,或许很多第三方库都会存在类似方法,但是如果你为了使用该方法直接引入一个第三方库, 那显然没有太大的必要
版权所有,如需转载请标明出处,谢谢
源码如下:
package com.tobiasy.toolkit.bean;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/**
 * @author tobiasy
 */
public class BeanUtils {
    public static Long copyProperties(Object source, Object target) {
        Long start = System.currentTimeMillis();
        if (source == null) {
            return System.currentTimeMillis() - start;
        }
        Class<? extends Object> sourceClass = source.getClass();
        Class<? extends Object> targetClass = target.getClass();
        List<Field> sourceField = getExtendsField(sourceClass);
        List<Field> targetField = getExtendsField(targetClass);
        for (Field field : sourceField) {
            String fieldName = field.getName();
            for (Field trg : targetField) {
                String fieldName1 = trg.getName();
                Boolean f = field.getType().getName().equals(trg.getType().getName());
                if (fieldName.equals(fieldName1) && f) {
                    String methodSuffix = capitalizeCase(fieldName);
                    try {
                        Object s1 = null;
                        s1 = invoke(source, "get" + methodSuffix);
                        invoke(target, "set" + methodSuffix, trg.getType(), s1);
                    } catch (NoSuchMethodException e) {
                        continue;
                    }
                }
            }
        }
        return System.currentTimeMillis() - start;
    }

    public static List<Field> getExtendsField(Class<? extends Object> clazz) {
        Field[] superField = clazz.getSuperclass().getDeclaredFields();
        Field[] targetField = clazz.getDeclaredFields();
        List<Field> fieldList = new ArrayList<>();
        for (Field field : superField) {
            fieldList.add(field);
        }
        for (Field field : targetField) {
            Boolean f = true;
            for (Field field1 : superField) {
                if (field1.getName().equals(field.getName())) {
                    f = false;
                }
            }
            if (f) {
                fieldList.add(field);
            }
        }
        return fieldList;
    }

    public static String capitalizeCase(String str) {
        char[] ch = str.toCharArray();
        char a = 'a';
        char z = 'z';
        if (ch[0] >= a && ch[0] <= z) {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static Object invoke(Object obj, String methodName) throws NoSuchMethodException {
        Object value = null;
        Method getMethod = obj.getClass().getMethod(methodName, new Class[]{});
        if (getMethod == null) {
            return null;
        }
        try {
            value = getMethod.invoke(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return value;
    }

    public static Object invoke(Object obj, String method, Object parameter) {
        Object result = null;
        try {
            Method m = null;
            if (parameter instanceof Integer) {
                m = obj.getClass().getMethod(method, int.class);
            } else {
                m = obj.getClass().getMethod(method, parameter.getClass());
            }
            result = m.invoke(obj, parameter);
        } catch (Exception e) {
            return null;
        }
        return result;
    }

    public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class... paramTypes) {
        try {
            return clazz.getDeclaredMethod(methodName, paramTypes);
        } catch (NoSuchMethodException var4) {
            return clazz.getSuperclass() != null?findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes):null;
        }
    }

    public static Method findMethod(Class clazz, String methodName, Class... paramTypes){
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return findDeclaredMethod(clazz, methodName, paramTypes);        }
    }
    public static Object invoke(Object object, String methodName, Class paramTypes, Object parameters){
        Class<?> clazz = object.getClass();
        Method method = findMethod(clazz, methodName, paramTypes);
        if (method == null) {
            return null;
        }
        try {
            return method.invoke(object, parameters);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 


posted @ 2018-07-04 10:15  孤独的拾荒者  阅读(224)  评论(0编辑  收藏  举报