public class EnumBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String ENUM_CLASS = "java.lang.Enum";
private int id;
private String name;
private String value;
public static List<EnumBean> enumToBeanList(Class clz) throws Throwable {
if (!"java.lang.Enum".equalsIgnoreCase(clz.getSuperclass().getName())) {
throw new IllegalArgumentException(
"The argument [ " + clz.getName() + " ] can not be asssigned by " + "java.lang.Enum");
} else {
ArrayList list = new ArrayList();
EnumBean bean = null;
Enum[] enums = null;
String value = null;
Method m = clz.getDeclaredMethod("values", (Class[]) null);
enums = (Enum[]) ((Enum[]) m.invoke((Object) null, (Object[]) null));
Enum[] arg5 = enums;
int arg6 = enums.length;
for (int arg7 = 0; arg7 < arg6; ++arg7) {
Enum e = arg5[arg7];
value = Enum.valueOf(clz, e.name()).toString();
bean = new EnumBean();
bean.setName(e.name());
if (value.contains(":")) {
bean.setId(Integer.parseInt(value.substring(0, value.indexOf(":"))));
bean.setValue(value.substring(value.indexOf(":") + 1));
} else {
bean.setId(e.ordinal());
bean.setValue(e.name());
}
list.add(bean);
}
return list;
}
}
public int getId() {
return this.id;
}
private void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
private void setName(String name) {
this.name = name;
}
public String getValue() {
return this.value;
}
private void setValue(String value) {
this.value = value;
}
}
java 枚举转实体List
点赞
收藏