JAva読み出し列挙クラスの値をlistとmapに変換


まずEnumクラス:
public enum RuleTypeEnum {  
  //officeClerk,estimator,administrator,personnel      key 
    officeClerk("officeClerk",99),  
    estimator("estimator",1),     
    administrator("administrator",2), 
    personnel("personnel",3);      

   private String reason;
    private int index;

    RuleTypeEnum(String reason, int index) {
        this.reason = reason;
        this.index = index;
    }


    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    } 
}

/以下はEnumツールクラス/
public class EnumUtils {
  private static Logger log= LoggerFactory.getLogger(EnumUtils.class); 
    /**
     *           Map
     * @param enumT    
     * @param methodNames      
     * @param 
     * @return
     */
    public static  Map EnumToMap(Class enumT,String... methodNames) {
        Map enummap = new HashMap();
        if (!enumT.isEnum()) { //    enum
            return enummap;
        }
        T enums []= enumT.getEnumConstants(); //         
        if (enums == null || enums.length <= 0) {
            return enummap;
        }
        int count = methodNames.length;
        /**    key  */
        String keyMathod = "getReason";
        /**    value  */
        String valueMathod = "getIndex";
         
        if (count >= 1 && !"".equals(methodNames[0])) {
            keyMathod = methodNames[0];
        }
        if (count == 2 && !"".equals(methodNames[1])) {
            valueMathod = methodNames[1];
        }
        for (int i = 0;i < enums.length; i++) {
            T tobj = enums[i];
            try {
                /**  key */
                Object resultkey = getMethodValue(keyMathod, tobj);
                if ("".equals(resultkey)) {
                    continue;
                }
                /**  value */
                Object resultValue = getMethodValue(valueMathod, tobj);
                /**           enum*/
                if ("".equals(resultValue)) {
                    resultValue = tobj;
                }
                enummap.put(resultkey+"", resultValue+"");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("     Map  ",e);
            }
        }
        return enummap;
    }

    private static  Object getMethodValue(String methodName, T obj, Object... args) {
        Object resut = "";
        try {
            /********************************* start *****************************************/
            /**      ,         */
            Method[] methods = obj.getClass().getMethods();
            if (methods.length <= 0) {
                return resut;
            }
            Method method = null;
            for (int i = 0, len = methods.length; i < len; i++) {
                /**        */
                if (methods[i].getName().equalsIgnoreCase(methodName)) {
                    /**    ,          */
                    methodName = methods[i].getName();
                    method = methods[i];
                    break;
                }
            }
            /*************************** end ***********************************************/
            if (method == null) {
                return resut;
            }
            /**    */
            resut = method.invoke(obj, args);
            if (resut == null) {
                resut = "";
            }
            /**    */
            return resut;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resut;
    }


    /***
     *      key   value   list
     * @param enumT    
     * @param methodNames        
     * @param 
     * @return
     */
    public static  List getEnumToList(Class enumT, String... methodNames) {
        List enumList = new ArrayList<>(); //     list  enumEntity          
        if (!enumT.isEnum()) {
            return enumList;
        }
        T enums[] = enumT.getEnumConstants();  //            
        if (enums.length == 0) {  //              
            return enumList;
        }
        int count = methodNames.length;
        String keyMethod = "getReason";  //     key   
        String valueMethod = "getIndex";  //     value    
        if (count >= 1 && !methodNames[0].equals("")) { //            1 ,     
            keyMethod = methodNames[0];
        }
        if (count == 2 && !methodNames[1].equals("")) { //          2 ,     
            valueMethod = methodNames[1];
        }
        try {
            for (int i = 0; i < enums.length; i++) {
                EnumEntity enumEntity = new EnumEntity();
                T object = enums[i];     //         
                Object resultKey = getMethodValue(keyMethod, object); //  key 
                if (resultKey.equals("")) {
                    continue;
                }
                Object resultValue = getMethodValue(valueMethod, object); //  value 
                if (resultValue.equals("")) {
                    resultValue = object;
                }
                //MessageUtils.getMessage       .
                enumEntity.setKey(MessageUtils.getMessage(resultKey.toString()));  // key     
                enumEntity.setValue(resultValue.toString()); // value     
                enumList.add(enumEntity);   //  list
            }
        } catch (Exception e) {
            e.getStackTrace();
            log.error("     List  ", e);
        }
        return enumList;
    }
}

//次は呼び出し方法
public List getRuleType(){Class clasz=RuleTypeEnum.class;List list=EnumUtils.getEnumToList(clasz);//デフォルトのメソッド名でgetReasonとgetIndex return list;}
/パーフェクト//