通过反射获取枚举

来源:互联网 发布:有哪些是三级域名 编辑:程序博客网 时间:2024/04/29 09:43
public enum EnumSmsType {not_exits(-1,"不存在的"),alert_login_pwd(0,"修改登录密码"),alert_auth(1,"认证信息修改"),alert_email(2,"修改邮箱"),set_operate_pwd(3,"设置操作密码"),register_code(4,"注册验证码"),change_phone(5,"更改手机号"),door_secret(6,"门禁短信"),door_secret_forget(7,"门禁密保重置");private int value;private String desc;private EnumSmsType(int value,String desc) {this.value = value;this.desc=desc;}public int getValue() {return value;}public String getDesc() {return desc;}public static EnumSmsType setValue(int str) {EnumSmsType[] list=EnumSmsType.values();for(EnumSmsType enums:list){if(enums.getValue()==str){return enums;}}return not_exits;}}
package com.poobo.common.Enums;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.LinkedHashMap;import java.util.Map;import com.alibaba.fastjson.JSONObject;import com.poobo.sms.EnumSmsType;public class EnumsUtils {/** * 获取枚举列表 * @author 2017年1月5日上午10:32:11 * @param em * @return */public static Map<Integer, String> toCodeDescriptionMap(Enum em) {         Class clazz = em.getDeclaringClass();         LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();         try {             Method toName = clazz.getMethod("getDesc");             Method toCode = clazz.getMethod("getValue");             //得到enum的所有实例             Object[] objs = clazz.getEnumConstants();             for (Object obj : objs) {                 map.put((Integer)toCode.invoke(obj),(String)toName.invoke(obj));             }             return map ;        } catch (NoSuchMethodException e) {             e.printStackTrace();         } catch (InvocationTargetException e) {             e.printStackTrace();         } catch (IllegalAccessException e) {             e.printStackTrace();         }                 return null;    } /** * 获取所有值和描述 * @author 2017年1月5日上午10:35:58 * @param em * @return */public static String getAllDesc(Enum em){ StringBuffer sb=new StringBuffer();        Class clazz = em.getDeclaringClass();         LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();         try {             Method toName = clazz.getMethod("getDesc");             Method toCode = clazz.getMethod("getValue");             //得到enum的所有实例             Object[] objs = clazz.getEnumConstants();             for (Object obj : objs) {                 map.put((Integer)toCode.invoke(obj),(String)toName.invoke(obj));                 sb.append(toCode.invoke(obj)+":"+toName.invoke(obj)+",");            }         } catch (NoSuchMethodException e) {             e.printStackTrace();         } catch (InvocationTargetException e) {             e.printStackTrace();         } catch (IllegalAccessException e) {             e.printStackTrace();         }                 return sb.toString();    }public static void main(String[] args) {System.out.println(JSONObject.toJSONString(EnumsUtils.toCodeDescriptionMap(EnumSmsType.alert_auth)));System.out.println(EnumsUtils.getAllDesc(EnumSmsType.alert_auth));}}

0 0
原创粉丝点击