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; 
 | 
    } 
 | 
  
 | 
} 
 |