fix
Helius
2021-12-02 1bbb94e533d60b11687316eec2413be2d385ed5b
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
package com.matrix.core.enums;
 
import com.matrix.core.tools.LogUtil;
import com.matrix.system.hive.hievEnum.SmsPlatformEnum;
import com.matrix.system.hive.hievEnum.SmsTypeEnum;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
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;
 
/**
 * TODO 这里后期改成自动扫描包下面的特定注解,而不是硬编码注册
 */
public class EnumsManager {
    private static final List<String> CLASS_NAME = new ArrayList<>();
    private static ClassLoader classLoader;
    private static final String SUFFIX = ".class";
 
    static Map<String,List<EnumsShowVo>> showAbleMap =new HashMap<>();
 
    static {
//        showAbleMap.put(SmsPlatformEnum.values()[0].getEnumCode(),SmsPlatformEnum.values()[0].getEnumsShowVos());
//        showAbleMap.put(SmsTypeEnum.values()[0].getEnumCode(),SmsTypeEnum.values()[0].getEnumsShowVos());
 
        initEnum("com.matrix");
    }
 
    public static List<EnumsShowVo> getShowEnum(String emumCode){
        return showAbleMap.get(emumCode);
    }
 
 
    private static 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 static 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 static 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 static 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 (!ApiShowAble.class.isAssignableFrom(clazz)) {
                continue;
            }
 
            Object[] constants = clazz.getEnumConstants();
            ApiShowAble apiShowAble = (ApiShowAble) constants[0];
 
            showAbleMap.put(apiShowAble.getEnumCode(), apiShowAble.getEnumsShowVos());
        }
    }
 
}