All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.tool.file.excel.EntityAnnotationUtils Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy