标签:blog io ar 使用 java for strong sp 2014
package com.util; import java.util.LinkedHashMap; import java.util.Map; /** * <strong>功能:</strong>枚举使用工具 <strong>作者:</strong>Gary Huang <strong>日期:</strong> * 2014-3-5 <strong>版权:<strong>版权所有(C) 2014,QQ 834865081 */ public class EnumUtil { public static void main(String[] args) throws Exception { System.out.println(parseEnum("enum:com.util.Task1.Yes")); } public static Map<Integer, String> parseEnum(String clazz) throws Exception { if(clazz.startsWith("enum:")){ clazz = clazz.replace("enum:", "" ); }else{ return null ; } Class<?> ref = forName(clazz) ; if(null == ref){ throw new ClassNotFoundException(String.format("%s class not fond ", clazz)); } Map<Integer, String> map = new LinkedHashMap<Integer, String>(); if (ref.isEnum()) { Object[] ts = ref.getEnumConstants(); for (Object t : ts) { Enum<?> e = (Enum<?>) t; map.put(e.ordinal(), e.name()); } } return map; } static Class<?> forName(String className) throws Exception{ String[]packageNames = className.split("\\.") ; String tempClassName = "" ; Class<?> returnClazz = null ; for(String packageName : packageNames){ tempClassName += packageName ; if(fristCharIsUpper(packageName)){ if( null == returnClazz){ try{ returnClazz = Class.forName( tempClassName); }catch(Exception e){} }else { Class<?> classes[]=returnClazz.getDeclaredClasses(); for(Class<?> tempClazz : classes){ if(tempClazz.getSimpleName().equals(packageName)){ returnClazz = tempClazz ; } } } } tempClassName += "." ; } return returnClazz ; } static boolean fristCharIsUpper(String name){ if(null != name && name.length() >0){ char[]chars = name.toCharArray() ; int intChar = (int)chars[0] ; return intChar <= 90 && intChar >= 65 ; } return false ; } }
标签:blog io ar 使用 java for strong sp 2014
原文地址:http://blog.csdn.net/hfmbook/article/details/40301327