6
jinlin
2023-12-06 da4cb0bcbb9c35105afe449b31b3e2b03828d5d2
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
package com.zt.core.dictionary;
 
import com.zt.common.annotation.Dictionary;
import com.zt.common.constant.Constant;
import com.zt.common.constant.IDictionary;
import com.zt.core.sys.dto.DictDto;
import com.zt.core.sys.dto.DictLeafDto;
import com.zt.modules.sys.dto.DictEnumItemDto;
import com.zt.core.sys.dto.DictItemDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@Component("dictionaryParser")
public class DictionaryParser {
 
    @Autowired
    private ResourceLoader resourceLoader;
 
    /**
     * 获取指定包下所有添加了执行注解的方法信息
     *
     * @return
     * @throws Exception
     */
    @Cacheable(value = Constant.Cache.SYS, key = "'dict:code'")
    public List<DictDto> getConstantDicts() {
        List<DictDto> dicts = new ArrayList<>();
        try {
            ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
            MetadataReaderFactory metaReader = new CachingMetadataReaderFactory(resourceLoader);
            Resource[] resources = resolver.getResources("classpath*:com/zt/**/*.class");
 
            for (Resource r : resources) {
                MetadataReader reader = metaReader.getMetadataReader(r);
                dicts.addAll(resolveClass(reader));
            }
        } catch (IOException e) {
        }
 
        return dicts;
    }
 
    private List<DictDto> resolveClass(MetadataReader reader) {
        List<DictDto> dicts = new ArrayList<>();
        // 获取注解元数据
        AnnotationMetadata metadata = reader.getAnnotationMetadata();
        // 获取类中Dictionary注解的value属性
        Map<String, Object> attributes = metadata.getAnnotationAttributes(Dictionary.class.getCanonicalName());
        if (attributes == null) {
            return dicts;
        }
        String dictType = (String) attributes.get("type"), dictName = (String) attributes.get("name");
        if (StringUtils.isBlank(dictType) || StringUtils.isBlank(dictName)) {
            return dicts;
        }
        DictLeafDto dictDto = new DictLeafDto(dictType, dictName);
 
        String className = reader.getClassMetadata().getClassName();
        try {
            List<DictItemDto> dataList = new ArrayList<>();
 
            Enum[] enums = (Enum[]) Class.forName(className).getEnumConstants();
            for (Enum anEnum : enums) {
                if (anEnum instanceof IDictionary) {
                    IDictionary dictionary = (IDictionary) anEnum;
                    dataList.add(new DictEnumItemDto(((Enum) dictionary).name(), dictionary.getName(), dictionary
                            .getValue()));
                }
            }
            dictDto.setDataList(dataList);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        dicts.add(dictDto);
        return dicts;
    }
 
}