java 枚举转实体List

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;
    }
}

 

转载于:https://www.cnblogs.com/wang-yi/p/9927421.html

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页