java获取枚举类的所有属性
程序员文章站
2022-03-26 16:41:13
能做什么?:通过取项目某个包路径下的所有枚举类来获取该类的所有属性直接上java代码:package com.xxx.common.ennum;import cn.hutool.core.util.ClassUtil;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Set;/**...
能做什么?:通过取项目某个包路径下的所有枚举类来获取该类的所有属性
直接上java代码:
package com.xxx.common.ennum;
import cn.hutool.core.util.ClassUtil;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
/**
* @Desc 获取枚举类的所有属性
* @Author: cxl
* @Date: 2020/11/18 16:49
* @Version 1.0
*/
public class EnumUtils {
private static final String basePackage = "com.xxx.common.ennum";
public static HashMap<String, List<EnumDto>> getEnums() throws Exception {
Set<Class<?>> classes = ClassUtil.scanPackage(basePackage);
HashMap<String, List<EnumDto>> enums = new HashMap<>();
for (Class<?> aClass : classes) {
if (ClassUtil.isEnum(aClass)) {
Class<Enum> em = (Class<Enum>) aClass;
Method codeMethod = ClassUtil.getDeclaredMethod(aClass, "getCode");
Method descMethod = ClassUtil.getDeclaredMethod(aClass, "getDesc");
if (codeMethod == null || descMethod == null) continue;
//得到enum的所有实例
Object[] objs = em.getEnumConstants();
EnumDto dto;
List<EnumDto> list = new ArrayList<>();
for (Object obj : objs) {
dto = new EnumDto();
dto.setCode((String) codeMethod.invoke(obj));
dto.setDesc((String) descMethod.invoke(obj));
list.add(dto);
}
enums.put(aClass.getName().replace(basePackage + ".", "").replace("$","."), list);
}
}
return enums;
}
}
package com.xxx.common.ennum;
import lombok.Data;
/**
* @Author: cxl
* @Date: 2020/11/18 17:57
* @Version 1.0
*/
@Data
public class EnumDto {
private String code;
private String desc;
}
一个枚举类示例:
package com.xxx.common.ennum;
import com.xxx.modules.market.goods.ennum.MarketEnum;
/**
* @Author: cxl
* @Date: 2020/6/3 14:34
* @Version 1.0
*/
public interface BusCheckEnum {
enum CheckResult implements BusCheckEnum {
AGREE("agree", "同意"),
WAIT("wait","等待审核"),
REFUSE("refuse", "拒绝");
private String code;
private String desc;
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
CheckResult(String code, String desc) {
this.code = code;
this.desc = desc;
}
/**
* 根据code获取desc
* @return
*/
public static String getDescByCode(String code){
for(CheckResult platformFree:CheckResult.values()){
if(code.equals(platformFree.getCode())){
return platformFree.getDesc();
}
}
return null;
}
}
}
接口调用结果:
{
"code": 0,
"msg": "成功",
"data": {
"ResourceTypeEnum": [
{
"code": "all",
"desc": "全部"
},
{
"code": "expert",
"desc": "问答"
},
{
"code": "online",
"desc": "商城"
},
{
"code": "imgtext",
"desc": "图文"
}
],
"WxMsgTagEnum": [
{
"code": "newUserReg",
"desc": "新用户注册"
},
{
"code": "buySuccess",
"desc": "购买成功"
}
],
"BusCheckEnum.CheckResult": [
{
"code": "agree",
"desc": "同意"
},
{
"code": "wait",
"desc": "等待审核"
},
{
"code": "refuse",
"desc": "拒绝"
}
],
}
}
ClassUtil的来源是hutool工具包,下载地址:https://hutool.cn/docs/#/
在系统登录时调用该接口,获取所有枚举类集合存在本地缓存中,具体要用的时候直接传入类名来取即可
本文地址:https://blog.csdn.net/qq_40757662/article/details/109803954
上一篇: 设计模式:工厂模式
下一篇: Python 3.x 新特性及10大变化