package com.wang;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 获取枚举值的code和description
*/
public class Test06_Enums {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String[] classNames = new String[]{"com.wang.enums.FilterEnum", "com.wang.enums.SexEnum"};
for (String className : classNames) {
getEnumDetail(className);
System.out.println();
}
}
private static void getEnumDetail(String className) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class<?> forName = Class.forName(className);
Method method = forName.getMethod("values");
Object[] values = (Object[]) method.invoke(null);
for (Object value : values) {
System.out.print("value = " + value + "----");
System.out.println(forName.getMethod("getCode").invoke(value).toString()
+ "----------" +
forName.getMethod("getDescription").invoke(value).toString());
}
}
}
输出结果