利用反射,通过key获取value(所有枚举类)

public class EnumConvertTools {

    private EnumConvertTools() {
        throw new IllegalStateException("Utility class");
    }
     /**
      * @description:通用的枚举转换的工具类
      * <p>
      *     通过传入的参数,获取枚举的信息
      *     clazz 为对象的class对象
      *     convertType 为枚举的信息
      *     value 为比对的Key值
      * </p>
      * @Date: 2022/12/4 23:24
      * @jdk: 1.8
      */
    public static <T> String getEnumNameOrCodeByConvert(Class<T> clazz, String convertType, String value) {
        if (StringUtils.isNotBlank(value)) {
            //Enum接口中没有values()方法,我们仍然可以通过Class对象取得所有的enum实例
            T[] arr = clazz.getEnumConstants();
            //通过类型进行判定
            if (EnumConvertEnum.ConvertTypeEnum.CODE_TO_NAME.getCode().equals(convertType)) {
                //通过code获取name
                return getNameByCode(clazz, arr, value);
            }else if (EnumConvertEnum.ConvertTypeEnum.NAME_TO_CODE.getCode().equals(convertType)) {
                //通过name获取code
                return getCodeByName(clazz, arr, value);
            }else {
                throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"枚举的此种转换暂时不支持!");
            }
        }
        return null;
    }

     /**
      * @description:通过name获取code
      * @Date: 2022/12/5 8:58
      * @jdk: 1.8
      */
    private static  <T> String getCodeByName(Class<T> clazz, T[] arr, String value) {
        try {
            //获取执行的方法名称
            Method getCode = clazz.getDeclaredMethod("getCode");
            //获取getName的方法
            Method methodByConvert = getMethodByConvert(clazz);
            //定义获取到的名称
            for (T entity : arr) {
                //获取名称
                String name = methodByConvert.invoke(entity).toString();
                if (value.equals(name)) {
                    return getCode.invoke(entity).toString();
                }
            }
        }catch (Exception e) {
            throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"枚举方法获取失败!");
        }
        return null;
    }

     /**
      * @description:通过code获取name
      * @Date: 2022/12/4 23:31
      * @jdk: 1.8
      */
    private static <T> String getNameByCode(Class<T> clazz, T[] arr, String value) {
        try {
            //获取执行的方法名称
            Method getCode = clazz.getDeclaredMethod("getCode");
            //获取getName的方法
            Method methodByConvert = getMethodByConvert(clazz);
            for (T entity : arr) {
                //获取传过来方法的
                String typeCodeVal = getCode.invoke(entity).toString();
                if (typeCodeVal.equals(value)) {
                    return methodByConvert.invoke(entity).toString();
                }
            }
        }catch (Exception e) {
            throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"枚举方法获取失败!");
        }
        return null;
    }

     /**
      * @description:公共的获取method的方法
      * @Date: 2022/12/5 8:57
      * @jdk: 1.8
      */
    private static <T> Method getMethodByConvert(Class<T> clazz) {
        try {
            return clazz.getDeclaredMethod("getCnName");
        }catch (Exception e) {
            try {
                return clazz.getDeclaredMethod("getName");
            }catch (Exception ee) {
                throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"获取Name的方法异常,请联系管理员!");
            }
        }
    }

    /**
     * @description:通过枚举的方法获取到枚举的对象信息
     * <p>
     *     此方法通过传入枚举的方法与方法对应的Key做对比 返回匹配到的枚举对象
     * </p>
     * @Date: 2022/12/5 9:04
     * @jdk: 1.8
     */
    public static <T extends Enum<T>> T getEnumObjectByInvoke(Class<T> clazz, String convertMethodName
            , String value) {
        //判空可以自由发挥
        judgeFiledByEnumObject(value);
        //定义返回值
        T result = null;
        try {
            //Enum接口中没有values()方法,我们仍然可以通过Class对象取得所有的enum实例
            T[] arr = clazz.getEnumConstants();
            //获取定义的方法
            Method targetMethod = clazz.getDeclaredMethod(convertMethodName);
            String typeCodeVal;
            //遍历枚举定义
            for (T entity : arr) {
                //获取传过来方法的
                typeCodeVal = targetMethod.invoke(entity).toString();
                //执行的方法的值等于参数传过来要判断的值
                if (value.equals(typeCodeVal)) {
                    result = entity;
                    break;
                }
            }
        } catch (IllegalAccessException | SecurityException | IllegalArgumentException | NoSuchMethodException |
                 InvocationTargetException e) {
            throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"枚举的对象信息转换失败!");
        }
        return result;
    }

     /**
      * @description:判空可以自由发挥
      * @Date: 2022/12/5 9:06
      * @jdk: 1.8
      */
    private static void judgeFiledByEnumObject(String value) {
        if (StringUtils.isBlank(value)) {
            throw new YnetPlatformException(ErrorConstantPlatformVariable.SYS_THROW_INFO_CODE,"传入转换的参数不能为空!");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值