935090232@qq.com
2022-04-20 281dc31c4a4d4e2cf266dd65a470c7d29e4e7332
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.matrix.core.enums;
 
 
import com.matrix.core.tools.LogUtil;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
 
 
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class EnumsManager implements ApplicationRunner {
    private final List<String> CLASS_NAME = new ArrayList<>();
    private ClassLoader classLoader;
    private static final String SUFFIX = ".class";
 
    private Map<String, List<EnumsShowVo>> showAbleMap = new HashMap<>();
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        LogUtil.info("扫描自定义枚举------------------");
        initEnum("com.matrix");
        LogUtil.info("扫描自定义枚举结束===============");
    }
 
    public List<EnumsShowVo> getShowEnum(String emumCode) {
        return showAbleMap.get(emumCode);
    }
 
 
    private void initEnum(String... packages) {
        classLoader = Thread.currentThread().getContextClassLoader();
 
        for (String basePackage : packages) {
            Enumeration<URL> resources = null;
            try {
                resources = classLoader.getResources(basePackage.replaceAll("\\.", "/"));
            } catch (IOException e) {
                return;
            }
 
            // 扫描当前工程和jar包
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                if ("file".equals(url.getProtocol())) {
                    doFileScan(url.getPath());
                } else if ("jar".equals(url.getProtocol())) {
                    doJarScan(basePackage, url);
                }
            }
        }
 
        // 初始化枚举数据
        enumValue();
    }
 
    /**
     * 扫描当前工程对应包下的所有类
     *
     * @param path
     */
    private void doFileScan(String path) {
        String rootPath = classLoader.getResource("").getPath();
        File pathFile = new File(path);
 
        File[] files = pathFile.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
 
        for (File file : files) {
            if (file.isDirectory()) {
                String nextPath = path + "/" + file.getName();
                doFileScan(nextPath);
            } else if (file.getName().endsWith(SUFFIX)) {
                if (!path.contains(rootPath)) {
                    return;
                }
                String subStr = path.substring(rootPath.length());
                String className = (subStr + "/" + file.getName().replaceAll(SUFFIX, "")).replaceAll("/", "\\.");
                CLASS_NAME.add(className);
            }
        }
    }
 
    /**
     * 扫描jar包下对应包下所有类
     *
     * @param basePackage
     * @param baseURL
     */
    public void doJarScan(String basePackage, URL baseURL) {
        basePackage = basePackage.replaceAll("\\.", "/");
        JarFile jarFile;
        try {
            URLConnection urlConnection = baseURL.openConnection();
            JarURLConnection jarUrl = (JarURLConnection) urlConnection;
            jarFile = jarUrl.getJarFile();
        } catch (IOException e) {
            throw new RuntimeException("未找到资源");
        }
 
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
 
            String name = entry.getName();
            if (name.startsWith(basePackage)) {
                if (name.endsWith(SUFFIX)) {
                    String className = name.replaceAll(SUFFIX, "").replaceAll("/", "\\.");
                    CLASS_NAME.add(className);
                }
            }
        }
    }
 
    public void enumValue() {
        for (String className : CLASS_NAME) {
            Class<?> clazz = null;
            try {
                clazz = classLoader.loadClass(className);
            } catch (ClassNotFoundException e) {
                continue;
            }
 
            // 判断类是否为枚举类型
            if (!clazz.isEnum()) {
                continue;
            }
 
            // 判断ApiShowAble是否为类的父类
            if (!EnumApiShowAble.class.isAssignableFrom(clazz)) {
                continue;
            }
 
            Object[] constants = clazz.getEnumConstants();
            EnumApiShowAble enumApiShowAble = (EnumApiShowAble) constants[0];
 
            showAbleMap.put(enumApiShowAble.getEnumCode(), enumApiShowAble.getEnumsShowVos());
        }
    }
 
 
}