Browse Source

PoiUtils initRowField add thread security

pull/3157/head
gongxuanzhang 2 years ago
parent
commit
6923b804af
  1. 20
      easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java

20
easyexcel-core/src/main/java/com/alibaba/excel/util/PoiUtils.java

@ -1,6 +1,9 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import java.lang.reflect.Field;
import com.alibaba.excel.exception.ExcelRuntimeException; import com.alibaba.excel.exception.ExcelRuntimeException;
import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Row; 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.util.BitFieldFactory;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import java.lang.reflect.Field;
/** /**
* utils * utils
* *
@ -22,7 +23,7 @@ public class PoiUtils {
*/ */
public static final BitField CUSTOM_HEIGHT = BitFieldFactory.getInstance(0x640); 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 * Whether to customize the height
@ -32,16 +33,20 @@ public class PoiUtils {
*/ */
public static boolean customHeight(Row row) { public static boolean customHeight(Row row) {
if (row instanceof XSSFRow) { if (row instanceof XSSFRow) {
XSSFRow xssfRow = (XSSFRow) row; XSSFRow xssfRow = (XSSFRow)row;
return xssfRow.getCTRow().getCustomHeight(); return xssfRow.getCTRow().getCustomHeight();
} }
if (row instanceof HSSFRow) { if (row instanceof HSSFRow) {
HSSFRow hssfRow = (HSSFRow) row; HSSFRow hssfRow = (HSSFRow)row;
try { try {
if (ROW_RECORD_FIELD == null) { 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; return CUSTOM_HEIGHT.getValue(record.getOptionFlags()) == 1;
} catch (IllegalAccessException ignore) { } catch (IllegalAccessException ignore) {
} }
@ -49,7 +54,6 @@ public class PoiUtils {
return false; return false;
} }
private static void initRowRecordField() { private static void initRowRecordField() {
try { try {
ROW_RECORD_FIELD = HSSFRow.class.getDeclaredField("row"); ROW_RECORD_FIELD = HSSFRow.class.getDeclaredField("row");

Loading…
Cancel
Save