Java两个对象之间通过拷贝进行赋值

两个对象拥有相同的字段,这两个可以通过拷贝互相给另一个对象进行赋值。通过使用下面两个工具类可以实现该功能。

        使用方法 如上,DbzAecopdVoEntity和DbzAecopd1Entity具有相同的属性,通过BeanUtils.copyProperties(aecopd1Entity,dbzAecopd),可以将DbzAecopdVoEntity相同属性的值赋值给DbzAecopd1Entity。

以下是工具类代码

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;


public class BeanUtils {

    public static void copyProperties(Object to, Object from) {
        Collection getters = IntrospectorUtil.getGetters(from.getClass());

        for (Iterator iterator = getters.iterator(); iterator.hasNext();) {
            Method getter = (Method) iterator.next();

            String propertyName = IntrospectorUtil.getPropertyName(getter.getName());


            try {
                //On r閏up鑢e le setter s'il existe
                Method setter = to.getClass().getDeclaredMethod(IntrospectorUtil.getSetterMethodName(propertyName),
                        new Class[]{getter.getReturnType()});

                Object getterResult = getter.invoke(from, new Object[0]);

                setter.invoke(to, new Object[]{getterResult});
            } catch (NoSuchMethodException ex) {
                //Pas grave on passe au suivant
                continue;
            } catch (IllegalAccessException ex) {
                continue;
            } catch (InvocationTargetException ex) {
                continue;
            }
        }
    }

}

 

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IntrospectorUtil {

    public static Collection getGetters(Class classToAnalyze) {
        ArrayList result = new ArrayList();

        Method[] methods = classToAnalyze.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (isGetter(method)) result.add(method);
        }
        return result;
    }

    public static boolean isGetter(Method method) {
        String methodName = method.getName();

        if (!methodName.startsWith("get") &&
                !methodName.startsWith("is")) {
            return false;
        }

        if (methodName.startsWith("is") && (!method.getReturnType().equals(boolean.class)))
            return false;

        if (method.getReturnType().equals(void.class)) return false;

        if (method.getParameterTypes().length != 0) return false;

        return true;

    }

    public static Collection getSetters(Class classToAnalyze) {
        ArrayList result = new ArrayList();

        Method[] methods = classToAnalyze.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (isSetter(method)) result.add(method);
        }
        return result;
    }

    public static boolean isSetter(Method method) {
        String methodName = method.getName();

        if (!methodName.startsWith("set")) return false;

        if (method.getParameterTypes().length != 1) return false;

        return true;
    }

    public static String getPropertyName(String getterOrSetterName) {
        if (getterOrSetterName.startsWith("set") ||
                getterOrSetterName.startsWith("get"))
            return decapitalize(getterOrSetterName.substring(3));
        if (getterOrSetterName.startsWith("is"))
            return decapitalize(getterOrSetterName.substring(2));
        throw new IllegalArgumentException(getterOrSetterName + " is not a correct getter or setter"
                + "name");
    }

    public static String decapitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                Character.isUpperCase(name.charAt(0))) {
            return name;
        }
        char chars[] = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }

    public static String getSetterMethodName(String propertyName) {
        return "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    }

    public static String getBooleanGetterMethodName(String propertyName) {
        return "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    }

    public static String getGetterMethodName(String propertyName) {
        return "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    }


    public static void copyFields(Object from, Object to) {
        Collection getters = IntrospectorUtil.getGetters(from.getClass());

        for (Iterator iterator = getters.iterator(); iterator.hasNext();) {
            Method getter = (Method) iterator.next();

            String propertyName = IntrospectorUtil.getPropertyName(getter.getName());


            try {
                Method setter = to.getClass().getDeclaredMethod(IntrospectorUtil.getSetterMethodName(propertyName),
                        new Class[]{getter.getReturnType()});

                Object getterResult = getter.invoke(from, new Object[0]);

                setter.invoke(to, new Object[]{getterResult});
            } catch (NoSuchMethodException ex) {
                continue;
            } catch (IllegalAccessException ex) {
                continue;
            } catch (InvocationTargetException ex) {
                continue;
            }
        }
    }
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37773018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值