标签:blog java 使用 os strong io for 2014
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.reflect.MethodUtils;
/**
*
* <strong>功能:</strong>枚举使用工具
* <strong>作者:</strong>Gary Huang
* <strong>日期:</strong> 2014-3-5
* <strong>版权:<strong>版权所有(C) 2014,QQ 834865081
*/
public class EumnUtil {
public static String getText(Class<?> ref , Object code){
return parseEumn(ref).get( TransformUtils.toString(code) ) ;
}
public static <T> Map<String, String> parseEumn(Class<T> ref){
Map<String, String> map = new LinkedHashMap<String, String>() ;
if(ref.isEnum()){
T[] ts = ref.getEnumConstants() ;
for(T t : ts){
String text = getInvokeValue(t, "getText") ;
Enum<?> tempEnum = (Enum<?>) t ;
if(text == null){
text = tempEnum.name() ;
}
String code = getInvokeValue(t, "getCode") ;
if(code == null){
code = TransformUtils.toString( tempEnum.ordinal() ) ;
}
map.put(code , text ) ;
}
}
return map ;
}
static <T> String getInvokeValue(T t , String methodName){
try {
Method method = MethodUtils.getAccessibleMethod( t.getClass() , methodName);
String text = TransformUtils.toString(method.invoke( t )) ;
return text ;
} catch (Exception e) {
return null ;
}
}
}
标签:blog java 使用 os strong io for 2014
原文地址:http://blog.csdn.net/hfmbook/article/details/38333083