獲得當前工程下的所有列舉值的文件
1:Controller
@Controller @RequestMapping("/api/help") @Api("幫助中心") public class HelpDocController extends MasterBaseApiController { /** * 開啟測試文件 */ @RequestMapping(value = "jacka/index") public String open() { return "api_url"; } /** * 獲得當前工程的所有列舉值 */ @RequestMapping(value = "enums",method = RequestMethod.GET) @ResponseBody public Map<String,Object> enumsDoc() { return EnumUtil.getAllEnumsInPackage("com.yuntu.master.common.enums"); } }
2:EnuUtil
package com.yuntu.master.common.util; import com.google.common.collect.Maps; import org.reflections.Reflections; importjava.util.Arrays; import java.util.Map; import java.util.Set; /** * Created by jack on 5/15/16. */ public class EnumUtil { /** * 獲得指定包下面的列舉類 * * @param packageName * @return */ public static Set<Class<? extends Enum>> getClassesInPackage(String packageName) { Reflections reflections = newReflections(packageName); Set<Class<? extends Enum>> allClasses = reflections.getSubTypesOf(Enum.class); return allClasses; } /** * 獲得指定包下面的所有的列舉類的值 * * @param packageName * @return */ public static Map<String, Object> getAllEnumsInPackage(String packageName) { Reflections reflections = new Reflections(packageName); Set<Class<? extends Enum>> allClasses = reflections.getSubTypesOf(Enum.class); Map<String, Object> result = Maps.newLinkedHashMap(); allClasses.forEach(t -> { Map<String, Object> value = Maps.newLinkedHashMap(); Arrays.asList(t.getEnumConstants()).forEach(c -> { Map<String, Object> fieldValue = Maps.newLinkedHashMap(); Arrays.asList(c.getClass().getDeclaredFields()).stream() .filter(field -> (!field.isEnumConstant()) && !"$VALUES".equals(field.getName())) .forEach(field -> { field.setAccessible(true); try { fieldValue.put(field.getName(), field.get(c)); } catch (IllegalAccessException e) { e.printStackTrace(); } }); value.put(c.toString(), fieldValue); }); result.put(t.getCanonicalName(), value); }); return result; } public static void main(String[] args) { EnumUtil.getAllEnumsInPackage("com.yuntu.master.common.enums")
.forEach((k, v) -> System.out.println(k + "=" + v)); } }
3:pom
1)JDK1.8
2)新增org.reflections依賴
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.8</version> </dependency>
4:效果:
com.yuntu.master.common.enums.MasterUserCustomerStatusEnum={Underway={value=1, description=進行中}, Affirm={value=2, description=已成交}, NotBuy={value=3, description=戰敗}}
com.yuntu.master.common.enums.MasterUserLevelEnum={FirstMaster={value=1, description=firstMaster}, SecondMaster={value=2, description=secondMaster}, Individual={value=3, description=individual}}
com.yuntu.master.common.enums.MasterInventoryStatusEnum={LittleCar={value=0, description=三臺內}, HavingCar={value=1, description=充足}, NoCar={value=2, description=無庫存}}
com.yuntu.master.common.enums.MasterUserTeamReportMouthEnum={Three_Month_Ago={mouthValue=3, mouthName=過去三個月, sequence=1}, Six_Month_Ago={mouthValue=6, mouthName=過去六個月, sequence=2}, Nine_Month_Ago={mouthValue=12, mouthName=過去十二個月, sequence=3}}