反射获取对象中的list数据

时间:2025-04-02 07:04:51

工具类:

package ;

import ;
import ;
import ;
import ;

import .*;
import ;
import ;
import ;
import ;

/**
 * @class_name: ReFlectUtil
 * @description:
 * @author: wm_yu
 * @create: 2019/07/19
 **/
public class ReFlectUtil {

    /**
     *反射获取List中的数据
     * @param args
     */

    public static void main(String[] args) {
        Teacher teacher = new Teacher();
       List<Student> list = new ArrayList<Student>();
        (new Student("张三", "男"));
        (new Student("李四", "女"));
        (new Student("王五", "女"));
       (list);
        getList(teacher,);
    }


    /**
     * 反射获取对象中的list数据
     * @param object
     * @param dateClass
     * @param <T>
     */
    public static<T> void getList(Object object,T dateClass){
       List<T> resultList = new ArrayList<>();
        if(!(object)){

            Field[] fields = getAllFields(());
            Field[] filterList= filterField(fields);
            (filterList).forEach(var -> {
                //List集合
                if((())){
                    Type type = ();
                    if(type instanceof ParameterizedType){
                        if(!()){
                            (true);
                        }
                        //获取到属性值的字节码
                        try {
                            Class<?> clzz = (object).getClass();
                            //反射调用获取到list的size方法来获取到集合的大小
                            Method sizeMethod = ("size");
                            if(!()){
                                (true);
                            }
                            //集合长度
                            int size = (int) ((object));
                            //循环遍历获取到数据
                            for (int i = 0; i < size; i++) {
                                //反射获取到list的get方法
                                Method getMethod = ("get", );
                                //调用get方法获取数据
                                if(!()){
                                    (true);
                                }
                                T var1 = (T) ((object), i);
                                (var1);
                            }

                        } catch (Exception e) {
                            ();
                        }
                    }
                }
            });

            ().forEach(var -> {
                ("反射获取到的数据是什么:" + var);
            });
        }
    }


    /**
     * 反射获取所有的字段
     * @param c
     * @return
     */
    public static Field[] getAllFields(Class c){
        List<Field> fieldList = new ArrayList<>();
        while (c!= null){
            (new ArrayList<>((())));
            c= ();
        }
        Field[] fields = new Field[()];
        (fields);
        return fields;
    }





    /**
     * 过滤字段
     * @param
     * @return
     */
    public static Field[] filterField(Field[] fields){
        List<Field> tempList = (fields).filter(field -> null != field
                && !(())
                && !(())
                && !(())).collect(());


        int arrLength = (tempList) ? 1:();

        Field[] resultArr = new Field[arrLength];
        if(!(tempList)){
            (resultArr);
        }
        return resultArr;
    }



}

实体类:

package ;

/**
 * @class_name: Student
 * @description:
 * @author: wm_yu
 * @create: 2019/07/19
 **/
public class Student {

    public Student(String name, String gender) {
         = name;
         = gender;
    }

    private String name;

    private String gender;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
         = gender;
    }


    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}
package ;

import ;

/**
 * @class_name: Teacher
 * @description:
 * @author: wm_yu
 * @create: 2019/07/19
 **/
public class Teacher {


    List<Student> list;


    public List<Student> getList() {
        return list;
    }

    public void setList(List<Student> list) {
         = list;
    }
}