此方法不推荐使用,可参考:枚举工具类,根据code获取枚举对象msg(多态)
1、定义一个CodeEnum接口,供每个枚举实现,以便重写共用方法
public interface CodeEnum {
Object getValue();
}
2、枚举类实现CodeEnum接口,并重写方法
import ·····.CodeEnum;
import lombok.Getter;
@Getter
public enum ProductStateEnum implements CodeEnum {
UP_PRODUCT(0,"已上架"),
DOWN_PRODUCT(1,"已下架")
;
private Integer code;
private String msg;
ProductStateEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
//根据code获取对象
@Override
public Object getValue() {
return code;
}
}
3、定义常量
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Constant {
/**
* 枚举类包名集合
*/
public static List<String> pathList = initPackagePathList();
/**
* 枚举接口类全路径
*/
//TODO
public final static String ENUM_MESSAGE_PATH = "com.chuai.ecshop.utils.enumUtils.CodeEnum";
/**
* 枚举类对应的全路径集合
*/
public static final List<String> ENUM_OBJECT_PATH = PackageUtil.getPackageClasses(pathList, true);
/**
* 存放单个枚举对象 map常量定义
*/
private static Map<Object, CodeEnum> SINGLE_ENUM_MAP = null;
/**
* 所有枚举对象的 map
*/
public static final Map<Class, Map<Object, CodeEnum>> ENUM_MAP = initialEnumMap(true);
//TODO
private static List<String> initPackagePathList() {
List<String> list = new ArrayList<>();
list.add("com.chuai.ecshop.myEnum");
return list;
}
/*static {
System.out.println("类被加载时,先初始化各个静态变量,再执行static块。" +
"所以不能在这里执行pathList的add操作(\"com.chuai.ecshop.myEnum\")。");
}*/
/**
* 加载所有枚举对象数据
*
* @param isFouceCheck 是否强制校验枚举是否实现了CodeEnum接口,若为false则没有实现接口的枚举类也会被加载
*/
private static Map<Class, Map<Object, CodeEnum>> initialEnumMap(boolean isFouceCheck) {
Map<Class, Map<Object, CodeEnum>> ENUM_MAP = new HashMap<>();
try {
for (String classname : ENUM_OBJECT_PATH) {
Class<?> cls = null;
cls = Class.forName(classname);
Class<?>[] iter = cls.getInterfaces();
boolean flag = false;
if (isFouceCheck) {
for (Class cz : iter) {
if (cz.getName().equals(ENUM_MESSAGE_PATH)) {
flag = true;
break;
}
}
}
if (flag == isFouceCheck) {
SINGLE_ENUM_MAP = new HashMap<>();
initialSingleEnumMap(cls);
ENUM_MAP.put(cls, SINGLE_ENUM_MAP);
}
}
} catch (Exception e) {
}
return ENUM_MAP;
}
/**
* 加载每个枚举对象数据
*/
private static void initialSingleEnumMap(Class<?> cls) throws Exception {
Method method = cls.getMethod("values");
CodeEnum inter[] = (CodeEnum[]) method.invoke(null, null);
for (CodeEnum codeEnum : inter) {
SINGLE_ENUM_MAP.put(codeEnum.getValue(), codeEnum);
}
}
}
4、获取全路径
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PackageUtil {
/**
* 返回包下所有的类
*
* @param packagePathList 包名全路径集合
* @param classWithPath 返回全路径开关 true 自动带上包名 false 只返回类名
* @return List<String> 包下所有的类
*/
public static List<String> getPackageClasses(List<String> packagePathList, boolean classWithPath) {
List<String> result = new ArrayList<>();
for(String packagePath : packagePathList) {
List<String> classNames = getClassName(packagePath);
String path = classWithPath ? packagePath + "." : "";
for (String className : classNames) {
//className:com.example.myFirstProject.enums.SexEnum
result.add(path + className.substring(className.lastIndexOf(".") + 1));
}
}
return result;
}
/**
* 获取该报名全路径下的所有class全路径集合
* @param packageName 包名全路径
* @return
*/
private static List<String> getClassName(String packageName) {
//根据报名获取该package的系统路径
String filePath = ClassLoader.getSystemResource("").getPath() + packageName.replace(".", "\\");
List<String> fileNames = getClassName(filePath, null);
return fileNames;
}
/**
* 获取filePath文件夹下的所有class的全路径集合
* @param filePath
* @param className
* @return
*/
private static List<String> getClassName(String filePath, List<String> className) {
List<String> myClassName = new ArrayList<>();
File file = new File(filePath);
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
if (childFile.isDirectory()) {
//递归获取该文件夹下的子文件夹里的所有文件
myClassName.addAll(getClassName(childFile.getPath(), myClassName));
} else {
String childFilePath = childFile.getPath();
//childFilePath: D:\workspace-git\springbootlearning\target\classes\com\example\myFirstProject\enums\SexEnum.class
childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));
childFilePath = childFilePath.replace("\\", ".");
myClassName.add(childFilePath);
}
}
return myClassName;
}
}
5、定义枚举工具类
public class EnumUtil {
/**
* 获取value返回枚举对象
* @param value
* @param clazz
* */
public static <T extends CodeEnum> T getEnumObject(Object value, Class<T> clazz){
return (T) Constant.ENUM_MAP.get(clazz).get(value);
}
}
6、实体类中调用
import ·····.ProductStateEnum;
import ·····.EnumUtil;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
import java.util.Date;
@Entity
@Data
public class Product {
@Id
private String pId;
private String pName;
private Integer pState;
public String getProductStatusEnum(){
return EnumUtil.getEnumObject(pState,ProductStateEnum.class).getMsg();
}
}