@Test public void testGetEnumInformation(){ List<EnumDictionary> enumDictionaryList = EnumUtil.getEnumInformation(HttpCodeEnum.class, "code","message"); System.out.println(enumDictionaryList); }
public static <T extends EnumDictionary> List<T> getEnumInformation(Class<?> clazz, String codeField, String descField) { if (!clazz.isEnum()) { throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class."); } Class<Enum> enumClass = (Class<Enum>) clazz; Enum[] objects = enumClass.getEnumConstants(); String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); String descMethodName = "get" + StringUtil.firstToUpperCase(descField); List<T> enumDictionaryList = new ArrayList<>(); try { Method valueMethod = clazz.getMethod(valueMethodName); Method descMethod = clazz.getMethod(descMethodName); for (Enum enumType : objects) { Object val = valueMethod.invoke(enumType); Object desc = descMethod.invoke(enumType); EnumDictionary dataDict = new EnumDictionary(); String type = ClassUtil.getSimpleTypeName(val); dataDict.setType(type); dataDict.setDesc(String.valueOf(desc)); dataDict.setValue(String.valueOf(val)); String name = enumType.name(); int ordinal = enumType.ordinal(); dataDict.setName(name); dataDict.setOrdinal(ordinal); enumDictionaryList.add((T) dataDict); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } return enumDictionaryList; }
EnumUtil { public static <T extends EnumDictionary> List<T> getEnumInformation(Class<?> clazz, String codeField, String descField) { if (!clazz.isEnum()) { throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class."); } Class<Enum> enumClass = (Class<Enum>) clazz; Enum[] objects = enumClass.getEnumConstants(); String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); String descMethodName = "get" + StringUtil.firstToUpperCase(descField); List<T> enumDictionaryList = new ArrayList<>(); try { Method valueMethod = clazz.getMethod(valueMethodName); Method descMethod = clazz.getMethod(descMethodName); for (Enum enumType : objects) { Object val = valueMethod.invoke(enumType); Object desc = descMethod.invoke(enumType); EnumDictionary dataDict = new EnumDictionary(); String type = ClassUtil.getSimpleTypeName(val); dataDict.setType(type); dataDict.setDesc(String.valueOf(desc)); dataDict.setValue(String.valueOf(val)); String name = enumType.name(); int ordinal = enumType.ordinal(); dataDict.setName(name); dataDict.setOrdinal(ordinal); enumDictionaryList.add((T) dataDict); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } return enumDictionaryList; } }
EnumUtil { public static <T extends EnumDictionary> List<T> getEnumInformation(Class<?> clazz, String codeField, String descField) { if (!clazz.isEnum()) { throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class."); } Class<Enum> enumClass = (Class<Enum>) clazz; Enum[] objects = enumClass.getEnumConstants(); String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); String descMethodName = "get" + StringUtil.firstToUpperCase(descField); List<T> enumDictionaryList = new ArrayList<>(); try { Method valueMethod = clazz.getMethod(valueMethodName); Method descMethod = clazz.getMethod(descMethodName); for (Enum enumType : objects) { Object val = valueMethod.invoke(enumType); Object desc = descMethod.invoke(enumType); EnumDictionary dataDict = new EnumDictionary(); String type = ClassUtil.getSimpleTypeName(val); dataDict.setType(type); dataDict.setDesc(String.valueOf(desc)); dataDict.setValue(String.valueOf(val)); String name = enumType.name(); int ordinal = enumType.ordinal(); dataDict.setName(name); dataDict.setOrdinal(ordinal); enumDictionaryList.add((T) dataDict); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } return enumDictionaryList; }  }
EnumUtil { public static <T extends EnumDictionary> List<T> getEnumInformation(Class<?> clazz, String codeField, String descField) { if (!clazz.isEnum()) { throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class."); } Class<Enum> enumClass = (Class<Enum>) clazz; Enum[] objects = enumClass.getEnumConstants(); String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); String descMethodName = "get" + StringUtil.firstToUpperCase(descField); List<T> enumDictionaryList = new ArrayList<>(); try { Method valueMethod = clazz.getMethod(valueMethodName); Method descMethod = clazz.getMethod(descMethodName); for (Enum enumType : objects) { Object val = valueMethod.invoke(enumType); Object desc = descMethod.invoke(enumType); EnumDictionary dataDict = new EnumDictionary(); String type = ClassUtil.getSimpleTypeName(val); dataDict.setType(type); dataDict.setDesc(String.valueOf(desc)); dataDict.setValue(String.valueOf(val)); String name = enumType.name(); int ordinal = enumType.ordinal(); dataDict.setName(name); dataDict.setOrdinal(ordinal); enumDictionaryList.add((T) dataDict); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } return enumDictionaryList; }  static List<T> getEnumInformation(Class<?> clazz, String codeField, String descField); static Map<String, List<Map<String, Object>>> getEnumInformation(Class<?> clazz); }
EnumUtil { public static <T extends EnumDictionary> List<T> getEnumInformation(Class<?> clazz, String codeField, String descField) { if (!clazz.isEnum()) { throw new RuntimeException(clazz.getCanonicalName() + " is not an enum class."); } Class<Enum> enumClass = (Class<Enum>) clazz; Enum[] objects = enumClass.getEnumConstants(); String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); String descMethodName = "get" + StringUtil.firstToUpperCase(descField); List<T> enumDictionaryList = new ArrayList<>(); try { Method valueMethod = clazz.getMethod(valueMethodName); Method descMethod = clazz.getMethod(descMethodName); for (Enum enumType : objects) { Object val = valueMethod.invoke(enumType); Object desc = descMethod.invoke(enumType); EnumDictionary dataDict = new EnumDictionary(); String type = ClassUtil.getSimpleTypeName(val); dataDict.setType(type); dataDict.setDesc(String.valueOf(desc)); dataDict.setValue(String.valueOf(val)); String name = enumType.name(); int ordinal = enumType.ordinal(); dataDict.setName(name); dataDict.setOrdinal(ordinal); enumDictionaryList.add((T) dataDict); } } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); } return enumDictionaryList; }  static List<T> getEnumInformation(Class<?> clazz, String codeField, String descField); static Map<String, List<Map<String, Object>>> getEnumInformation(Class<?> clazz);  }