diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/annotation/ExcelUnInheritable.java b/easyexcel-core/src/main/java/com/alibaba/excel/annotation/ExcelUnInheritable.java new file mode 100644 index 00000000..67f7527e --- /dev/null +++ b/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 { +} \ No newline at end of file diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java index f9a53e08..48719280 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/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; @@ -74,25 +76,25 @@ public class ClassUtils { * The cache configuration information for each of the class */ public static final ConcurrentHashMap, Map> CLASS_CONTENT_CACHE - = new ConcurrentHashMap<>(); + = new ConcurrentHashMap<>(); /** * The cache configuration information for each of the class */ private static final ThreadLocal, Map>> CLASS_CONTENT_THREAD_LOCAL - = new ThreadLocal<>(); + = new ThreadLocal<>(); /** * The cache configuration information for each of the class */ public static final ConcurrentHashMap CONTENT_CACHE - = new ConcurrentHashMap<>(); + = new ConcurrentHashMap<>(); /** * The cache configuration information for each of the class */ private static final ThreadLocal> CONTENT_THREAD_LOCAL - = new ThreadLocal<>(); + = new ThreadLocal<>(); /** * Calculate the configuration information for the class @@ -103,8 +105,8 @@ public class ClassUtils { * @return */ public static ExcelContentProperty declaredExcelContentProperty(Map dataMap, Class headClazz, - String fieldName, - ConfigurationHolder configurationHolder) { + String fieldName, + ConfigurationHolder configurationHolder) { Class clazz = null; if (dataMap instanceof BeanMap) { Object bean = ((BeanMap)dataMap).getBean(); @@ -116,7 +118,7 @@ public class ClassUtils { } private static ExcelContentProperty getExcelContentProperty(Class clazz, Class headClass, String fieldName, - ConfigurationHolder configurationHolder) { + ConfigurationHolder configurationHolder) { switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) { case THREAD_LOCAL: Map contentCacheMap = CONTENT_THREAD_LOCAL.get(); @@ -139,16 +141,16 @@ public class ClassUtils { } private static ExcelContentProperty doGetExcelContentProperty(Class clazz, Class headClass, - String fieldName, - ConfigurationHolder configurationHolder) { + String fieldName, + ConfigurationHolder configurationHolder) { ExcelContentProperty excelContentProperty = Optional.ofNullable( - declaredFieldContentMap(clazz, configurationHolder)) - .map(map -> map.get(fieldName)) - .orElse(null); + declaredFieldContentMap(clazz, configurationHolder)) + .map(map -> map.get(fieldName)) + .orElse(null); ExcelContentProperty headExcelContentProperty = Optional.ofNullable( - declaredFieldContentMap(headClass, configurationHolder)) - .map(map -> map.get(fieldName)) - .orElse(null); + declaredFieldContentMap(headClass, configurationHolder)) + .map(map -> map.get(fieldName)) + .orElse(null); ExcelContentProperty combineExcelContentProperty = new ExcelContentProperty(); combineExcelContentProperty(combineExcelContentProperty, headExcelContentProperty); @@ -159,7 +161,7 @@ public class ClassUtils { } public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty, - ExcelContentProperty excelContentProperty) { + ExcelContentProperty excelContentProperty) { if (excelContentProperty == null) { return; } @@ -188,14 +190,14 @@ public class ClassUtils { } private static Map declaredFieldContentMap(Class clazz, - ConfigurationHolder configurationHolder) { + ConfigurationHolder configurationHolder) { if (clazz == null) { return null; } switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) { case THREAD_LOCAL: Map, Map> classContentCacheMap - = CLASS_CONTENT_THREAD_LOCAL.get(); + = CLASS_CONTENT_THREAD_LOCAL.get(); if (classContentCacheMap == null) { classContentCacheMap = MapUtils.newHashMap(); CLASS_CONTENT_THREAD_LOCAL.set(classContentCacheMap); @@ -230,7 +232,7 @@ public class ClassUtils { ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class); ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class); Map fieldContentMap = MapUtils.newHashMapWithExpectedSize( - tempFieldList.size()); + tempFieldList.size()); for (Field field : tempFieldList) { ExcelContentProperty excelContentProperty = new ExcelContentProperty(); excelContentProperty.setField(field); @@ -244,7 +246,7 @@ public class ClassUtils { excelContentProperty.setConverter(converter); } catch (Exception e) { throw new ExcelCommonException( - "Can not instance custom converter:" + convertClazz.getName()); + "Can not instance custom converter:" + convertClazz.getName()); } } } @@ -262,9 +264,9 @@ public class ClassUtils { excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); excelContentProperty.setDateTimeFormatProperty( - DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); + DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); excelContentProperty.setNumberFormatProperty( - NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); + NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); fieldContentMap.put(field.getName(), excelContentProperty); } @@ -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> orderFieldMap = new TreeMap<>(); @@ -328,9 +336,9 @@ public class ClassUtils { WriteHolder writeHolder = (WriteHolder)configurationHolder; boolean needIgnore = !CollectionUtils.isEmpty(writeHolder.excludeColumnFieldNames()) - || !CollectionUtils.isEmpty(writeHolder.excludeColumnIndexes()) - || !CollectionUtils.isEmpty(writeHolder.includeColumnFieldNames()) - || !CollectionUtils.isEmpty(writeHolder.includeColumnIndexes()); + || !CollectionUtils.isEmpty(writeHolder.excludeColumnIndexes()) + || !CollectionUtils.isEmpty(writeHolder.includeColumnFieldNames()) + || !CollectionUtils.isEmpty(writeHolder.includeColumnIndexes()); if (!needIgnore) { return fieldCache; @@ -428,10 +436,10 @@ public class ClassUtils { } private static Map buildSortedAllFieldMap(Map> orderFieldMap, - Map indexFieldMap) { + Map indexFieldMap) { Map sortedAllFieldMap = new HashMap<>( - (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1); + (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1); Map tempIndexFieldMap = new HashMap<>(indexFieldMap); int index = 0; @@ -451,8 +459,8 @@ public class ClassUtils { } private static void declaredOneField(Field field, Map> orderFieldMap, - Map indexFieldMap, Set ignoreSet, - ExcelIgnoreUnannotated excelIgnoreUnannotated) { + Map indexFieldMap, Set ignoreSet, + ExcelIgnoreUnannotated excelIgnoreUnannotated) { String fieldName = FieldUtils.resolveCglibFieldName(field); FieldWrapper fieldWrapper = new FieldWrapper(); fieldWrapper.setField(field); @@ -471,8 +479,8 @@ public class ClassUtils { return; } boolean isStaticFinalOrTransient = - (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) - || Modifier.isTransient(field.getModifiers()); + (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) + || Modifier.isTransient(field.getModifiers()); if (excelProperty == null && isStaticFinalOrTransient) { ignoreSet.add(fieldName); return; @@ -485,8 +493,8 @@ public class ClassUtils { if (excelProperty != null && excelProperty.index() >= 0) { if (indexFieldMap.containsKey(excelProperty.index())) { throw new ExcelCommonException( - "The index of '" + indexFieldMap.get(excelProperty.index()).getFieldName() - + "' and '" + field.getName() + "' must be inconsistent"); + "The index of '" + indexFieldMap.get(excelProperty.index()).getFieldName() + + "' and '" + field.getName() + "' must be inconsistent"); } indexFieldMap.put(excelProperty.index(), fieldWrapper); return; @@ -579,5 +587,4 @@ public class ClassUtils { CLASS_CONTENT_THREAD_LOCAL.remove(); CONTENT_THREAD_LOCAL.remove(); } -} - +} \ No newline at end of file