Java 利用反射与内省组装数据

2017-10-19  本文已影响0人  _npc_

/**
 * 描述:
 *
 * @author chen_q_i@163.com
 * 2017/10/19 : 9:26.
 * @version : 1.0
 */
public class Girl {



    @PrimaryKey
    private  long id;
    private  String name;
    private Integer age;
    private String address;
    private String tel;
    private long utime;
    private  long ctime;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public long getUtime() {
        return utime;
    }

    public void setUtime(long utime) {
        this.utime = utime;
    }

    public long getCtime() {
        return ctime;
    }

    public void setCtime(long ctime) {
        this.ctime = ctime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}



/**
 * 描述:
 *
 * @author chen_q_i@163.com
 * 2017/10/19 : 10:37.
 * @version : 1.0
 */
public class NewGirl {
    private String name;
    private long utime;
    private  long ctime;
    private long id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getUtime() {
        return utime;
    }

    public void setUtime(long utime) {
        this.utime = utime;
    }

    public long getCtime() {
        return ctime;
    }

    public void setCtime(long ctime) {
        this.ctime = ctime;
    }

    public long getId() {
        return id;
    }

    @Override
    public String toString() {
        return "NewGirl{" +
                "name='" + name + '\'' +
                ", utime=" + utime +
                ", ctime=" + ctime +
                ", id=" + id +
                '}';
    }

    public void setId(long id) {
        this.id = id;
    }
}

工具类


import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

/**
 * 描述:
 *
 * @author chen_q_i@163.com
 * 2017/10/19 : 15:02.
 * @version : 1.0
 */
public class BuildViewFromEntity {

    public static void main(String[] args) throws IllegalAccessException, IntrospectionException, InvocationTargetException, InstantiationException {

        Girl girl = new Girl();
        girl.setName("111111111111111111111");
        girl.setId(2323L);
        girl.setCtime(System.currentTimeMillis());
        girl.setUtime(System.currentTimeMillis() + 22323333);
        NewGirl newGirl = new NewGirl();
        voUtils(girl, newGirl);
        System.out.println(newGirl);
    }


    public static void voUtils(Object target, Object voObj) throws IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException {
        PropertyDescriptor[] pds = Introspector.getBeanInfo(target.getClass()).getPropertyDescriptors();
        PropertyDescriptor[] ps = Introspector.getBeanInfo(voObj.getClass()).getPropertyDescriptors();

        Arrays.stream(pds)
                .filter(pd -> !"class".equals(pd.getName()) && pd.getReadMethod() != null)
                .forEach(pd -> {
                    Object value = null;
                    try {
                        value = pd.getReadMethod().invoke(target);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    if (value != null) {
                        System.out.println(value);
                        Object finalValue = value;

                        Arrays.stream(ps)
                                .filter(p -> p.getName().equals(pd.getName()))
                                .forEach(p -> {
                                    try {
                                        p.getWriteMethod().invoke(voObj, finalValue);
                                    } catch (IllegalAccessException | InvocationTargetException e) {
                                        e.printStackTrace();
                                    }
                                });
                    }

                });


    }

}


image.png
上一篇 下一篇

猜你喜欢

热点阅读