驼峰参数、下划线["_"]参数,属性互传

 

 

package cc.ash;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;

public class Test {

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


        System.out.println(int.class == Integer.TYPE);

        Stu stu = new Stu();
        stu.setStuName("abc");
        stu.setAge(25);
        stu.setGender(5);


        Stu_ stu_ = new Stu_();
        transFormField(Stu.class, stu, Stu_.class, stu_);
//        System.out.println("stu_ = " + stu_);

        Object o = transFormField(stu, Stu_.class);
        Stu_ stu_2 = (Stu_) o;

        Stu_ stu_1 = new Stu_();
        transFormField(stu, stu_1);

//        System.out.println("stu_1 = " + stu_1);

    }





    /**
     * resource 相同字段名称(忽略下划线后)targetClass类型的实例
     * @param resource
     * @param targetClass
     * @param <R>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static final <R> Object transFormField(R resource, Class targetClass) throws IllegalAccessException, InstantiationException {

        Object instance = targetClass.newInstance();
        Object target = transFormField(resource, instance);

        return target;
    }


    /**
     * resource 相同字段名称(忽略下划线后)的值复制给target
     * @param resource
     * @param target
     * @param <R>
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static final <R,T> T transFormField(R resource, T target) throws IllegalAccessException, InstantiationException {

        Class<?> resourceClass = resource.getClass();
        Class<?> targetClass = target.getClass();

        transFormField(resourceClass, resource, targetClass, target);
        return target;
    }


    /**
     * 驼峰参数、下划线["_"]参数,属性互传
     * @param resourceClass
     * @param resourceInstance
     * @param targetClass
     * @param targetInstance
     * @param <R>
     * @param <T>
     * @throws IllegalAccessException
     */
    public static final <R, T> void transFormField(Class resourceClass, R resourceInstance, Class targetClass, T targetInstance) throws IllegalAccessException {

        Field[] rFields = andSuperField(resourceClass, resourceClass.getDeclaredFields());
        Field[] tFields = andSuperField(targetClass, targetClass.getDeclaredFields());

        for (int i = 0; i < rFields.length; i++) {
            Field rField = rFields[i];
            String rfName = rField.getName();
            if(StringUtils.indexOf(rfName, "_") != -1) {
                rfName = rfName.replace("_", "");
            }

            for (int j = 0; j < tFields.length; j++) {
                Field tField = tFields[j];
                String tfName = tField.getName();
                if(StringUtils.indexOf(tfName, "_") != -1) {
                    tfName = tfName.replace("_", "");
                }


                //去掉"_"后属性名称相等,可赋值便复制属性值
                if(StringUtils.equalsIgnoreCase(tfName, rfName)) {

                    rField.setAccessible(true);
                    Object val = rField.get(resourceInstance);
                    if(ObjectUtils.isEmpty(val)) continue;

                    Class<?> readClz = val.getClass();
                    Class<?> writeClz = rField.getType();

                    if(writeClz.isAssignableFrom(readClz) || writeClz.isPrimitive() || readClz.isPrimitive()) {
                        tField.setAccessible(true);
                        tField.set(targetInstance, val);
                    }
                }
            }
        }
    }

    /**
     * 递归合并所有超类的字段
     * @param clz
     * @param fields
     * @return
     */
    public static final Field[] andSuperField(Class clz, Field[] fields) {

        Class superclass = clz.getSuperclass();

        if(superclass != Object.class) {
            Field[] sFields = superclass.getDeclaredFields();
            fields = ArrayUtils.addAll(fields, sFields);
            andSuperField(superclass, fields);
        }

        return fields;
    }

}

class Person {

    private int gender;

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }
}

class Stu extends Person {
    private String stuName;
    private int age;

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getAge() {
        return age;
    }

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

class Stu_ extends Person {

    private int age_ = 12;
    private String stu_name;

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public int getAge_() {
        return age_;
    }

    public void setAge_(int age_) {
        this.age_ = age_;
    }
}
posted @ 2019-12-25 12:41  毁乐乖狂,自有诪张  阅读(500)  评论(0编辑  收藏  举报