Browse Source

添加注解`ExcelUnInheritable`,声明不可指定类的`Field`不进行继承,可避免继承结构中字段重复和`Converter`覆盖问题

pull/3724/head
sunshuaiju 11 months ago
parent
commit
aed2b13159
  1. 16
      easyexcel-core/src/main/java/com/alibaba/excel/annotation/ExcelUnInheritable.java
  2. 9
      easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java

16
easyexcel-core/src/main/java/com/alibaba/excel/annotation/ExcelUnInheritable.java

@ -0,0 +1,16 @@
package com.alibaba.excel.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Declare that this type of field is not inheritable
*
* @author ShuaiJu Sun
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelUnInheritable {
}

9
easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java

@ -13,11 +13,13 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.ExcelUnInheritable;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
@ -306,8 +308,14 @@ public class ClassUtils {
// level.
while (tempClass != null) {
Collections.addAll(tempFieldList, tempClass.getDeclaredFields());
// Get the parent class and give it to yourself
tempClass = tempClass.getSuperclass();
ExcelUnInheritable excelUnInheritable = Optional.ofNullable(tempClass)
.map(item -> item.getAnnotation(ExcelUnInheritable.class)).orElse(null);
if (Objects.nonNull(excelUnInheritable)){
break;
}
}
// Screening of field
Map<Integer, List<FieldWrapper>> orderFieldMap = new TreeMap<>();
@ -580,4 +588,3 @@ public class ClassUtils {
CONTENT_THREAD_LOCAL.remove();
}
}

Loading…
Cancel
Save