Files
Mediation-Backend/ruoyi-common/src/main/java/com/ruoyi/common/interfaces/EnumsInterface.java
T
2024-01-05 15:34:00 +08:00

39 lines
1.1 KiB
Java

package com.ruoyi.common.interfaces;
import java.lang.reflect.Method;
/**
* 枚举接口,反射出枚举code对应的文本 或 文本对应的code
*
* @Author wangqiong
* @Date 2023/01/5
* @Version V1.0
*/
public interface EnumsInterface {
static <T extends EnumsInterface>String getText(Class<T> clazz, String code){
try {
Method method = clazz.getMethod("getTextByCode", String.class);
if(method!=null){
return (String)method.invoke(null,code);
}
}catch (Exception e){
System.err.println("getTextByCode方法不存在");
e.printStackTrace();
}
return code;
}
static <T extends EnumsInterface>String getCode(Class<T> clazz,String text){
try {
Method method = clazz.getMethod("getCodeByText", String.class);
if(method!=null){
return (String)method.invoke(null,text);
}
}catch (Exception e){
System.err.println("getCodeByText方法不存在");
e.printStackTrace();
}
return text;
}
}