Java | Use reflection to convert attribute values in enumeration Enum

common wx: CodingTechWork, learn and progress together.

need

  During the development process, sometimes it is necessary to convert one field attribute value to another field attribute value in each enumeration class, such as performing front-end display according to the requirement of converting code to name. This article summarizes how to map enumeration attribute values ​​to each other simply and cleverly through reflection.

practice

enum class

package com.test.selfcoding;

/**
 * @Description TODO
 * @Author LiaoJy
 * @Date 2023/6/18
 */
public enum ColourEnum {
    
    

    /**
     * 红色
     */
    RED(1, "红色"),
    /**
     * 黄色
     */
    YELLOW(2, "黄色"),
    /**
     * 绿色
     */
    GREEN(3, "绿色");

    private Integer code;
    private String desc;

    ColourEnum(Integer code, String desc) {
    
    
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
    
    
        return code;
    }

    public String getDesc() {
    
    
        return desc;
    }

    public static String getDesc(Integer code) {
    
    
        for (ColourEnum c : ColourEnum.values()) {
    
    
            if (c.code.intValue() == code.intValue()) {
    
    
                return c.getDesc();
            }
        }
        return null;
    }

    public static Integer getCode(String desc) {
    
    
        for (ColourEnum c : ColourEnum.values()) {
    
    
            if (c.desc.equals(desc)) {
    
    
                return c.getCode();
            }
        }
        return null;
    }

    public static ColourEnum getEnum(Integer code) {
    
    
        if (code == null) {
    
    
            return null;
        }
        for (ColourEnum c : ColourEnum.values()) {
    
    
            if (c.code.intValue() == code.intValue()) {
    
    
                return c;
            }
        }
        return null;
    }

}

reflection test class


package com.test.selfcoding.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description
 * @Author LiaoJy
 * @Date 2023/6/18
 */
public class EnumReflectServiceTest {
    
    

    public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    
    
        String codeValue = "1";
        String descValue = "黄色";
        Class<Enum> clazz = (Class<Enum>) Class.forName("com.test.selfcoding.ColourEnum");
        if (Integer.class.equals(clazz.getDeclaredField("code").getType())) {
    
    
            Method method = clazz.getMethod("getDesc", Integer.class);
            String result = (String)  method.invoke(clazz, Integer.parseInt(codeValue));
            System.out.println("颜色描述:" + result);
        }

        if (String.class.equals(clazz.getDeclaredField("desc").getType())) {
    
    
            Method method = clazz.getMethod("getCode", String.class);
            Integer result = (Integer)  method.invoke(clazz, descValue);
            System.out.println("颜色编码:" + result);
        }
    }

}

operation result

颜色描述:红色
颜色编码:2

Guess you like

Origin blog.csdn.net/Andya_net/article/details/131415381