common-module/utils/ClassUtils.java
cuijiawang ea58e018b4 init
2025-05-27 17:58:49 +08:00

59 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<String, Object> }
* @author zhaoyuanyuan
* @date 2024/06/24 17:26
*/
public static Map<String,Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String,Object> 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<Map<String, Object>> }
* @author zhaoyuanyuan
* @date 2024/06/24 17:27
*/
public static <T> List<Map<String, Object>> objectListToListMap(List<T> objectList) throws IllegalAccessException {
List<Map<String, Object>> resultList = new ArrayList<>();
if (objectList == null || objectList.isEmpty()){
return resultList;
}
for (T t : objectList) {
resultList.add(objectToMap(t));
}
return resultList;
}
}