java 根据值获取枚举对象
Posted 遥远2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 根据值获取枚举对象相关的知识,希望对你有一定的参考价值。
关键方法:
/** * 值映射为枚举 * * @param enumClass 枚举类 * @param value 枚举值 * @param method 取值方法 * @param <E> 对应枚举 * @return */ public static <E extends Enum<?>> E valueOf(Class<E> enumClass, Object value, Method method) { E[] es = enumClass.getEnumConstants(); for (E e : es) { Object evalue; try { method.setAccessible(true); evalue = method.invoke(e); } catch (IllegalAccessException | InvocationTargetException e1) { throw ExceptionUtils.mpe("Error: NoSuchMethod in {}. Cause:", e, enumClass.getName()); } if (value instanceof Number && evalue instanceof Number && new BigDecimal(String.valueOf(value)).compareTo(new BigDecimal(String.valueOf(evalue))) == 0) { return e; } if (Objects.equals(evalue, value)) { return e; } } return null; }
测试枚举类:
@Getter @NoArgsConstructor @AllArgsConstructor public enum StrEnum { TEST1("000","成功"), TEST2("999","失败"), TEST3("888","处理中"); private String value; private String text; } @Getter @NoArgsConstructor @AllArgsConstructor public enum IntEnum { TEST1(0,"成功"), TEST2(1,"失败"), TEST3(2,"处理中"); private int value; private String text; } @Getter @NoArgsConstructor @AllArgsConstructor public enum LongEnum { TEST1(0L,"成功"), TEST2(1L,"失败"), TEST3(2L,"处理中"); private Long value; private String text; } @Getter @NoArgsConstructor @AllArgsConstructor public enum ReEnum { //成功SUCCESS SUCCESS("000", "成功", "000000", ""), // 新增项目 FAIL700101("7001", "验证失败", "700101", "没有收到报文"), FAIL700102("7001", "验证失败", "700102", "传入参数不合法"); private String code; private String text; private String subCode; private String subText; }
调用:
@Test public void valueOf() throws NoSuchMethodException { IntEnum intEnum = EnumUtils.valueOf(IntEnum.class,2,IntEnum.class.getMethod("getValue")); System.out.println(intEnum); StrEnum strEnum = EnumUtils.valueOf(StrEnum.class,"999",StrEnum.class.getMethod("getValue")); System.out.println(strEnum.getText()); LongEnum longEnum = EnumUtils.valueOf(LongEnum.class,2,LongEnum.class.getMethod("getValue")); System.out.println(longEnum); ReEnum reEnum = EnumUtils.valueOf(ReEnum.class,"700102",ReEnum.class.getMethod("getSubCode")); System.out.println(reEnum.getSubText()); }
附完整:
1、EnumUtils.java
package com.gomepay.gb.utils.enums; import com.gomepay.gb.utils.ExceptionUtils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.Objects; /** * @author www.gomepay.com * @date 2020/1/19 */ public class EnumUtils { /** * 值映射为枚举 * * @param enumClass 枚举类 * @param value 枚举值 * @param method 取值方法 * @param <E> 对应枚举 * @return */ public static <E extends Enum<?>> E valueOf(Class<E> enumClass, Object value, Method method) { E[] es = enumClass.getEnumConstants(); for (E e : es) { Object evalue; try { method.setAccessible(true); evalue = method.invoke(e); } catch (IllegalAccessException | InvocationTargetException e1) { throw ExceptionUtils.mpe("Error: NoSuchMethod in {}. Cause:", e, enumClass.getName()); } if (value instanceof Number && evalue instanceof Number && new BigDecimal(String.valueOf(value)).compareTo(new BigDecimal(String.valueOf(evalue))) == 0) { return e; } if (Objects.equals(evalue, value)) { return e; } } return null; } /** * 根据value值获取enum对象 * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> E getEnumByValue(final Class<E> enumClass,Object value) { try { return valueOf(enumClass,value,enumClass.getMethod("getValue")); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } /** * 根据value值获取text * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> String getTextByValue(final Class<E> enumClass,Object value) { E e = getEnumByValue(enumClass,value); Object evalue; Method method = null; try { method = enumClass.getMethod("getText"); method.setAccessible(true); evalue = method.invoke(e); } catch (NoSuchMethodException |IllegalAccessException | InvocationTargetException e1) { throw ExceptionUtils.mpe("Error: NoSuchMethod in {}. Cause:", e, enumClass.getName()); } if (evalue != null) { return String.valueOf(evalue); } return null; } /** * 根据code值获取enum对象,如果code值相同,则获取第一个enum对象 * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> E getEnumByCode(final Class<E> enumClass,Object value) { try { return valueOf(enumClass,value,enumClass.getMethod("getCode")); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } /** * 根据code值获取text,如果code值相同,则获取第一个enum对象的text * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> String getTextByCode(final Class<E> enumClass,Object value) { E e = getEnumByCode(enumClass,value); Object evalue; Method method = null; try { method = enumClass.getMethod("getText"); method.setAccessible(true); evalue = method.invoke(e); } catch (NoSuchMethodException |IllegalAccessException | InvocationTargetException e1) { throw ExceptionUtils.mpe("Error: NoSuchMethod in {}. Cause:", e, enumClass.getName()); } if (evalue != null) { return String.valueOf(evalue); } return null; } /** * 根据code值获取enum对象 * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> E getEnumBySubCode(final Class<E> enumClass,Object value) { try { return valueOf(enumClass,value,enumClass.getMethod("getSubCode")); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } /** * 根据code值获取text,如果code值相同,则获取第一个enum对象的text * @param enumClass * @param value * @param <E> * @return */ public static <E extends Enum<E>> String getSubTextBySubCode(final Class<E> enumClass,Object value) { E e = getEnumBySubCode(enumClass,value); Object evalue; Method method = null; try { method = enumClass.getMethod("getSubText"); method.setAccessible(true); evalue = method.invoke(e); } catch (NoSuchMethodException |IllegalAccessException | InvocationTargetException e1) { throw ExceptionUtils.mpe("Error: NoSuchMethod in {}. Cause:", e, enumClass.getName()); } if (evalue != null) { return String.valueOf(evalue); } return null; } }
2、TestEnum3.java
View Code
---------------------------------------完毕。另附:hutoool操作枚举工具方法
package com.gomepay.enums; import cn.hutool.core.lang.Console; import cn.hutool.core.util.EnumUtil; import org.junit.Test; import java.util.List; import java.util.Map; /** * @author www.gomepay.com * @date 2020/1/19 */ public class TestEnum1 { //定义枚举 public enum TestEnum{ TEST1("type1"), TEST2("type2"), TEST3("type3"); private TestEnum(String type) { this.type = type; } private String type; public String getType() { return this.type; } } /** * 获取枚举类中所有枚举对象的name列表 */ @Test public void test1() { List<String> names = EnumUtil.getNames(TestEnum.class); //输出:[TEST1, TEST2, TEST3] Console.log(names); } /** * 获得枚举类中各枚举对象下指定字段的值 */ @Test public void test2() { List<Object> types = EnumUtil.getFieldValues(TestEnum.class, "type"); //输出:[type1, type2, type3] Console.log(types); } /** * 获取枚举字符串值和枚举对象的Map对应,使用LinkedHashMap保证有序,结果中键为枚举名,值为枚举对象 */ @Test public void test3() { Map<String,TestEnum> enumMap = EnumUtil.getEnumMap(TestEnum.class); TestEnum t = enumMap.get("TEST1"); //输出:TEST1对象 Console.log(t); } /** * 获得枚举名对应指定字段值的Map,键为枚举名,值为字段值 */ @Test public void test4() { Map<String, Object> enumMap = EnumUtil.getNameFieldMap(TestEnum.class, "type"); String type = (String)enumMap.get("TEST2"); //输出:type2 Console.log(type); } }
以上是关于java 根据值获取枚举对象的主要内容,如果未能解决你的问题,请参考以下文章