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

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;

    }

 

}

你可能感兴趣的:(Java 反射取出Enum所有常量的属性值)