package com.sgcc.utils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Class反射工具类 * * @author zhaoyuanyuan * @version 1.0.0 * @date 2024/06/24 17:26 */ public class ClassUtils { /** * 对象转化为Map * * @param obj * @return {@code Map } * @author zhaoyuanyuan * @date 2024/06/24 17:26 */ public static Map objectToMap(Object obj) throws IllegalAccessException { Map map = new HashMap<>(); if (obj == null){ return map; } Field[] fields = obj.getClass().getDeclaredFields(); for(Field field:fields){ field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; } /** * 类型转化,实体对象列表转Map列表 * * @param objectList * @return {@code List> } * @author zhaoyuanyuan * @date 2024/06/24 17:27 */ public static List> objectListToListMap(List objectList) throws IllegalAccessException { List> resultList = new ArrayList<>(); if (objectList == null || objectList.isEmpty()){ return resultList; } for (T t : objectList) { resultList.add(objectToMap(t)); } return resultList; } }