CSDNRGY 2017-05-17 08:09 采纳率: 87.5%
浏览 2095
已采纳

如何根据注解动态调用set方法?

如何根据注解动态调用set方法?
我想实现,excel表格动态注入到List中
这是表格数据和Emp类
图片说明
现在已经可以让,表格的头和Emp类的注解的进行关联
但是我不知道如何实现动态的调用emp对象的set方法
即使不调用set方法,直接通过属性注入也行
但是我不知道怎么写了

 public class Demo0<T> {

    public static void main(String[] args) throws Exception {
        List<Emp> emps = new ArrayList<>();
        //excel表
        Workbook wb = new XSSFWorkbook("d:/员工.xlsx");
        int sheetLength = wb.getNumberOfSheets();
        //遍历sheet
        for (int i = 0; i < sheetLength; i++) {
            Sheet sheet = wb.getSheetAt(i);
            int rowLength = sheet.getPhysicalNumberOfRows();
            //遍历行
            for (int j = 0; j < rowLength; j++) {
                Emp emp = new Emp();
                Row row = sheet.getRow(j);
                int cellLength = row.getPhysicalNumberOfCells();
                //遍历列
                for (int k = 0; k < cellLength; k++) {
                    Cell cell = row.getCell(k);
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue+"\t");
                    /*----------开始自动关联了-----------*/
                    //获取当前sheet的第一行,也就是excel的表头
                    Row title = sheet.getRow(0);
                    //获取当前列的表头的值
                    Cell titleCell = title.getCell(k);
                    String titleCellValue = titleCell.getStringCellValue();
                    //如何自动映射?
                    //获取Emp类的属性
                    Field[] fields = Emp.class.getDeclaredFields();
                    for (int l = 0; l < fields.length; l++) {
                        Field field = fields[l];
                        //根据属性,获取Excel注解
                        ExcelField ef = field.getDeclaredAnnotation(ExcelField.class);
                        //获取注解的值
                        String anTitle = ef.title();

                        //注解的值和excel标题的值相同的时候
                        //就把excel中当前列的值,注入到emp对象对应的属性中
                        /*******重点*****/
                        /*******如何根据注解,动态调用set方法?*****/
                        if(titleCellValue.equals(anTitle)){
                            emp.setEmpId(cellValue);
                            continue;
                        }
                        if(titleCellValue.equals(anTitle)){
                            emp.setEname(cellValue);
                            continue;
                        }
                    }
                }
                emps.add(emp);
                System.out.println();
            }
        }
        System.out.println(emps);
    }
}
 public class Emp {

    @ExcelField(title = "员工姓名")
    public String ename;
    @ExcelField(title = "员工编号")
    public String empId;

    public Emp() {
    }

    public Emp(String empId, String ename) {
        this.empId = empId;
        this.ename = ename;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Override
    public String toString() {
        return "Emp [ename=" + ename + ", empId=" + empId + "]";
    }

}
 @Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelField {
    String title();
}
  • 写回答

1条回答 默认 最新

  • 时间在这里停顿 2017-05-17 09:34
    关注

    你可以试一下把Field换成Method,invoke可以调用该方法表示的底层方法

    //动态加载类
    Class cls=Class.forName(Emp);
    //动态获取全部方法信息
    Method[] ary=cls.getDeclaredMethods();
    //创建类的实例
    Object obj = cls.newInstance();
    //迭代全部方法查找以set为开头的方法
    for (Method method : ary) {
        ExcelField ann=method.getAnnotation(ExcelField.class);
        if(titleCellValue.equals(ann.title())){
            //动态执行方法
            method.invoke(obj, cellValue);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题