Browse Source

修改cglib问题

developing
Jiaju Zhuang 3 years ago
parent
commit
35dd6f1aa3
  1. 5
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  2. 40
      src/main/java/com/alibaba/excel/util/BeanMapUtils.java
  3. 2
      src/main/java/com/alibaba/excel/util/ClassUtils.java
  4. 3
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  5. 4
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  6. 5
      src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java
  7. 4
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

5
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.metadata.property.ExcelContentProperty;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; 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.ConverterUtils;
import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import net.sf.cglib.beans.BeanMap;
/** /**
* Convert to the object the user needs * Convert to the object the user needs
* *
@ -115,7 +114,7 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
map.put(FieldUtils.resolveCglibFieldName(excelContentProperty.getField()), value); map.put(FieldUtils.resolveCglibFieldName(excelContentProperty.getField()), value);
} }
} }
BeanMap.create(resultModel).putAll(map); BeanMapUtils.create(resultModel).putAll(map);
return resultModel; return resultModel;
} }

40
src/main/java/com/alibaba/excel/util/BeanMapUtils.java

@ -0,0 +1,40 @@
package com.alibaba.excel.util;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.beans.BeanMap.Generator;
import net.sf.cglib.core.DefaultNamingPolicy;
/**
* bean utils
*
* @author Jiaju Zhuang
*/
public class BeanMapUtils {
/**
* Helper method to create a new <code>BeanMap</code>. For finer
* control over the generated instance, use a new instance of
* <code>BeanMap.Generator</code> instead of this static method.
*
* Custom naming policy to prevent null pointer exceptions.
* <url>https://github.com/alibaba/easyexcel/issues/2064</url>
*
* @param bean the JavaBean underlying the map
* @return a new <code>BeanMap</code> 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";
}
}
}

2
src/main/java/com/alibaba/excel/util/ClassUtils.java

@ -27,7 +27,7 @@ import com.alibaba.excel.write.metadata.holder.WriteHolder;
**/ **/
public class ClassUtils { public class ClassUtils {
private static final Map<Class<?>, SoftReference<FieldCache>> FIELD_CACHE = new ConcurrentHashMap<>(); public static final Map<Class<?>, SoftReference<FieldCache>> FIELD_CACHE = new ConcurrentHashMap<>();
public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFiledMap, public static void declaredFields(Class<?> clazz, Map<Integer, Field> sortedAllFiledMap,
Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, Boolean convertAllFiled, Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, Boolean convertAllFiled,

3
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.Head;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
@ -120,7 +121,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex, private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFiledMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMap.create(oneRowData); BeanMap beanMap = BeanMapUtils.create(oneRowData);
Set<String> beanMapHandledSet = new HashSet<String>(); Set<String> beanMapHandledSet = new HashSet<String>();
int cellIndex; int cellIndex;
// If it's a class it needs to be cast by type // If it's a class it needs to be cast by type

4
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.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WriteHandlerUtils; 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.fill.FillWrapper;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import net.sf.cglib.beans.BeanMap;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -186,7 +186,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (oneRowData instanceof Map) { if (oneRowData instanceof Map) {
dataMap = (Map)oneRowData; dataMap = (Map)oneRowData;
} else { } else {
dataMap = BeanMap.create(oneRowData); dataMap = BeanMapUtils.create(oneRowData);
} }
WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
Map<String, ExcelContentProperty> fieldNameContentPropertyMap = Map<String, ExcelContentProperty> fieldNameContentPropertyMap =

5
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 java.util.List;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.core.DebuggingClassWriter; import net.sf.cglib.core.DebuggingClassWriter;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
/** /**
* 临时测试 * 临时测试
@ -40,7 +41,7 @@ public class Xls03Test {
//camlData.setAEst("test3"); //camlData.setAEst("test3");
//camlData.setTEST("test4"); //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"));
LOGGER.info("test:{}", beanMap.get("Test")); LOGGER.info("test:{}", beanMap.get("Test"));

4
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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanMap;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -35,7 +35,7 @@ public class Wirte {
public void simpleWrite1() { public void simpleWrite1() {
LargeData ss = new LargeData(); LargeData ss = new LargeData();
ss.setStr23("ttt"); ss.setStr23("ttt");
Map map = BeanMap.create(ss); Map map = BeanMapUtils.create(ss);
System.out.println(map.containsKey("str23")); System.out.println(map.containsKey("str23"));
System.out.println(map.containsKey("str22")); System.out.println(map.containsKey("str22"));
System.out.println(map.get("str23")); System.out.println(map.get("str23"));

Loading…
Cancel
Save