ClassReflection(Copy bean and list) 运用反射来实现bean或list的弱引用

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by TerryZhang on 2017/1/21.
 */

public class ClassReflection {
    /**
     * @param from 用于赋值的实体类
     * @param to   需要待赋值的实体类
     * @author ym
     * @CreateTime 2012-11-22下午03:23:23
     * 描述:反射实体类赋值
     */
    public static void reflectionAttr(Object from, Object to) throws Exception {
        Class clazz1 = Class.forName(from.getClass().getName());
        Class clazz2 = Class.forName(to.getClass().getName());
//      获取两个实体类的所有属性
        Field[] fields1 = clazz1.getDeclaredFields();
        Field[] fields2 = clazz2.getDeclaredFields();
        ClassReflection cr = new ClassReflection();
//      遍历class1Bean,获取逐个属性值,然后遍历class2Bean查找是否有相同的属性,如有相同则赋值
        for (Field f1 : fields1) {
            if (f1.getName().equals("id"))
                continue;
            Object value;
            if (f1.getType() == Boolean.class||f1.getType()==boolean.class)
                value = cr.invokeGetMethod(from, f1.getName(), true);
            else
                value = cr.invokeGetMethod(from, f1.getName(), false);
            for (Field f2 : fields2) {
                if (f1.getName().equals(f2.getName())) {
                    Object[] obj = new Object[1];
                    obj[0] = value;
                    cr.invokeSetMethod(to, f2.getName(), obj);
                }
            }
        }

    }

    /**
     * 执行某个Field的getField方法
     *
     * @param clazz     类
     * @param fieldName 类的属性名称
     * @param isBoolean 是否为boolean类型
     * @return
     */
    private Object invokeGetMethod(Object clazz, String fieldName, boolean isBoolean) {
        String methodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        Method method;
        try {
            if (isBoolean)
                method = Class.forName(clazz.getClass().getName()).getDeclaredMethod("is" + methodName);
            else
                method = Class.forName(clazz.getClass().getName()).getDeclaredMethod("get" + methodName);
            return method.invoke(clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 执行某个Field的setField方法
     *
     * @param clazz     类
     * @param fieldName 类的属性名称
     * @param args      参数,默认为null
     * @return
     */
    private Object invokeSetMethod(Object clazz, String fieldName, Object[] args) {
        String methodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        Method method;
        try {
            Class[] parameterTypes = new Class[1];
            Class c = Class.forName(clazz.getClass().getName());
            Field field = c.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            method = c.getDeclaredMethod("set" + methodName, parameterTypes);
            return method.invoke(clazz, args);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 深度copy list 内部bean 需要序列化
     *
     * @param src 来源list
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static List deepCopy(List src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        List dest = (List) in.readObject();
        return dest;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值