前言
在项目中,会存在很多枚举,比如下面颜色和水果的枚举,COLOR[RED(1),GREEN(2),BLUE(3)],FRUIT[APPLE(4),BANANA(5),ORANGE(6)],但是问题是,如果要增加枚举类型,就会涉及修改代码的问题,一个比较常见的例子是,枚举生成一个列表,供前台选择,如果增加类型,那么前后台都需要进行修改。
一个思路,是将枚举项保存在数据库里,项目启动或定时刷新枚举项。在项目中,只定义枚举类型,比如
public enum COLOR{}
public enum FRUIT{}
在接口中,使用COLOR.valueOf()就可以获取枚举,并传入后面的处理层了。为什么要使用枚举呢?类型安全么,只要可以获取枚举,就说明前台传的值是正确的,即进行了范围校验。
代码
代码是参考了一篇文章https://www.niceideas.ch/roll...
有兴趣的可以看一下。他的例子,是在一个数据库表里面有一张类似业务流水表,上面有一个CODE字段,代码了对记录的不同处理,CODE很多,他不想手写大量的if...else语句,想转成enum进行处理(这个我还没有相同,动态的enum也没办法直接在代码上switch case,那么是不是他在生成的枚举上,包含了调用的方法)。
下面是在他代码之上,进行了些许的修改,便于适应我自己的业务场景。
public enum CodeInfoEnum {
LOCK(1L,1L,"LOCK_TYPE","LOCK"),UNLOCK(1L,2L,"LOCK_TYPE","LOCK");
public Long classId;
public Long infoId;
public String classCode;
public String infoCode;
CodeInfoEnum(Long classId,Long infoId,String classCode,String infoCode){
this.classId = classId;
this.infoId = infoId;
this.classCode = classCode;
this.infoCode = infoCode;
}
public static CodeInfoEnum getByInfoId(Long infoId){
return CodeInfoEnum.valueOf(infoId+"");
}
public static List<CodeInfoEnum> getByClassId(Long classId){
return Arrays.stream(CodeInfoEnum.values()).filter(item->item.classId.equals(classId)).collect(Collectors.toList());
}
public static CodeInfoEnum getByClassCodeAndInfoCode(String classCode,String infoCode){
Optional<CodeInfoEnum> opt = Arrays.stream(CodeInfoEnum.values()).filter(item->item.classCode.equals(classCode)&&item.infoCode.equals(infoCode)).findFirst();
return opt.orElse(null);
}
@Override
public String toString() {
return "CodeInfoEnum{" +
"classId=" + classId +
", infoId=" + infoId +
", classCode='" + classCode + '\'' +
", infoCode='" + infoCode + '\'' +
'}';
}
}
public class DynamicEnumTest {
private static ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
private static void setFailsafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException,
IllegalAccessException {
// let's make the field accessible
field.setAccessible(true);
// next we change the modifier in the Field instance to
// not be final anymore, thus tricking reflection into
// letting us modify the static final field
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field);
// blank out the final bit in the modifiers int
modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers);
FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false);
fa.set(target, value);
}
private static void blankField(Class<?> enumClass, String fieldName) throws NoSuchFieldException,
IllegalAccessException {
for (Field field : Class.class.getDeclaredFields()) {
if (field.getName().contains(fieldName)) {
AccessibleObject.setAccessible(new Field[] { field }, true);
setFailsafeFieldValue(field, enumClass, null);
break;
}
}
}
private static void cleanEnumCache(Class<?> enumClass) throws NoSuchFieldException, IllegalAccessException {
blankField(enumClass, "enumConstantDirectory"); // Sun (Oracle?!?) JDK 1.5/6
blankField(enumClass, "enumConstants"); // IBM JDK
}
private static ConstructorAccessor getConstructorAccessor(Class<?> enumClass, Class<?>[] additionalParameterTypes)
throws NoSuchMethodException {
Class<?>[] parameterTypes = new Class[additionalParameterTypes.length + 2];
parameterTypes[0] = String.class;
parameterTypes[1] = int.class;
System.arraycopy(additionalParameterTypes, 0, parameterTypes, 2, additionalParameterTypes.length);
return reflectionFactory.newConstructorAccessor(enumClass.getDeclaredConstructor(parameterTypes));
}
private static Object makeEnum(Class<?> enumClass, String value, int ordinal, Class<?>[] additionalTypes,
Object[] additionalValues) throws Exception {
Object[] parms = new Object[additionalValues.length + 2];
parms[0] = value;
parms[1] = Integer.valueOf(ordinal);
System.arraycopy(additionalValues, 0, parms, 2, additionalValues.length);
return enumClass.cast(getConstructorAccessor(enumClass, additionalTypes).newInstance(parms));
}
/**
* Add an enum instance to the enum class given as argument
*
* @param <T> the type of the enum (implicit)
* @param enumType the class of the enum to be modified
* @param enumName the name of the new enum instance to be added to the class.
*/
@SuppressWarnings("unchecked")
public static <T extends Enum<?>> void addEnum(Class<T> enumType, String enumName,Class<?>[] paramClass,Object[] paramValue) {
// 0. Sanity checks
if (!Enum.class.isAssignableFrom(enumType)) {
throw new RuntimeException("class " + enumType + " is not an instance of Enum");
}
// 1. Lookup "$VALUES" holder in enum class and get previous enum instances
Field valuesField = null;
Field[] fields = CodeInfoEnum.class.getDeclaredFields();
for (Field field : fields) {
if (field.getName().contains("$VALUES")) {
valuesField = field;
break;
}
}
AccessibleObject.setAccessible(new Field[] { valuesField }, true);
try {
// 2. Copy it
T[] previousValues = (T[]) valuesField.get(enumType);
List<T> values = new ArrayList<T>(Arrays.asList(previousValues));
// 3. build new enum
T newValue = (T) makeEnum(enumType, // The target enum class
enumName, // THE NEW ENUM INSTANCE TO BE DYNAMICALLY ADDED
values.size(),
//new Class<?>[] {}, // could be used to pass values to the enum constuctor if needed
paramClass,
//new Object[] {}
paramValue
); // could be used to pass values to the enum constuctor if needed
// 4. add new value
values.add(newValue);
Object object=values.toArray((T[]) Array.newInstance(enumType, 0));
// 5. Set new values field
setFailsafeFieldValue(valuesField, null, object);
// 6. Clean enum cache
cleanEnumCache(enumType);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
public static void main(String[] args) {
//
synchronized (CodeInfoEnum.class) {
addEnum(CodeInfoEnum.class, "3", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{2L, 3L, "ActiveStatus", "Active"});
addEnum(CodeInfoEnum.class, "4", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{2L, 4L, "ActiveStatus", "Inactive"});
addEnum(CodeInfoEnum.class, "5", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{3L, 5L, "Optype", "OP1"});
addEnum(CodeInfoEnum.class, "6", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{3L, 6L, "Optype", "OP2"});
addEnum(CodeInfoEnum.class, "7", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{3L, 7L, "Optype", "OP3"});
addEnum(CodeInfoEnum.class, "8", new Class<?>[]{Long.class, Long.class, String.class, String.class}, new Object[]{3L, 8L, "Optype", "OP4"});
}
CodeInfoEnum codeInfoEnum =CodeInfoEnum.valueOf("5");
System.out.println(codeInfoEnum);
// Run a few tests just to show it works OK.
System.out.println(Arrays.deepToString(CodeInfoEnum.values()));
System.out.println("============================打印所有枚举(包括固定的和动态的),可以将数据库中保存的CIC以枚举的形式加载到JVM");
for(CodeInfoEnum codeInfo:CodeInfoEnum.values()){
System.out.println(codeInfo.toString());
}
System.out.println("============================通过codeId找到的枚举,用于PO转VO的处理");
CodeInfoEnum activeStatus_Active = CodeInfoEnum.getByInfoId(3L);
System.out.println(activeStatus_Active);
System.out.println("============================通过ClassId找到的枚举列表");
List<CodeInfoEnum> activeStatusEnumList = CodeInfoEnum.getByClassId(3L);
for(CodeInfoEnum codeInfo : activeStatusEnumList){
System.out.println(codeInfo);
}
System.out.println("============================通过ClassCode和InfoCode获取枚举,用于导入验证CIC合法性");
CodeInfoEnum toGetActiveStatus_Active = CodeInfoEnum.getByClassCodeAndInfoCode("ActiveStatus","Active");
System.out.println(toGetActiveStatus_Active);
System.out.println("============================通过ClassCode和InfoCode获取枚举,输入不存在的Code,则返回NULL");
CodeInfoEnum toGetActiveStatus_miss = CodeInfoEnum.getByClassCodeAndInfoCode("ActiveStatus","MISS");
System.out.println(toGetActiveStatus_miss);
}
}
我将项目中所有的枚举,都定义在了CodeInfoEnum中,其中包含两部分,一部分是固定的枚举,我是预定义的,比如记录状态(有效|删除),锁定状态(可用|锁定)等,这些的枚举字面值是英文大写单词;另一部分是动态的,具有比较强的业务含义,比如仓库管理中的库位类型,包装类型这些,这些的枚举字面值是数字,即数据库中的ID。
在使用的使用,注意:
- 在和前台交互的时候,统一使用字面值
- 在校验的时候,使用class_code和字面值进行校验。
- 在controller进行参数组装的时候,将前台传入的字面值转成CodeInfoEnum
- 在持久化的时候使用字面值
- 查询的时候,将数据库中的字面值转化为CodeInfoEnum
上面写的‘使用’,目前还是设想,还没有动手实现。也是我下面准备做的。希望有看到本文的大神,如能提供宝贵意见,将不胜感激。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。