From 6923b804affd621b6c04c2bf011a4f2223d0c622 Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 28 Apr 2023 18:12:26 +0800 Subject: [PATCH 1/7] PoiUtils initRowField add thread security --- .../java/com/alibaba/excel/util/PoiUtils.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java index 2989b22b..907bd16f 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java @@ -1,6 +1,9 @@ package com.alibaba.excel.util; +import java.lang.reflect.Field; + import com.alibaba.excel.exception.ExcelRuntimeException; + import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.ss.usermodel.Row; @@ -8,8 +11,6 @@ import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.xssf.usermodel.XSSFRow; -import java.lang.reflect.Field; - /** * utils * @@ -22,7 +23,7 @@ public class PoiUtils { */ public static final BitField CUSTOM_HEIGHT = BitFieldFactory.getInstance(0x640); - private static Field ROW_RECORD_FIELD; + private static volatile Field ROW_RECORD_FIELD; /** * Whether to customize the height @@ -32,16 +33,20 @@ public class PoiUtils { */ public static boolean customHeight(Row row) { if (row instanceof XSSFRow) { - XSSFRow xssfRow = (XSSFRow) row; + XSSFRow xssfRow = (XSSFRow)row; return xssfRow.getCTRow().getCustomHeight(); } if (row instanceof HSSFRow) { - HSSFRow hssfRow = (HSSFRow) row; + HSSFRow hssfRow = (HSSFRow)row; try { if (ROW_RECORD_FIELD == null) { - initRowRecordField(); + synchronized (PoiUtils.class) { + if (ROW_RECORD_FIELD == null) { + initRowRecordField(); + } + } } - RowRecord record = (RowRecord) ROW_RECORD_FIELD.get(hssfRow); + RowRecord record = (RowRecord)ROW_RECORD_FIELD.get(hssfRow); return CUSTOM_HEIGHT.getValue(record.getOptionFlags()) == 1; } catch (IllegalAccessException ignore) { } @@ -49,7 +54,6 @@ public class PoiUtils { return false; } - private static void initRowRecordField() { try { ROW_RECORD_FIELD = HSSFRow.class.getDeclaredField("row"); From ef47d3e9b5c6a22c518fd3c395ef0636dafcf004 Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Thu, 11 May 2023 16:05:39 +0800 Subject: [PATCH 2/7] add @Deprecated annotation and perfect comment --- .../write/builder/AbstractExcelWriterParameterBuilder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java index 5f6e8cad..014f97e2 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/AbstractExcelWriterParameterBuilder.java @@ -106,8 +106,9 @@ public abstract class AbstractExcelWriterParameterBuilder includeColumnFieldNames) { parameter().setIncludeColumnFieldNames(includeColumnFieldNames); return self(); From 7841d72f077add6745ef1df452a069cc3dd62394 Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 12 May 2023 09:37:10 +0800 Subject: [PATCH 3/7] modify mistake error key --- .../src/main/java/com/alibaba/excel/util/ClassUtils.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 ae96c45c..f485d388 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 @@ -341,13 +341,11 @@ public class ClassUtils { // The current field needs to be ignored if (writeHolder.ignore(field.getFieldName(), entry.getKey())) { - if (ignoreSet != null) { - ignoreSet.add(field.getFieldName()); - } + ignoreSet.add(field.getFieldName()); indexFieldMap.remove(index); } else { // Mandatory sorted fields - if (ignoreSet.contains(key)) { + if (indexFieldMap.containsKey(key)) { tempSortedFieldMapp.put(key, field); } else { // Need to reorder automatically From 150fb8358737d2f876304a4030ab0b3c58808131 Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 12 May 2023 09:38:41 +0800 Subject: [PATCH 4/7] revise comment --- .../com/alibaba/excel/util/ClassUtils.java | 61 ++++++++----------- 1 file changed, 26 insertions(+), 35 deletions(-) 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 f485d388..c520959e 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 @@ -1,20 +1,5 @@ package com.alibaba.excel.util; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.TreeMap; -import java.util.concurrent.ConcurrentHashMap; - import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; @@ -34,7 +19,6 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; - import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -42,6 +26,11 @@ import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.springframework.cglib.beans.BeanMap; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -49,7 +38,7 @@ import org.springframework.cglib.beans.BeanMap; * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -102,10 +91,11 @@ 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(); + Object bean = ((BeanMap) dataMap).getBean(); if (bean != null) { clazz = bean.getClass(); } @@ -114,7 +104,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(); @@ -137,7 +127,8 @@ 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)) @@ -156,7 +147,7 @@ public class ClassUtils { } public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty, - ExcelContentProperty excelContentProperty) { + ExcelContentProperty excelContentProperty) { if (excelContentProperty == null) { return; } @@ -185,7 +176,7 @@ public class ClassUtils { } private static Map declaredFieldContentMap(Class clazz, - ConfigurationHolder configurationHolder) { + ConfigurationHolder configurationHolder) { if (clazz == null) { return null; } @@ -322,7 +313,7 @@ public class ClassUtils { return fieldCache; } - WriteHolder writeHolder = (WriteHolder)configurationHolder; + WriteHolder writeHolder = (WriteHolder) configurationHolder; boolean needIgnore = !CollectionUtils.isEmpty(writeHolder.excludeColumnFieldNames()) || !CollectionUtils.isEmpty(writeHolder.excludeColumnIndexes()) @@ -333,7 +324,7 @@ public class ClassUtils { return fieldCache; } // ignore filed - Map tempSortedFieldMapp = MapUtils.newHashMap(); + Map tempSortedFieldMap = MapUtils.newHashMap(); int index = 0; for (Map.Entry entry : sortedFieldMap.entrySet()) { Integer key = entry.getKey(); @@ -346,18 +337,18 @@ public class ClassUtils { } else { // Mandatory sorted fields if (indexFieldMap.containsKey(key)) { - tempSortedFieldMapp.put(key, field); + tempSortedFieldMap.put(key, field); } else { // Need to reorder automatically // Check whether the current key is already in use - while (tempSortedFieldMapp.containsKey(index)) { + while (tempSortedFieldMap.containsKey(index)) { index++; } - tempSortedFieldMapp.put(index++, field); + tempSortedFieldMap.put(index++, field); } } } - fieldCache.setSortedFieldMap(tempSortedFieldMapp); + fieldCache.setSortedFieldMap(tempSortedFieldMap); // resort field resortField(writeHolder, fieldCache); @@ -365,9 +356,9 @@ public class ClassUtils { } /** - * it only works when {@link WriteHolder#getIncludeColumnFieldNames()} or - * {@link WriteHolder#getIncludeColumnIndexes()} ()} has value - * and {@link WriteHolder#getOrderByIncludeColumn()} ()} is true + * it only works when {@link WriteHolder#includeColumnFieldNames()} or + * {@link WriteHolder#includeColumnIndexes()} has value + * and {@link WriteHolder#orderByIncludeColumn()} is true **/ private static void resortField(WriteHolder writeHolder, FieldCache fieldCache) { if (!writeHolder.orderByIncludeColumn()) { @@ -425,7 +416,7 @@ public class ClassUtils { } private static Map buildSortedAllFieldMap(Map> orderFieldMap, - Map indexFieldMap) { + Map indexFieldMap) { Map sortedAllFieldMap = new HashMap<>( (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1); @@ -448,8 +439,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); From e2a3d6bb07490f45c9a5dd9d051239c60cc90cbb Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 12 May 2023 09:50:10 +0800 Subject: [PATCH 5/7] reverse import --- .../com/alibaba/excel/util/ClassUtils.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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 c520959e..b70a9469 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 @@ -1,5 +1,20 @@ package com.alibaba.excel.util; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; + import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; @@ -19,6 +34,7 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; + import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -26,11 +42,6 @@ import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.springframework.cglib.beans.BeanMap; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; - /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with From 65aeb150893fc52d55c425f87a85c7b9ed0fd890 Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 12 May 2023 09:53:09 +0800 Subject: [PATCH 6/7] reverse import --- .../com/alibaba/excel/util/ClassUtils.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) 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 b70a9469..8427b66d 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 @@ -102,11 +102,11 @@ 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(); + Object bean = ((BeanMap)dataMap).getBean(); if (bean != null) { clazz = bean.getClass(); } @@ -115,7 +115,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(); @@ -138,8 +138,8 @@ 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)) @@ -158,7 +158,7 @@ public class ClassUtils { } public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty, - ExcelContentProperty excelContentProperty) { + ExcelContentProperty excelContentProperty) { if (excelContentProperty == null) { return; } @@ -187,7 +187,7 @@ public class ClassUtils { } private static Map declaredFieldContentMap(Class clazz, - ConfigurationHolder configurationHolder) { + ConfigurationHolder configurationHolder) { if (clazz == null) { return null; } @@ -324,7 +324,7 @@ public class ClassUtils { return fieldCache; } - WriteHolder writeHolder = (WriteHolder) configurationHolder; + WriteHolder writeHolder = (WriteHolder)configurationHolder; boolean needIgnore = !CollectionUtils.isEmpty(writeHolder.excludeColumnFieldNames()) || !CollectionUtils.isEmpty(writeHolder.excludeColumnIndexes()) @@ -427,7 +427,7 @@ public class ClassUtils { } private static Map buildSortedAllFieldMap(Map> orderFieldMap, - Map indexFieldMap) { + Map indexFieldMap) { Map sortedAllFieldMap = new HashMap<>( (orderFieldMap.size() + indexFieldMap.size()) * 4 / 3 + 1); @@ -450,8 +450,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); From 6bf3b1ec3645118010adf635cf268c69c7f722ea Mon Sep 17 00:00:00 2001 From: gongxuanzhang Date: Fri, 12 May 2023 09:56:41 +0800 Subject: [PATCH 7/7] reverse import --- .../com/alibaba/excel/util/ClassUtils.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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 8427b66d..92b828b4 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 @@ -49,7 +49,7 @@ import org.springframework.cglib.beans.BeanMap; * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - *

+ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -102,8 +102,7 @@ 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(); @@ -138,8 +137,7 @@ 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)) @@ -335,7 +333,7 @@ public class ClassUtils { return fieldCache; } // ignore filed - Map tempSortedFieldMap = MapUtils.newHashMap(); + Map tempSortedFieldMapp = MapUtils.newHashMap(); int index = 0; for (Map.Entry entry : sortedFieldMap.entrySet()) { Integer key = entry.getKey(); @@ -348,18 +346,18 @@ public class ClassUtils { } else { // Mandatory sorted fields if (indexFieldMap.containsKey(key)) { - tempSortedFieldMap.put(key, field); + tempSortedFieldMapp.put(key, field); } else { // Need to reorder automatically // Check whether the current key is already in use - while (tempSortedFieldMap.containsKey(index)) { + while (tempSortedFieldMapp.containsKey(index)) { index++; } - tempSortedFieldMap.put(index++, field); + tempSortedFieldMapp.put(index++, field); } } } - fieldCache.setSortedFieldMap(tempSortedFieldMap); + fieldCache.setSortedFieldMap(tempSortedFieldMapp); // resort field resortField(writeHolder, fieldCache); @@ -559,3 +557,4 @@ public class ClassUtils { CONTENT_THREAD_LOCAL.remove(); } } +