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;
}
Aggregations