枚举类中新增方法
public Object searchName(){
return this.name == null ? this.name() : this.name;
}
public Object searchValue(){
return this.value == null ? this.name() : this.value;
}
public class GetEnumValue {
static Method searchValue=null;
static Method searchName=null;
static Map<String,Object> map=new HashMap<String, Object>();
/**
* 判断参数类型是否正确并给method对象赋值
* @param classObj
* @param object
* @return
*/
static String isOk(Class<?> classObj, Object object){
if(classObj==null)
return "参数1不能为null";
if(object==null)
return "参数2不能为null";
try {
searchValue = classObj.getMethod("searchValue");
try {
searchName = classObj.getMethod("searchName");
return null;
}catch (NoSuchMethodException e){
return "未找到searchName方法";
}
} catch (NoSuchMethodException e) {
return "未找到searchValue方法";
}
}
/**
* 通过name获取枚举对象的value
* @param classObj
* @param name
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static Map<String,Object> getValue(Class<?> classObj,Object name) throws InvocationTargetException, IllegalAccessException {
String isOkStr=isOk(classObj,name);
if(isOkStr!=null){
return returnMap(false,isOkStr);
}
Object returnObj=null;
//查询遍历对象的enum属性
Object[] objs = classObj.getEnumConstants();
if (objs==null)
return returnMap(false,"参数类中不包含枚举值");
for (Object obj :objs){
//匹配name
if(searchName.invoke(obj).equals(name)){
//获取value
returnObj=searchValue.invoke(obj);
break;
}
}
if(returnObj==null)
returnObj="枚举类中不包含该name";
return returnMap(true,returnObj);
}
/**
* 通过value获取枚举对象的name
* @param classObj
* @param value
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static Map<String,Object> getName(Class<?> classObj,Object value) throws InvocationTargetException, IllegalAccessException {
String isOkStr=isOk(classObj,value);
if(isOkStr!=null){
return returnMap(false,isOkStr);
}
Object returnObj=null;
//查询遍历对象的enum属性
Object[] objs = classObj.getEnumConstants();
if (objs==null)
return returnMap(false,"参数类中不包含枚举值");
for (Object obj :objs){
//匹配value
if(searchValue.invoke(obj).equals(value)){
//获取name
returnObj=searchName.invoke(obj);
break;
}
}
if(returnObj==null)
returnObj="枚举类中不包含该value";
return returnMap(true,returnObj);
}
private static Map<String,Object> returnMap(boolean flag,Object returnObj){
map.put("flag", flag);
map.put("obj",returnObj);
return map;
}
}
String fileName=null;
Map<String,Object> fileGet= GetEnumValue.getValue(TemplateName.class, "agency-number-template.xls");
if(fileGet.get("flag").equals(false)){
return null;
}else {
fileName=fileGet.get("obj").toString();
}
public enum TemplateName {
target("target-number-template.xls", "目标客户查询平台模板.xls"),
times("times-number-template.xls", "TIMES帐号模板.xls"),
agency("agency-number-template.xls", "业务代办系统帐号模板.xls"),
self("self-air-number-template.xlsx", "自建空充系统帐号模板.xlsx"),
remuneration("remuneration-number-template.xlsx", "酬金登记帐号模板.xlsx"),
ca("ca-number-template.xlsx", "CA帐号申请模板.xlsx"),
qd("qd-power-number-template.xlsx", "社会渠道系统批卡权限放开条件审批表模板.xlsx"),
node("reportNodeTemple.xlsx", "单行表头导入模板.xlsx");
private String name ;
private String value ;
private TemplateName( String name , String value ){
this.name = name ;
this.value = value ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Object searchName(){
return this.name == null ? this.name() : this.name;
}
public Object searchValue(){
return this.value == null ? this.name() : this.value;
}
}