Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加注解ExcelUnInheritable,声明不可指定类的Field不进行继承 #3724

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
}
77 changes: 42 additions & 35 deletions easyexcel-core/src/main/java/com/alibaba/excel/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
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;
Expand Down Expand Up @@ -74,25 +76,25 @@ public class ClassUtils {
* The cache configuration information for each of the class
*/
public static final ConcurrentHashMap<Class<?>, Map<String, ExcelContentProperty>> CLASS_CONTENT_CACHE
= new ConcurrentHashMap<>();
= new ConcurrentHashMap<>();

/**
* The cache configuration information for each of the class
*/
private static final ThreadLocal<Map<Class<?>, Map<String, ExcelContentProperty>>> CLASS_CONTENT_THREAD_LOCAL
= new ThreadLocal<>();
= new ThreadLocal<>();

/**
* The cache configuration information for each of the class
*/
public static final ConcurrentHashMap<ContentPropertyKey, ExcelContentProperty> CONTENT_CACHE
= new ConcurrentHashMap<>();
= new ConcurrentHashMap<>();

/**
* The cache configuration information for each of the class
*/
private static final ThreadLocal<Map<ContentPropertyKey, ExcelContentProperty>> CONTENT_THREAD_LOCAL
= new ThreadLocal<>();
= new ThreadLocal<>();

/**
* Calculate the configuration information for the class
Expand All @@ -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();
Expand All @@ -116,7 +118,7 @@ public static ExcelContentProperty declaredExcelContentProperty(Map<?, ?> dataMa
}

private static ExcelContentProperty getExcelContentProperty(Class<?> clazz, Class<?> headClass, String fieldName,
ConfigurationHolder configurationHolder) {
ConfigurationHolder configurationHolder) {
switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) {
case THREAD_LOCAL:
Map<ContentPropertyKey, ExcelContentProperty> contentCacheMap = CONTENT_THREAD_LOCAL.get();
Expand All @@ -139,16 +141,16 @@ private static ExcelContentProperty getExcelContentProperty(Class<?> clazz, Clas
}

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);
Expand All @@ -159,7 +161,7 @@ private static ExcelContentProperty doGetExcelContentProperty(Class<?> clazz, Cl
}

public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty,
ExcelContentProperty excelContentProperty) {
ExcelContentProperty excelContentProperty) {
if (excelContentProperty == null) {
return;
}
Expand Down Expand Up @@ -188,14 +190,14 @@ private static ContentPropertyKey buildKey(Class<?> clazz, Class<?> headClass, S
}

private static Map<String, ExcelContentProperty> declaredFieldContentMap(Class<?> clazz,
ConfigurationHolder configurationHolder) {
ConfigurationHolder configurationHolder) {
if (clazz == null) {
return null;
}
switch (configurationHolder.globalConfiguration().getFiledCacheLocation()) {
case THREAD_LOCAL:
Map<Class<?>, Map<String, ExcelContentProperty>> classContentCacheMap
= CLASS_CONTENT_THREAD_LOCAL.get();
= CLASS_CONTENT_THREAD_LOCAL.get();
if (classContentCacheMap == null) {
classContentCacheMap = MapUtils.newHashMap();
CLASS_CONTENT_THREAD_LOCAL.set(classContentCacheMap);
Expand Down Expand Up @@ -230,7 +232,7 @@ private static Map<String, ExcelContentProperty> doDeclaredFieldContentMap(Class
ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class);
ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class);
Map<String, ExcelContentProperty> fieldContentMap = MapUtils.newHashMapWithExpectedSize(
tempFieldList.size());
tempFieldList.size());
for (Field field : tempFieldList) {
ExcelContentProperty excelContentProperty = new ExcelContentProperty();
excelContentProperty.setField(field);
Expand All @@ -244,7 +246,7 @@ private static Map<String, ExcelContentProperty> doDeclaredFieldContentMap(Class
excelContentProperty.setConverter(converter);
} catch (Exception e) {
throw new ExcelCommonException(
"Can not instance custom converter:" + convertClazz.getName());
"Can not instance custom converter:" + convertClazz.getName());
}
}
}
Expand All @@ -262,9 +264,9 @@ private static Map<String, ExcelContentProperty> doDeclaredFieldContentMap(Class
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);
}
Expand Down Expand Up @@ -306,8 +308,14 @@ private static FieldCache doDeclaredFields(Class<?> clazz, ConfigurationHolder c
// 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<>();
Expand All @@ -328,9 +336,9 @@ private static FieldCache doDeclaredFields(Class<?> clazz, ConfigurationHolder c
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;
Expand Down Expand Up @@ -428,10 +436,10 @@ private static void resortField(WriteHolder writeHolder, FieldCache fieldCache)
}

private static Map<Integer, FieldWrapper> buildSortedAllFieldMap(Map<Integer, List<FieldWrapper>> orderFieldMap,
Map<Integer, FieldWrapper> indexFieldMap) {
Map<Integer, FieldWrapper> indexFieldMap) {

Map<Integer, FieldWrapper> sortedAllFieldMap = new HashMap<>(
(orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1);
(orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1);

Map<Integer, FieldWrapper> tempIndexFieldMap = new HashMap<>(indexFieldMap);
int index = 0;
Expand All @@ -451,8 +459,8 @@ private static Map<Integer, FieldWrapper> buildSortedAllFieldMap(Map<Integer, Li
}

private static void declaredOneField(Field field, Map<Integer, List<FieldWrapper>> orderFieldMap,
Map<Integer, FieldWrapper> indexFieldMap, Set<String> ignoreSet,
ExcelIgnoreUnannotated excelIgnoreUnannotated) {
Map<Integer, FieldWrapper> indexFieldMap, Set<String> ignoreSet,
ExcelIgnoreUnannotated excelIgnoreUnannotated) {
String fieldName = FieldUtils.resolveCglibFieldName(field);
FieldWrapper fieldWrapper = new FieldWrapper();
fieldWrapper.setField(field);
Expand All @@ -471,8 +479,8 @@ private static void declaredOneField(Field field, Map<Integer, List<FieldWrapper
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;
Expand All @@ -485,8 +493,8 @@ private static void declaredOneField(Field field, Map<Integer, List<FieldWrapper
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;
Expand Down Expand Up @@ -579,5 +587,4 @@ public static void removeThreadLocalCache() {
CLASS_CONTENT_THREAD_LOCAL.remove();
CONTENT_THREAD_LOCAL.remove();
}
}

}