java 获取枚举数项中的值,获取枚举列表,获取枚举项对象

 java中常用的枚举方法有values和valueof

valueOf方法会把一个String类型的名称转变成枚举项,也就是在枚举项中查找字面值和该参数相等的枚举项。

 values是获取所有的枚举项

package HtppClient1.EnumUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.ReflectionUtils;

import com.sun.org.apache.bcel.internal.generic.IfInstruction;


enum TestEnums {
	weirenzheng(0, "0、未认证;", 16, -1, 0),
    onceTime(1, "上月支付1次及以下;", 8, 0, 1),
    towTimes(2, "上月支付1-2次", 12, 1, 2),
    threeTimes(3, "上月支付3-4次;", 16, 3, 4),
    fiveTimes(4, "上月支付5-7次", 20, 4, 7),
    sixTimes(5, "上月支付7次以上", 24, 7, Integer.MAX_VALUE);
    public int code;//排序
    public String desc;//描述
    public int score;//分值
    public int lower;//最低次数
    public int high;//最高次数

    TestEnums() {
    }

    TestEnums(int code, String desc, int score, int lower, int high) {
        this.code = code;
        this.desc = desc;
        this.score = score;
        this.lower = lower;
        this.high = high;
    }

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }



    public int getScore() {
        return score;
    }



    public int getLower() {
        return lower;
    }



    public int getHigh() {
        return high;
    }
    /**
     * 根据code返回枚举值
     * @param Code
     * @return
     */
    public static TestEnums   getEumByCode(int Code){
    	for(TestEnums testEnums: TestEnums.values()) {
    		if(testEnums.getCode()==Code) {
    			return testEnums;
    		}
    	}
    	return null;  	
    }

    /**
     * 根据序号获取枚举数组中的值,序号必须从0开始
     * @param key
     * @return
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static  List   getEumByKey(int Code) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    	List enumList=getEumValueList();
    	List enumList1=new ArrayList();
    	for(TestEnums testEnums: TestEnums.values()) {
    		if(testEnums.getCode()==Code) {
    			Class clazz=testEnums.getClass();
    			// 获取所有常量
    	        Object object = clazz.getEnumConstants()[Code];
        	    Field[]  filedFields=clazz.getFields();    	            	        	
				for (Field field : filedFields) {   				
					field.setAccessible(true);
					Object sssObject=field.get(object);
					if(enumList.contains(field.getName())) {
						continue;
					}else {
						if(sssObject!=null)
							enumList1.add((T) sssObject);
					}
				}	
    			return enumList1;
    		}
    	}
    	return null;
    }
    /**
     * 获取枚举值常量列表
     * @param Code
     * @return
     */
    public static List  getEumValueList() {
    		  List list=new ArrayList();
    		  for(Object object:TestEnums.values()) {
					list.add(object.toString());
    		  }
    		  return list;	
    }
    /**
     * 传入方法名称  values是值 ,field是 字段mingcheng
     * @param 
     * @param enumType
     * @param value
     * @param field
     * @return
     * @throws Exception
     */
    public static > T getEnumOnValue(Class enumType, String value, String field) throws Exception {

       for (Object obj : enumType.getEnumConstants()) {
          Method m = obj.getClass().getDeclaredMethod("values", null);
          Object[] results = (Object[]) m.invoke(obj, null);
          for (Object result : results) {
             Field codeField = result.getClass().getDeclaredField(field);
             ReflectionUtils.makeAccessible(codeField);
             String fileValue = String.valueOf(ReflectionUtils.getField(codeField, result));
             if (fileValue.equals(value)) {
                return (T) result;
             }

          }
       }
       return null;
    }
}

test代码:

import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{		 
    public static void main( String[] args ) throws Exception
    {
    	TestEnums testEnums = TestEnums.getEnumOnValue(TestEnums.class, "1", "code");
       List list=TestEnums.getEumByKey(1);
       for (Object string : list) {
		System.out.println(":"+string);
       }
    }
} 
  

 

你可能感兴趣的:(枚举值)