diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index adfa6589..bba3e487 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -13,12 +13,11 @@ import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ConverterUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.MapUtils; -import net.sf.cglib.beans.BeanMap; - /** * Convert to the object the user needs * @@ -115,7 +114,7 @@ public class ModelBuildEventListener implements ReadListenerBeanMap. For finer + * control over the generated instance, use a new instance of + * BeanMap.Generator instead of this static method. + * + * Custom naming policy to prevent null pointer exceptions. + * https://github.com/alibaba/easyexcel/issues/2064 + * + * @param bean the JavaBean underlying the map + * @return a new BeanMap instance + */ + public static BeanMap create(Object bean) { + Generator gen = new Generator(); + gen.setBean(bean); + gen.setNamingPolicy(EasyExcelNamingPolicy.INSTANCE); + return gen.create(); + } + + public static class EasyExcelNamingPolicy extends DefaultNamingPolicy { + public static final EasyExcelNamingPolicy INSTANCE = new EasyExcelNamingPolicy(); + + @Override + protected String getTag() { + return "ByEasyExcelCGLIB"; + } + } +} diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 2f628780..96cc11f8 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -27,7 +27,7 @@ import com.alibaba.excel.write.metadata.holder.WriteHolder; **/ public class ClassUtils { - private static final Map, SoftReference> FIELD_CACHE = new ConcurrentHashMap<>(); + public static final Map, SoftReference> FIELD_CACHE = new ConcurrentHashMap<>(); public static void declaredFields(Class clazz, Map sortedAllFiledMap, Map indexFiledMap, Map ignoreMap, Boolean convertAllFiled, diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index d2677d3d..01f9b53d 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -13,6 +13,7 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.WorkBookUtil; @@ -120,7 +121,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex, Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); - BeanMap beanMap = BeanMap.create(oneRowData); + BeanMap beanMap = BeanMapUtils.create(oneRowData); Set beanMapHandledSet = new HashSet(); int cellIndex; // If it's a class it needs to be cast by type diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index fd3802c0..c3b66472 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -16,6 +16,7 @@ import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WriteHandlerUtils; @@ -24,7 +25,6 @@ import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; -import net.sf.cglib.beans.BeanMap; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -186,7 +186,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { if (oneRowData instanceof Map) { dataMap = (Map)oneRowData; } else { - dataMap = BeanMap.create(oneRowData); + dataMap = BeanMapUtils.create(oneRowData); } WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); Map fieldNameContentPropertyMap = diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java index e5d7dfc3..906c092e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java @@ -3,14 +3,15 @@ package com.alibaba.easyexcel.test.temp; import java.util.List; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.fastjson.JSON; +import net.sf.cglib.beans.BeanMap; import net.sf.cglib.core.DebuggingClassWriter; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cglib.beans.BeanMap; /** * 临时测试 @@ -40,7 +41,7 @@ public class Xls03Test { //camlData.setAEst("test3"); //camlData.setTEST("test4"); - BeanMap beanMap = BeanMap.create(camlData); + BeanMap beanMap = BeanMapUtils.create(camlData); LOGGER.info("test:{}", beanMap.get("test")); LOGGER.info("test:{}", beanMap.get("Test")); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java index 98e77527..3cc54749 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java @@ -10,12 +10,12 @@ import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; -import net.sf.cglib.beans.BeanMap; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; @@ -35,7 +35,7 @@ public class Wirte { public void simpleWrite1() { LargeData ss = new LargeData(); ss.setStr23("ttt"); - Map map = BeanMap.create(ss); + Map map = BeanMapUtils.create(ss); System.out.println(map.containsKey("str23")); System.out.println(map.containsKey("str22")); System.out.println(map.get("str23"));