【代码生成扩展】如何用反射遍历枚举类型?

public class TestGetEnum {

 

    public void printEnumValues(Class c) {

        if (c.isEnum()) {

            try {

                Object[] objs = c.getEnumConstants();

                for (Object obj : objs) {

                    Method m = obj.getClass().getDeclaredMethod("values", null);

                    Object[] result = (Object[]) m.invoke(obj, null);

                    System.out.println(result);

                    List list = Arrays.asList(result);

                    Iterator it = list.iterator();

                    while (it.hasNext()) {

                        Object objOne = it.next();

                        Field code = objOne.getClass().getDeclaredField("code");

                        Field codeDesc = objOne.getClass().getDeclaredField("codeDesc");

                        Field priority = null;

                        try {

                            priority = objOne.getClass().getDeclaredField("priority");

                        } catch (Exception e) {

                            priority = null;

                        }

                        code.setAccessible(true);

                        codeDesc.setAccessible(true);

                        if (priority != null) {

                            priority.setAccessible(true);

                        }

                        System.out.print(code.getName() + ":" + code.get(objOne) + "," + codeDesc.getName() + ":"

                                + codeDesc.get(objOne));

                        if (priority != null) {

                            System.out.print("," + priority.getName() + ":" + priority.get(objOne));

                        }

                        System.out.println();

 

                    }

                    break;

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

 

    public static void main(String[] args) {

 

        TestGetEnum t = new TestGetEnum();

        t.printEnumValues(EducationLevelEnum.class);

        

    }

 

}

 

 

 

public enum EducationLevelEnum {

    DEFAULT("0","------",0),

    HIGHSCHOOLDIPLOMA("1","High School Diploma",100),

    ASSOCIATEDEGREE("5","Associate Degree",150),

    BACHELORSDEGREE("2","Bachelors Degree",200),

    MASTERSDEGREE("3","Masters Degree",300),

    DOCTORATE("4","Doctorate",400);

    EducationLevelEnum(String code, String codeDesc, Integer priority) {

        this.code = code;

        this.codeDesc = codeDesc;

        this.priority = priority;

    }

 

    private String code;

    private String codeDesc;

    private String kind=KindEnum.EDUCATIONLEVEL.getKindName();

 

    EducationLevelEnum(String code, String codeDesc) {

        this.code = code;

        this.codeDesc = codeDesc;

    }

 

    private Integer priority;

    public Integer getPriority() {

        return priority;

    }

    

    public String getCode() {

        return code;

    }

 

    public String getCodeDesc() {

        return codeDesc;

    }

 

    public String getKind() {

        return kind;

    }

 

}

 来自:

http://www.verydemo.com/demo_c89_i128540.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值