Search in sources :

Example 6 with ExcelColumn

use of com.github.liaochong.myexcel.core.annotation.ExcelColumn in project myexcel by liaochong.

the class ReflectUtil method getFieldMapOfExcelColumn.

public static Map<Integer, Field> getFieldMapOfExcelColumn(Class<?> dataType) {
    if (dataType == Map.class) {
        return Collections.emptyMap();
    }
    Map<Integer, Field> fieldMap = FIELD_CACHE.get(dataType);
    if (fieldMap != null) {
        return fieldMap;
    }
    ClassFieldContainer classFieldContainer = ReflectUtil.getAllFieldsOfClass(dataType);
    List<Field> fields = classFieldContainer.getFieldsByAnnotation(ExcelColumn.class);
    if (fields.isEmpty()) {
        // If no field contains an ExcelColumn annotation, all fields are read in the default order
        List<Field> allFields = classFieldContainer.getFields();
        fieldMap = new HashMap<>(allFields.size());
        for (int i = 0, size = allFields.size(); i < size; i++) {
            fieldMap.put(i, allFields.get(i));
        }
    } else {
        fieldMap = new HashMap<>(fields.size());
        for (Field field : fields) {
            ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
            int index = excelColumn.index();
            if (index < 0) {
                continue;
            }
            Field f = fieldMap.get(index);
            if (Objects.nonNull(f)) {
                throw new IllegalStateException("Index cannot be repeated: " + index + ". Please check it.");
            }
            field.setAccessible(true);
            fieldMap.put(index, field);
        }
    }
    FIELD_CACHE.cache(dataType, fieldMap);
    return fieldMap;
}
Also used : BigInteger(java.math.BigInteger) ClassFieldContainer(com.github.liaochong.myexcel.core.reflect.ClassFieldContainer) Field(java.lang.reflect.Field) ExcelColumn(com.github.liaochong.myexcel.core.annotation.ExcelColumn)

Aggregations

ExcelColumn (com.github.liaochong.myexcel.core.annotation.ExcelColumn)6 ClassFieldContainer (com.github.liaochong.myexcel.core.reflect.ClassFieldContainer)4 Field (java.lang.reflect.Field)4 ExcludeColumn (com.github.liaochong.myexcel.core.annotation.ExcludeColumn)1 IgnoreColumn (com.github.liaochong.myexcel.core.annotation.IgnoreColumn)1 MultiColumn (com.github.liaochong.myexcel.core.annotation.MultiColumn)1 BooleanDropDownList (com.github.liaochong.myexcel.core.constant.BooleanDropDownList)1 Constants (com.github.liaochong.myexcel.core.constant.Constants)1 DropDownList (com.github.liaochong.myexcel.core.constant.DropDownList)1 ImageFile (com.github.liaochong.myexcel.core.constant.ImageFile)1 LinkEmail (com.github.liaochong.myexcel.core.constant.LinkEmail)1 LinkUrl (com.github.liaochong.myexcel.core.constant.LinkUrl)1 NumberDropDownList (com.github.liaochong.myexcel.core.constant.NumberDropDownList)1 Pair (com.github.liaochong.myexcel.core.container.Pair)1 ConvertContext (com.github.liaochong.myexcel.core.converter.ConvertContext)1 WriteConverterContext (com.github.liaochong.myexcel.core.converter.WriteConverterContext)1 ContentTypeEnum (com.github.liaochong.myexcel.core.parser.ContentTypeEnum)1 StyleParser (com.github.liaochong.myexcel.core.parser.StyleParser)1 Table (com.github.liaochong.myexcel.core.parser.Table)1 Td (com.github.liaochong.myexcel.core.parser.Td)1