1
zj
2025-09-17 59e827830f2866a82cf1714fe2941cb0bd380c0a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.ruoyi.im.util;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @description:
 * @create: 2024-08-19 18:29
 **/
public class ConverterUtil {
 
    public static <T, V> V convert(T pojo, Class<V> voClass) {
        try {
            V vo = voClass.newInstance();
            Field[] pojoFields = pojo.getClass().getDeclaredFields();
            Field[] voFields = voClass.getDeclaredFields();
            for (Field pojoField : pojoFields) {
                pojoField.setAccessible(true);
                for (Field voField : voFields) {
                    voField.setAccessible(true);
                    if (pojoField.getName().equals(voField.getName()) && pojoField.getType().equals(voField.getType())) {
                        voField.set(vo, pojoField.get(pojo));
                        break;
                    }
                }
            }
            return vo;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static <T, V> List<V> convertToList(List<T> pojoList, Class<V> voClass) {
        List<V> voList = new ArrayList<>();
        for (T pojo : pojoList) {
            V vo = convert(pojo, voClass);
            voList.add(vo);
        }
        return voList;
    }
 
 
}