diff --git a/example/pom.xml b/example/pom.xml index 297cd2ae..9e32182d 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -59,7 +59,7 @@ com.github.liaochong myexcel - 4.3.0.RC + 4.3.0.RC5 org.apache.logging.log4j diff --git a/pom.xml b/pom.xml index 89290754..4e32a31e 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.github.liaochong myexcel - 4.3.0.RC4 + 4.3.0.RC5 jar myexcel @@ -23,9 +23,9 @@ UTF-8 1.8 5.2.3 - 1.15.4 + 1.16.1 1.18.22 - 3.15.0.RELEASE + 3.15.4.RELEASE 2.3 2.3.32 3.0.9 @@ -33,7 +33,7 @@ 5.0.3 4.0.1 5.8.2 - 3.8.2 + 3.9.4 1.10.0 diff --git a/src/main/java/com/github/liaochong/myexcel/core/AbstractReadHandler.java b/src/main/java/com/github/liaochong/myexcel/core/AbstractReadHandler.java index 88b844aa..f35e7e06 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/AbstractReadHandler.java +++ b/src/main/java/com/github/liaochong/myexcel/core/AbstractReadHandler.java @@ -158,14 +158,14 @@ private void setConfiguration(Class dataType, boolean isMapType) { ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType); ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration); - List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); - fields.forEach(field -> { - ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class); + List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); + fields.forEach(fieldDefinition -> { + ExcelColumn excelColumn = fieldDefinition.getField().getAnnotation(ExcelColumn.class); if (excelColumn == null) { return; } ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn); - readContext.convertContext.excelColumnMappingMap.put(field, mapping); + readContext.convertContext.excelColumnMappingMap.put(fieldDefinition.getField(), mapping); }); } @@ -183,7 +183,7 @@ protected void setFieldHandlerFunction() { fieldHandler = (colNum, content) -> { FieldDefinition fieldDefinition = fieldDefinitionMap.get(colNum); if (fieldDefinition != null) { - convert(content, currentRow.getRowNum(), colNum, fieldDefinition.getField()); + convert(content, currentRow.getRowNum(), colNum, fieldDefinition); } }; } else { @@ -197,7 +197,7 @@ protected void setFieldHandlerFunction() { boolean isList = fieldDefinition.getField().getType() == List.class; if (!isList && fieldDefinition.getParentFields().isEmpty()) { if (target == null) { - convert(content, currentRow.getRowNum(), colNum, fieldDefinition.getField()); + convert(content, currentRow.getRowNum(), colNum, fieldDefinition); } } else { try { @@ -227,7 +227,7 @@ protected void setFieldHandlerFunction() { } } if (isBase) { - convert(prevObj, content, currentRow.getRowNum(), colNum, fieldDefinition.getField()); + convert(prevObj, content, currentRow.getRowNum(), colNum, fieldDefinition); } else { Object targetParent = ((List) prevObj).get(((List) prevObj).size() - 1); Object targetObj = fieldDefinition.getField().get(targetParent); @@ -235,7 +235,7 @@ protected void setFieldHandlerFunction() { targetObj = new LinkedList<>(); fieldDefinition.getField().set(targetParent, targetObj); } - convert(targetObj, content, currentRow.getRowNum(), colNum, fieldDefinition.getField()); + convert(targetObj, content, currentRow.getRowNum(), colNum, fieldDefinition); } } else { Object value; @@ -245,7 +245,7 @@ protected void setFieldHandlerFunction() { } else { value = ((List) prevObj).get(((List) prevObj).size() - 1); } - convert(value, content, currentRow.getRowNum(), colNum, fieldDefinition.getField()); + convert(value, content, currentRow.getRowNum(), colNum, fieldDefinition); } } } catch (Exception e) { @@ -257,20 +257,20 @@ protected void setFieldHandlerFunction() { } } - protected void convert(Object prevObj, String value, int rowNum, int colNum, Field field) { - if (value == null || field == null) { + protected void convert(Object prevObj, String value, int rowNum, int colNum, FieldDefinition fieldDefinition) { + if (value == null || fieldDefinition.getField() == null) { return; } - readContext.reset(obj, field, value, rowNum, colNum); + readContext.reset(obj, fieldDefinition, value, rowNum, colNum); ReadConverterContext.convert(prevObj, readContext); readContext.revert(); } - protected void convert(String value, int rowNum, int colNum, Field field) { - if (value == null || field == null) { + protected void convert(String value, int rowNum, int colNum, FieldDefinition fieldDefinition) { + if (value == null || fieldDefinition.getField() == null) { return; } - readContext.reset(obj, field, value, rowNum, colNum); + readContext.reset(obj, fieldDefinition, value, rowNum, colNum); ReadConverterContext.convert(obj, readContext); readContext.revert(); } diff --git a/src/main/java/com/github/liaochong/myexcel/core/AbstractSimpleExcelBuilder.java b/src/main/java/com/github/liaochong/myexcel/core/AbstractSimpleExcelBuilder.java index cb95c98f..d8c7a434 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/AbstractSimpleExcelBuilder.java +++ b/src/main/java/com/github/liaochong/myexcel/core/AbstractSimpleExcelBuilder.java @@ -37,6 +37,7 @@ import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer; import com.github.liaochong.myexcel.core.strategy.WidthStrategy; import com.github.liaochong.myexcel.utils.ConfigurationUtil; +import com.github.liaochong.myexcel.utils.FieldDefinition; import com.github.liaochong.myexcel.utils.ReflectUtil; import com.github.liaochong.myexcel.utils.StringUtil; import com.github.liaochong.myexcel.utils.TdUtil; @@ -74,7 +75,7 @@ abstract class AbstractSimpleExcelBuilder { /** * 已排序字段 */ - protected List filteredFields = Collections.emptyList(); + protected List filteredFields = Collections.emptyList(); /** * 标题 */ @@ -129,16 +130,17 @@ public AbstractSimpleExcelBuilder(boolean isCsvBuild) { * @param groups 分组 * @return Field */ - protected List getFilteredFields(ClassFieldContainer classFieldContainer, Class... groups) { + protected List getFilteredFields(ClassFieldContainer classFieldContainer, Class... groups) { ConfigurationUtil.parseConfiguration(classFieldContainer, configuration); this.parseGlobalStyle(); - List preElectionFields = this.getPreElectionFields(classFieldContainer); - List buildFields = this.getGroupFields(preElectionFields, groups); + List preElectionFields = this.getPreElectionFields(classFieldContainer); + List buildFields = this.getGroupFields(preElectionFields, groups); // 初始化标题容器 List titles = new ArrayList<>(buildFields.size()); for (int i = 0, size = buildFields.size(); i < size; i++) { - Field field = buildFields.get(i); + FieldDefinition fieldDefinition = buildFields.get(i); + Field field = fieldDefinition.getField(); ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class); String[] columnStyles = null; if (excelColumn != null) { @@ -164,7 +166,7 @@ protected List getFilteredFields(ClassFieldContainer classFieldContainer, formats.put(i, excelColumn.dateFormatPattern()); } ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn); - excelColumnMappingMap.put(field, mapping); + excelColumnMappingMap.put(fieldDefinition.getField(), mapping); } else { if (configuration.useFieldNameAsTitle) { titles.add(field.getName()); @@ -176,7 +178,7 @@ protected List getFilteredFields(ClassFieldContainer classFieldContainer, setGlobalFormat(i, field); } setTitles(titles); - hasMultiColumn = buildFields.stream().anyMatch(field -> field.isAnnotationPresent(MultiColumn.class)); + hasMultiColumn = buildFields.stream().anyMatch(fieldDefinition -> fieldDefinition.getField().isAnnotationPresent(MultiColumn.class)); return buildFields; } @@ -366,19 +368,19 @@ private void setFormula(int i, Td td) { if (filteredFields.isEmpty()) { return; } - Field field = filteredFields.get(i); - ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(field); + FieldDefinition fieldDefinition = filteredFields.get(i); + ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField()); if (excelColumnMapping != null && excelColumnMapping.formula) { td.formula = true; } } protected void setPrompt(Td td, int index) { - if (filteredFields == null || filteredFields.isEmpty()) { + if (filteredFields.isEmpty()) { return; } - Field field = filteredFields.get(index); - ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(field); + FieldDefinition fieldDefinition = filteredFields.get(index); + ExcelColumnMapping excelColumnMapping = excelColumnMappingMap.get(fieldDefinition.getField()); if (excelColumnMapping != null && excelColumnMapping.promptContainer != null) { td.promptContainer = excelColumnMapping.promptContainer; } @@ -468,22 +470,22 @@ protected void setTitles(List titles) { } } - protected List getGroupFields(List preElectionFields, Class[] groups) { + protected List getGroupFields(List preElectionFields, Class[] groups) { List> selectedGroupList = Objects.nonNull(groups) ? Arrays.stream(groups).filter(Objects::nonNull).collect(Collectors.toList()) : Collections.emptyList(); return preElectionFields.stream() - .filter(field -> (!field.isAnnotationPresent(ExcludeColumn.class) && !field.isAnnotationPresent(IgnoreColumn.class)) && ReflectUtil.isFieldSelected(selectedGroupList, field)) + .filter(fieldDefinition -> (!fieldDefinition.getField().isAnnotationPresent(ExcludeColumn.class) && !fieldDefinition.getField().isAnnotationPresent(IgnoreColumn.class)) && ReflectUtil.isFieldSelected(selectedGroupList, fieldDefinition)) .sorted(ReflectUtil::sortFields) .collect(Collectors.toList()); } - protected List getPreElectionFields(ClassFieldContainer classFieldContainer) { + protected List getPreElectionFields(ClassFieldContainer classFieldContainer) { if (Objects.nonNull(fieldDisplayOrder) && !fieldDisplayOrder.isEmpty()) { this.selfAdaption(); return fieldDisplayOrder.stream() .map(classFieldContainer::getFieldByName) .collect(Collectors.toList()); } - List preElectionFields; + List preElectionFields; if (configuration.includeAllField) { if (configuration.excludeParent) { preElectionFields = classFieldContainer.getDeclaredFields(); @@ -493,7 +495,7 @@ protected List getPreElectionFields(ClassFieldContainer classFieldContain } else { if (configuration.excludeParent) { preElectionFields = classFieldContainer.getDeclaredFields().stream() - .filter(field -> field.isAnnotationPresent(ExcelColumn.class)) + .filter(fieldDefinition -> fieldDefinition.getField().isAnnotationPresent(ExcelColumn.class)) .collect(Collectors.toList()); } else { preElectionFields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); @@ -501,7 +503,7 @@ protected List getPreElectionFields(ClassFieldContainer classFieldContain } if (configuration.ignoreStaticFields) { preElectionFields = preElectionFields.stream() - .filter(field -> !Modifier.isStatic(field.getModifiers())) + .filter(fieldDefinition -> !Modifier.isStatic(fieldDefinition.getField().getModifiers())) .collect(Collectors.toList()); } return preElectionFields; @@ -529,7 +531,7 @@ private void selfAdaption() { * @param 泛型 * @return 结果集 */ - protected List>> getMultiRenderContent(T data, List sortedFields) { + protected List>> getMultiRenderContent(T data, List sortedFields) { List> convertResult = this.getOriginalRenderContent(data, sortedFields); // 获取最大长度 @@ -576,7 +578,7 @@ private void selfAdaption() { * @param 泛型 * @return 结果集 */ - protected LinkedList> getOriginalRenderContent(T data, List sortedFields) { + protected LinkedList> getOriginalRenderContent(T data, List sortedFields) { return sortedFields.stream() .map(field -> { Pair value = WriteConverterContext.convert(field, data, convertContext); diff --git a/src/main/java/com/github/liaochong/myexcel/core/DefaultExcelReader.java b/src/main/java/com/github/liaochong/myexcel/core/DefaultExcelReader.java index 6f472c4d..3211ae70 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/DefaultExcelReader.java +++ b/src/main/java/com/github/liaochong/myexcel/core/DefaultExcelReader.java @@ -115,14 +115,14 @@ private DefaultExcelReader(Class dataType) { ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType); ConfigurationUtil.parseConfiguration(classFieldContainer, readContext.convertContext.configuration); - List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); - fields.forEach(field -> { - ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class); + List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); + fields.forEach(fieldDefinition -> { + ExcelColumn excelColumn = fieldDefinition.getField().getAnnotation(ExcelColumn.class); if (excelColumn == null) { return; } ExcelColumnMapping mapping = ExcelColumnMapping.mapping(excelColumn); - readContext.convertContext.excelColumnMappingMap.put(field, mapping); + readContext.convertContext.excelColumnMappingMap.put(fieldDefinition.getField(), mapping); }); } } @@ -423,7 +423,7 @@ private T instanceObj(Map fieldDefinitionMap, DataForm return; } content = trim.apply(content); - readContext.reset(obj, fieldDefinition.getField(), content, row.getRowNum(), index); + readContext.reset(obj, fieldDefinition, content, row.getRowNum(), index); ReadConverterContext.convert(obj, readContext); }); return obj; diff --git a/src/main/java/com/github/liaochong/myexcel/core/XSSFSheetXMLHandler.java b/src/main/java/com/github/liaochong/myexcel/core/XSSFSheetXMLHandler.java index bbcdc1a7..70fcaaf7 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/XSSFSheetXMLHandler.java +++ b/src/main/java/com/github/liaochong/myexcel/core/XSSFSheetXMLHandler.java @@ -141,7 +141,6 @@ else if ("c".equals(localName)) { this.nextDataType = xssfDataType.NUMBER; cellRef = attributes.getValue("r"); String cellType = attributes.getValue("t"); - String cellStyleStr = attributes.getValue("s"); if ("b".equals(cellType)) nextDataType = xssfDataType.BOOLEAN; else if ("e".equals(cellType)) diff --git a/src/main/java/com/github/liaochong/myexcel/core/context/ReadContext.java b/src/main/java/com/github/liaochong/myexcel/core/context/ReadContext.java index 143e71d9..3a70cec6 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/context/ReadContext.java +++ b/src/main/java/com/github/liaochong/myexcel/core/context/ReadContext.java @@ -16,6 +16,7 @@ import com.github.liaochong.myexcel.core.SaxExcelReader; import com.github.liaochong.myexcel.core.converter.ConvertContext; +import com.github.liaochong.myexcel.utils.FieldDefinition; import java.lang.reflect.Field; @@ -31,6 +32,8 @@ public class ReadContext { private Field field; + private FieldDefinition fieldDefinition; + private String val; private int rowNum; @@ -50,9 +53,10 @@ public ReadContext(ConvertContext convertContext) { this.convertContext = convertContext; } - public void reset(T object, Field field, String val, int rowNum, int colNum) { + public void reset(T object, FieldDefinition fieldDefinition, String val, int rowNum, int colNum) { this.object = object; - this.field = field; + this.fieldDefinition = fieldDefinition; + this.field = fieldDefinition.getField(); this.val = val; this.rowNum = rowNum; this.colNum = colNum; @@ -66,10 +70,6 @@ public T getObject() { return this.object; } - public Field getField() { - return this.field; - } - public String getVal() { return this.val; } @@ -86,10 +86,6 @@ public void setObject(T object) { this.object = object; } - public void setField(Field field) { - this.field = field; - } - public void setVal(String val) { this.val = val; } @@ -109,4 +105,20 @@ public Hyperlink getHyperlink() { public void setHyperlink(Hyperlink hyperlink) { this.hyperlink = hyperlink; } + + public FieldDefinition getFieldDefinition() { + return fieldDefinition; + } + + public void setFieldDefinition(FieldDefinition fieldDefinition) { + this.fieldDefinition = fieldDefinition; + } + + public Field getField() { + return field; + } + + public void setField(Field field) { + this.field = field; + } } diff --git a/src/main/java/com/github/liaochong/myexcel/core/converter/ReadConverterContext.java b/src/main/java/com/github/liaochong/myexcel/core/converter/ReadConverterContext.java index c1f7a656..e0837a02 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/converter/ReadConverterContext.java +++ b/src/main/java/com/github/liaochong/myexcel/core/converter/ReadConverterContext.java @@ -31,9 +31,11 @@ import com.github.liaochong.myexcel.core.converter.reader.TimestampReadConverter; import com.github.liaochong.myexcel.exception.ExcelReadException; import com.github.liaochong.myexcel.exception.SaxReadException; +import com.github.liaochong.myexcel.utils.FieldDefinition; import com.github.liaochong.myexcel.utils.PropertyUtil; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Timestamp; @@ -156,7 +158,13 @@ public static void convert(Object obj, ReadContext readContext) { if (obj instanceof List) { ((List) obj).add(value); } else { - readContext.getField().set(obj, value); + FieldDefinition fieldDefinition = readContext.getFieldDefinition(); + Method setMethod = fieldDefinition.getSetMethod(); + if (setMethod != null) { + setMethod.invoke(obj, value); + } else { + fieldDefinition.getField().set(obj, value); + } } } catch (Exception e) { throw new SaxReadException("Failed to set the " + readContext.getField().getDeclaringClass().getName() + "#" + readContext.getField().getName() + " field value to " + readContext.getVal(), e); diff --git a/src/main/java/com/github/liaochong/myexcel/core/converter/WriteConverterContext.java b/src/main/java/com/github/liaochong/myexcel/core/converter/WriteConverterContext.java index b7cdd2d7..271c93af 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/converter/WriteConverterContext.java +++ b/src/main/java/com/github/liaochong/myexcel/core/converter/WriteConverterContext.java @@ -31,6 +31,7 @@ import com.github.liaochong.myexcel.core.converter.writer.MultiWriteConverter; import com.github.liaochong.myexcel.core.converter.writer.OriginalWriteConverter; import com.github.liaochong.myexcel.core.converter.writer.StringWriteConverter; +import com.github.liaochong.myexcel.utils.FieldDefinition; import com.github.liaochong.myexcel.utils.ReflectUtil; import java.lang.reflect.Field; @@ -73,13 +74,13 @@ public static synchronized void registering(WriteConverter... writeConverters) { } } - public static Pair convert(Field field, Object object, ConvertContext convertContext) { - Object result = ReflectUtil.getFieldValue(object, field); + public static Pair convert(FieldDefinition fieldDefinition, Object object, ConvertContext convertContext) { + Object result = ReflectUtil.getFieldValue(object, fieldDefinition); if (result == null) { return Constants.NULL_PAIR; } - WriteConverter writeConverter = getWriteConverter(field, field.getType(), result, convertContext, WRITE_CONVERTER_CONTAINER); - return writeConverter.convert(field, field.getType(), result, convertContext); + WriteConverter writeConverter = getWriteConverter(fieldDefinition.getField(), fieldDefinition.getField().getType(), result, convertContext, WRITE_CONVERTER_CONTAINER); + return writeConverter.convert(fieldDefinition.getField(), fieldDefinition.getField().getType(), result, convertContext); } public static WriteConverter getWriteConverter(Field field, Class fieldType, Object result, ConvertContext convertContext, List> writeConverterContainer) { diff --git a/src/main/java/com/github/liaochong/myexcel/core/reflect/ClassFieldContainer.java b/src/main/java/com/github/liaochong/myexcel/core/reflect/ClassFieldContainer.java index a38a381d..788424ff 100644 --- a/src/main/java/com/github/liaochong/myexcel/core/reflect/ClassFieldContainer.java +++ b/src/main/java/com/github/liaochong/myexcel/core/reflect/ClassFieldContainer.java @@ -15,8 +15,9 @@ */ package com.github.liaochong.myexcel.core.reflect; +import com.github.liaochong.myexcel.utils.FieldDefinition; + import java.lang.annotation.Annotation; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -32,31 +33,31 @@ public class ClassFieldContainer { private Class clazz; - private final List declaredFields = new ArrayList<>(); + private final List declaredFields = new ArrayList<>(); - private final Map fieldMap = new HashMap<>(); + private final Map fieldMap = new HashMap<>(); private ClassFieldContainer parent; - public Field getFieldByName(String fieldName) { + public FieldDefinition getFieldByName(String fieldName) { return this.getFieldByName(fieldName, this); } @SafeVarargs - public final List getFieldsByAnnotation(Class... annotationClazzs) { + public final List getFieldsByAnnotation(Class... annotationClazzs) { Objects.requireNonNull(annotationClazzs); - List annotationFields = new ArrayList<>(); + List annotationFields = new ArrayList<>(); this.getFieldsByAnnotation(this, annotationFields, annotationClazzs); return annotationFields; } - public List getFields() { - List fields = new ArrayList<>(); + public List getFields() { + List fields = new ArrayList<>(); this.getFieldsByContainer(this, fields); return fields; } - private void getFieldsByContainer(ClassFieldContainer classFieldContainer, List fields) { + private void getFieldsByContainer(ClassFieldContainer classFieldContainer, List fields) { ClassFieldContainer parentContainer = classFieldContainer.getParent(); if (parentContainer != null) { this.getFieldsByContainer(parentContainer, fields); @@ -65,14 +66,14 @@ private void getFieldsByContainer(ClassFieldContainer classFieldContainer, List< } @SafeVarargs - private final void getFieldsByAnnotation(ClassFieldContainer classFieldContainer, List annotationFieldContainer, Class... annotationClazzs) { + private final void getFieldsByAnnotation(ClassFieldContainer classFieldContainer, List annotationFieldContainer, Class... annotationClazzs) { ClassFieldContainer parentContainer = classFieldContainer.getParent(); if (parentContainer != null) { this.getFieldsByAnnotation(parentContainer, annotationFieldContainer, annotationClazzs); } - List annotationFields = classFieldContainer.declaredFields.stream().filter(field -> { + List annotationFields = classFieldContainer.declaredFields.stream().filter(fieldDefinition -> { for (Class annotationClazz : annotationClazzs) { - boolean isAnnotationPresent = field.isAnnotationPresent(annotationClazz); + boolean isAnnotationPresent = fieldDefinition.getField().isAnnotationPresent(annotationClazz); if (isAnnotationPresent) { return true; } @@ -82,24 +83,24 @@ private final void getFieldsByAnnotation(ClassFieldContainer classFieldContainer filterFields(annotationFields, annotationFieldContainer); } - private void filterFields(List declaredFields, List fieldContainer) { + private void filterFields(List declaredFields, List fieldDefinitionsContainer) { to: - for (Field field : declaredFields) { - for (int j = 0; j < fieldContainer.size(); j++) { - Field f = fieldContainer.get(j); - if (f.getName().equals(field.getName())) { - fieldContainer.set(j, field); + for (FieldDefinition fieldDefinition : declaredFields) { + for (int j = 0; j < fieldDefinitionsContainer.size(); j++) { + FieldDefinition f = fieldDefinitionsContainer.get(j); + if (f.getField().getName().equals(fieldDefinition.getField().getName())) { + fieldDefinitionsContainer.set(j, fieldDefinition); continue to; } } - fieldContainer.add(field); + fieldDefinitionsContainer.add(fieldDefinition); } } - private Field getFieldByName(String fieldName, ClassFieldContainer container) { - Field field = container.getFieldMap().get(fieldName); - if (field != null) { - return field; + private FieldDefinition getFieldByName(String fieldName, ClassFieldContainer container) { + FieldDefinition fieldDefinition = container.getFieldMap().get(fieldName); + if (fieldDefinition != null) { + return fieldDefinition; } ClassFieldContainer parentContainer = container.getParent(); if (parentContainer == null) { @@ -112,11 +113,11 @@ public Class getClazz() { return this.clazz; } - public List getDeclaredFields() { + public List getDeclaredFields() { return this.declaredFields; } - public Map getFieldMap() { + public Map getFieldMap() { return this.fieldMap; } diff --git a/src/main/java/com/github/liaochong/myexcel/utils/FieldDefinition.java b/src/main/java/com/github/liaochong/myexcel/utils/FieldDefinition.java index ee7bf408..a3cfcbab 100644 --- a/src/main/java/com/github/liaochong/myexcel/utils/FieldDefinition.java +++ b/src/main/java/com/github/liaochong/myexcel/utils/FieldDefinition.java @@ -14,7 +14,9 @@ */ package com.github.liaochong.myexcel.utils; +import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.List; /** @@ -27,8 +29,19 @@ public class FieldDefinition { private List parentFields; + private Method getMethod; + + private Method setMethod; + public FieldDefinition(Field field) { this.field = field; + try { + PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), field.getDeclaringClass()); + this.getMethod = propertyDescriptor.getReadMethod(); + this.setMethod = propertyDescriptor.getWriteMethod(); + } catch (Exception e) { + // do nothing + } } public Field getField() { @@ -46,4 +59,20 @@ public List getParentFields() { public void setParentFields(List parentFields) { this.parentFields = parentFields; } + + public Method getGetMethod() { + return getMethod; + } + + public void setGetMethod(Method getMethod) { + this.getMethod = getMethod; + } + + public Method getSetMethod() { + return setMethod; + } + + public void setSetMethod(Method setMethod) { + this.setMethod = setMethod; + } } diff --git a/src/main/java/com/github/liaochong/myexcel/utils/ReflectUtil.java b/src/main/java/com/github/liaochong/myexcel/utils/ReflectUtil.java index 4614f031..db3b679c 100644 --- a/src/main/java/com/github/liaochong/myexcel/utils/ReflectUtil.java +++ b/src/main/java/com/github/liaochong/myexcel/utils/ReflectUtil.java @@ -22,6 +22,7 @@ import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.time.LocalDate; @@ -75,23 +76,23 @@ public static Map getFieldDefinitionMapOfExcelColumn(C private static void getFieldDefinition(Class dataType, Map fieldDefinitionMap, List parentFields, int level) { ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType); - List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class, MultiColumn.class); + List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class, MultiColumn.class); if (level == 0 && fields.isEmpty()) { // If no field contains an ExcelColumn annotation, all fields are read in the default order - List allFields = classFieldContainer.getFields(); + List allFields = classFieldContainer.getFields(); for (int i = 0, size = allFields.size(); i < size; i++) { - FieldDefinition fieldDefinition = new FieldDefinition(allFields.get(i)); - fieldDefinitionMap.put(i, fieldDefinition); + fieldDefinitionMap.put(i, allFields.get(i)); } } else { List topParentFields = new LinkedList<>(); if (parentFields != null) { topParentFields.addAll(parentFields); } - for (Field field : fields) { + for (FieldDefinition fieldDefinition : fields) { if (level == 0) { parentFields = new LinkedList<>(); } + Field field = fieldDefinition.getField(); if (field.isAnnotationPresent(MultiColumn.class)) { MultiColumn multiColumn = field.getAnnotation(MultiColumn.class); List childrenParentFields = new LinkedList<>(topParentFields); @@ -102,8 +103,8 @@ private static void getFieldDefinition(Class dataType, Map dataType, Map getFieldMapOfTitleExcelColumn(Class dataType return fieldMap; } ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType); - List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); + List fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class); if (fields.isEmpty()) { throw new IllegalStateException("There is no field with @ExcelColumn"); } fieldMap = new HashMap<>(fields.size()); - for (Field field : fields) { + for (FieldDefinition fieldDefinition : fields) { + Field field = fieldDefinition.getField(); ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class); String title = excelColumn.title(); if (title.isEmpty()) { @@ -166,16 +168,21 @@ public static Map getFieldMapOfTitleExcelColumn(Class dataType /** * 根据对象以及指定字段,获取字段的值 * - * @param o 对象 - * @param field 指定字段 + * @param o 对象 + * @param fieldDefinition 指定字段 * @return 字段值 */ - public static Object getFieldValue(Object o, Field field) { - if (o == null || field == null) { + public static Object getFieldValue(Object o, FieldDefinition fieldDefinition) { + if (o == null || fieldDefinition == null) { return null; } try { - return field.get(o); + Method getMethod = fieldDefinition.getGetMethod(); + if (getMethod != null) { + return getMethod.invoke(o); + } else { + return fieldDefinition.getField().get(o); + } } catch (Exception e) { throw new RuntimeException(e); } @@ -186,8 +193,9 @@ private static void getAllFieldsOfClass(Class clazz, ClassFieldContainer cont Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); - container.getDeclaredFields().add(field); - container.getFieldMap().put(field.getName(), field); + FieldDefinition fieldDefinition = new FieldDefinition(field); + container.getDeclaredFields().add(fieldDefinition); + container.getFieldMap().put(field.getName(), fieldDefinition); } if (clazz.getSuperclass() != null) { ClassFieldContainer parentContainer = new ClassFieldContainer(); @@ -214,9 +222,9 @@ public static boolean isDate(Class clazz) { return clazz == Date.class || clazz == LocalDateTime.class || clazz == LocalDate.class || clazz == LocalTime.class; } - public static int sortFields(Field field1, Field field2) { - ExcelColumn excelColumn1 = field1.getAnnotation(ExcelColumn.class); - ExcelColumn excelColumn2 = field2.getAnnotation(ExcelColumn.class); + public static int sortFields(FieldDefinition fieldDefinition1, FieldDefinition fieldDefinition2) { + ExcelColumn excelColumn1 = fieldDefinition1.getField().getAnnotation(ExcelColumn.class); + ExcelColumn excelColumn2 = fieldDefinition2.getField().getAnnotation(ExcelColumn.class); if (excelColumn1 == null && excelColumn2 == null) { return 0; } @@ -235,11 +243,11 @@ public static int sortFields(Field field1, Field field2) { return order1 > order2 ? 1 : -1; } - public static boolean isFieldSelected(List> selectedGroupList, Field field) { + public static boolean isFieldSelected(List> selectedGroupList, FieldDefinition fieldDefinition) { if (selectedGroupList.isEmpty()) { return true; } - ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class); + ExcelColumn excelColumn = fieldDefinition.getField().getAnnotation(ExcelColumn.class); if (excelColumn == null) { return false; }