top.doudou.common.tool.file.excel.EntityAnnotationUtils Maven / Gradle / Ivy
package top.doudou.common.tool.file.excel;
import com.google.common.collect.Maps;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
/**
* 获取实体类注解属性
* @author 傻男人<[email protected]>
* @create 2020-3-26
*/
public class EntityAnnotationUtils {
/**
* 获取实体类注解的属性与对应的字段名
* @param target
* @param
* @return
*/
public static LinkedHashMap getAnnotationProperty(Class target) {
LinkedHashMap map = Maps.newLinkedHashMap();
Field[] fields = target.getDeclaredFields();
for (Field field : fields) {
ExcelMapping annotation = field.getAnnotation(ExcelMapping.class);
if(annotation != null){
map.put(annotation.value(),field.getName());
}
}
return map;
}
/**
* 获取实体类对应的写入属性
* @param target
* @param
* @return
*/
public static LinkedHashMap getExcelWriteProperty(Class target) {
LinkedHashMap map = Maps.newLinkedHashMap();
Field[] fields = target.getDeclaredFields();
boolean flag = false;
int position = 0;
for (Field field : fields) {
ExcelMapping annotation = field.getAnnotation(ExcelMapping.class);
if(annotation != null){
ExcelWriteDto excelWriteDto = new ExcelWriteDto();
excelWriteDto.setColumnWidth(annotation.columnWidth());
excelWriteDto.setFieldName(field.getName());
if(!flag){
if(annotation.position() == 0){
excelWriteDto.setPosition(position);
flag = true;
}
}else {
excelWriteDto.setPosition(position);
}
excelWriteDto.setValue(annotation.value());
map.put(excelWriteDto.getPosition(),excelWriteDto);
position ++;
}
}
return map;
}
}