通过反射获取枚举

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值