Java 反射取出Enum所有常量的属性值

来源:互联网 发布:网络地理位置 编辑:程序博客网 时间:2024/06/06 02:25

publicclass TestGetEnum {

 

    publicvoid 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();

            }

        }

    }

 

    publicstaticvoid main(String[] args) {

 

        TestGetEnum t = new TestGetEnum();

        t.printEnumValues(EducationLevelEnum.class);

        

    }

 

}

 

 

 

publicenum 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() {

        returnpriority;

    }

    

    public String getCode() {

        returncode;

    }

 

    public String getCodeDesc() {

        returncodeDesc;

    }

 

    public String getKind() {

        returnkind;

    }

 

}

原创粉丝点击