一般的枚举类,我们只写对应的get set方法, 要拿取每个字段的值必须先确认枚举实例
这里运用枚举类的values方法,循环判断实例的值,获取到对应的实例
参照方法getEventStatus(int value)
import org.apache.commons.lang3.EnumUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public enum EventStatus {
LAUNCHED(0, "待处理","发起"),
ACCEPTED(1, "处理中","认领"),
REJECTED(2, "待验收","打回"),
TRANSFERRED(3, "处理中","转交"),
RESOLVED(4, "待验收","解决"),
CLOSED(5, "已关闭","关闭");
private int value;
private String description;//事件状态
private String operateType;//用户操作类型
EventStatus(int value, String description,String operateType) {
this.value = value;
this.description = description;
this.operateType=operateType;
}
public static String getValueName(int intValue) {
return getNameByValue(intValue,"description");
}
public static String getOperateName(int intValue) {
return getNameByValue(intValue,"operateType");
}
public static String getNameByValue(int intValue, String propertyName){
EventStatus[] arr$ = values();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
EventStatus t = arr$[i$];
if (intValue == t.getValue()) {
return String.valueOf(dynamicGet(t,propertyName));
}
}
return "";
}
private static Object dynamicGet(EventStatus t, String propertyName) {
try {
Class clazz=t.getClass();
// Field field = clazz.getDeclaredField(propertyName);
PropertyDescriptor pd = new PropertyDescriptor(propertyName,
clazz);
Method getMethod = pd.getReadMethod();//获得get方法
Object o = getMethod.invoke(t);//执行get方法返回一个Object
return o;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static EventStatus getEventStatus(int value){
EventStatus[] arr$=values();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
EventStatus status = arr$[i$];
if (status.getValue()==value) {
return status;
}
}
return null;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getOperateType() {
return operateType;
}
public void setOperateType(String operateType) {
this.operateType = operateType;
}
public static void main(String[] args) {
System.out.println(EventStatus.getEventStatus(0).getDescription());;
System.out.println(getNameByValue(0,"description"));
}
}