同类对象之间非空属性间赋值

/*
* 同类对象之间非空属性间赋值
* 将origin属性注入到destination中
* */
public static  void mergeObject(T origin, T destination) {
    if (origin == null || destination == null)
        return;
    if (!origin.getClass().equals(destination.getClass()))
        return;

    Field[] fields = origin.getClass().getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        try {
            fields[i].setAccessible(true);
            Object value = fields[i].get(origin);
            if (null != value) {
                fields[i].set(destination, value);
            }
            fields[i].setAccessible(false);
        } catch (Exception e) {
        }
    }
}

你可能感兴趣的:(同类对象之间非空属性间赋值)