项目背景

我们开发过程中会碰到这样一类问题,就是数据层或三方接口返回的bean对象需要转换重新装换一下我们需要的对象。我们通常的做法就是通过getter/setter方法进行一个一个进行赋值,这样的话书写起来太复杂了,并且太重复了。我尝试写了一个工具类,能够对各种场景下的对象进行相互赋值。

功能介绍

  • 可以为将要赋值的对象进行单个单个的按顺序赋值
  • 通过传递的属性的index(就是他是第几个属性)获取本属性的值
  • 返回对象中属性的数量
  • 两个对象之间相互拷贝属性值
  • 传递一个list,遍历bean进行赋值
  • 传递一个数组,对对象进行赋值
  • 返回一个对象的属性值集合
  • 返回一个对象的属性值数组

注意注意注意!!!

getdeclaredfields方法不能保证字段声明的顺序进行返回,但是基本上会按照这个顺序的。所以以下的方法是建立在返回正确的顺序上的基础上的,但是两个对象相互拷贝是没有问题的。

import java.lang.reflect.field;
import java.util.arraylist;
import java.util.list;

/**
 * @author haoyan.shi
 * 想设计一个能够解析一个bean的全部属性并按照顺序进行遍历
 */
public class objectutils {

    /**
     * 按照属性的顺序赋值。可接受null,但是不能跳过某个属性进行赋值。就是说就算
     * 有一个值为空,那你也要传递进行null
     *
     * @param target
     * @param value
     * @param <e>
     * @return
     */
    public static <e> e foreachsetvalue(e target, object value) {
        if (target == null) {
            return target;
        }
        list<field> fields = new arraylist<>();
        try {
            // 1.解析出所有的属性
            field[] declaredfields = target.getclass().getdeclaredfields();
            for (field declaredfield : declaredfields) {
                declaredfield.setaccessible(true);
                fields.add(declaredfield);
            }
            // 2.把每个属性放入一个集合中
            if (fields.size() <= 0) {
                return target;
            }
            while (fields.get(0).get(target) != null) {
                fields.remove(0);
            }
            field field = fields.get(0);
            field.set(target, value);
            fields.remove(0);
        } catch (exception exception) {
            exception.printstacktrace();
        }
        return target;
    }

    /**
     * 本方法为了遍历索引进行赋值
     *
     * @param e
     * @param index
     * @return
     */
    public static object foreachgetvalue(object e, int index) {
        try {
            field[] declaredfields = e.getclass().getdeclaredfields();
            for (field declaredfield : declaredfields) {
                declaredfield.setaccessible(true);
            }
            return declaredfields[index].get(e);
        } catch (illegalaccessexception illegalaccessexception) {
            illegalaccessexception.printstacktrace();
        }
        return e;
    }

    public static int size(object o) {
        if (o == null) {
            return 0;
        }
        field[] declaredfields = o.getclass().getdeclaredfields();
        return declaredfields.length;
    }

    /**
     * 本方法是为了把已经有值得对象中属性名相同的名属性赋值到没有值得对象用。
     *
     * @param target
     * @param value
     */
    public static <e> e copyvaluefromobject(e target, object value) {
        if (target == null || value == null) {
            return null;
        }
        field[] vs = target.getclass().getdeclaredfields();
        field[] ts = value.getclass().getdeclaredfields();

        try {
            for (int i = 0; i < vs.length; i++) {
                for (int j = 0; j < ts.length; j++) {
                    if (vs[i].getname().equals(ts[j])) {
                        ts[j].set(target, vs[i].get(value));
                    }
                }
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return target;
    }

    /**
     * 这个方法能把list中的值按照顺序设置到目标对象中
     *
     * @param target
     * @param value
     * @param <e>
     * @return
     */
    public static <e> e foreachsetvaluefromlist(e target, list value) {

        if (target == null || value == null || value.size() == 0) {
            return target;
        }
        field[] ts = target.getclass().getdeclaredfields();
        try {
            for (int i = 0; i < ts.length; i++) {
                ts[i].set(target, value.get(i));
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return target;
    }

    /**
     * 从数组中进行设置值
     *
     * @param target
     * @param value
     * @param <e>
     * @return
     */
    public static <e> e foreachsetvaluefromarray(e target, object[] value) {

        if (target == null || value == null || value.length == 0) {
            return target;
        }
        field[] ts = target.getclass().getdeclaredfields();
        try {
            for (int i = 0; i < ts.length; i++) {
                ts[i].set(target, value[i]);
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return target;
    }


    public static object[] getarrayvalue(object o) {
        field[] declaredfields = o.getclass().getdeclaredfields();
        object[] result = new object[declaredfields.length];
        try {
            for (int i = 0; i < declaredfields.length; i++) {
                result[i] = declaredfields[i].get(o);
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }

    public static list getlistvalue(object o) {
        field[] declaredfields = o.getclass().getdeclaredfields();
        list result = new arraylist(declaredfields.length);
        try {
            for (int i = 0; i < declaredfields.length; i++) {
                result.add(declaredfields[i].get(o));
            }
        } catch (exception e) {
            e.printstacktrace();
        }
        return result;
    }
}

后期扩展:

1.我们可以定义一些注解,进行属性匹配。注入值更精确。
2.还可以用jdk1.8中的函数接口,进行赋值。
3.甚至都可以作为jdk的新特性去扩展这个功能。

到此这篇关于java循环对bean的属性进行赋值的文章就介绍到这了,更多相关javabean属性赋值内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!