mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-21 10:35:57 +08:00
利用ReflectUtil反射工具类获取Enum属性
This commit is contained in:
parent
cf60a5cd30
commit
201faf93b4
@ -10,6 +10,7 @@ import com.alibaba.excel.metadata.data.ReadCellData;
|
|||||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||||
import com.ruoyi.common.annotation.ExcelEnumFormat;
|
import com.ruoyi.common.annotation.ExcelEnumFormat;
|
||||||
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
@ -36,13 +37,12 @@ public class ExcelEnumConvert implements Converter<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
beforeConverter(contentProperty);
|
|
||||||
Object codeValue = cellData.getData();
|
Object codeValue = cellData.getData();
|
||||||
// 如果是空值
|
// 如果是空值
|
||||||
if (ObjectUtil.isNull(codeValue)) {
|
if (ObjectUtil.isNull(codeValue)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Map<Object, String> enumValueMap = beforeConverter(contentProperty);
|
Map<Object, String> enumValueMap = beforeConvert(contentProperty);
|
||||||
String textValue = enumValueMap.get(codeValue);
|
String textValue = enumValueMap.get(codeValue);
|
||||||
return Convert.convert(contentProperty.getField().getType(), textValue);
|
return Convert.convert(contentProperty.getField().getType(), textValue);
|
||||||
}
|
}
|
||||||
@ -52,28 +52,19 @@ public class ExcelEnumConvert implements Converter<Object> {
|
|||||||
if (ObjectUtil.isNull(object)) {
|
if (ObjectUtil.isNull(object)) {
|
||||||
return new WriteCellData<>("");
|
return new WriteCellData<>("");
|
||||||
}
|
}
|
||||||
Map<Object, String> enumValueMap = beforeConverter(contentProperty);
|
Map<Object, String> enumValueMap = beforeConvert(contentProperty);
|
||||||
String value = Convert.toStr(enumValueMap.get(object));
|
String value = Convert.toStr(enumValueMap.get(object), "");
|
||||||
return new WriteCellData<>(value);
|
return new WriteCellData<>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Object, String> beforeConverter(ExcelContentProperty contentProperty) {
|
private Map<Object, String> beforeConvert(ExcelContentProperty contentProperty) {
|
||||||
ExcelEnumFormat anno = getAnnotation(contentProperty.getField());
|
ExcelEnumFormat anno = getAnnotation(contentProperty.getField());
|
||||||
Map<Object, String> enumValueMap = new HashMap<>();
|
Map<Object, String> enumValueMap = new HashMap<>();
|
||||||
Enum<?>[] enumConstants = anno.enumClass().getEnumConstants();
|
Enum<?>[] enumConstants = anno.enumClass().getEnumConstants();
|
||||||
for (Enum<?> enumConstant : enumConstants) {
|
for (Enum<?> enumConstant : enumConstants) {
|
||||||
try {
|
Object codeValue = ReflectUtils.invokeGetter(enumConstant, anno.codeField());
|
||||||
Field codeFieldObj = enumConstant.getClass().getDeclaredField(anno.codeField());
|
String textValue = ReflectUtils.invokeGetter(enumConstant, anno.textField());
|
||||||
codeFieldObj.setAccessible(true);
|
enumValueMap.put(codeValue, textValue);
|
||||||
Object codeValue = codeFieldObj.get(enumConstant);
|
|
||||||
Field textFieldObj = enumConstant.getClass().getDeclaredField(anno.textField());
|
|
||||||
textFieldObj.setAccessible(true);
|
|
||||||
String textValue = (String) textFieldObj.get(enumConstant);
|
|
||||||
enumValueMap.put(codeValue, textValue);
|
|
||||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
|
||||||
log.error("枚举格式化转换处理异常", e);
|
|
||||||
throw new RuntimeException("枚举格式化转换处理异常");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return enumValueMap;
|
return enumValueMap;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user