当时的需求是导出的时候加一个title,然后用的是EasyExcel,注解使用的是ExcelProperty
, 然后就懒嘛,不想一个个加了,所以直接改注解,上代码;
/**
* 功能描述: <br>
* 修改注解信息
*
* @param T
* @param title
* @Return: void
* @Author: Lpj
* @Date: 2022-01-05 5:16 下午
*/
private static <T> void addTitleToExcelPropertyValue(Class<T> T, String title) {
Field[] fields = ReflectUtil.getFields(T);
for (Field field : fields) {
ExcelProperty annotation = AnnotationUtil.getAnnotation(field, ExcelProperty.class);
if(Objects.isNull(annotation)) continue;
InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
Map<String, Object> memberValues = (Map<String, Object>) ReflectUtil.getFieldValue(invocationHandler, "memberValues");
String[] originValue = annotation.value();
if(!ArrayUtil.contains(originValue, title)) {
String[] newValue = ArrayUtil.insert(originValue, 0, title);
memberValues.put("value", newValue);
}
}
}
因为每次导出,都要去前置插入title,所以,导出完要给移除掉。
/**
* 功能描述: <br>
* 移除注解信息
*
* @param T
* @param title
* @Return: void
* @Author: Lpj
* @Date: 2022-01-05 5:16 下午
*/
private static <T> void removeTitleFromExcelPropertyValue(Class<T> T, String title) {
Field[] fields = ReflectUtil.getFields(T);
for (Field field : fields) {
ExcelProperty annotation = AnnotationUtil.getAnnotation(field, ExcelProperty.class);
if(Objects.isNull(annotation)) continue;
InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
Map<String, Object> memberValues = (Map<String, Object>) ReflectUtil.getFieldValue(invocationHandler, "memberValues");
String[] originValue = annotation.value();
if(!ArrayUtil.contains(originValue, title)) {
String[] newValue = ArrayUtil.removeEle(originValue, title);
memberValues.put("value", newValue);
}
}
}