java反射动态调用get方法

前言:

        在开发过程中遇到一个问题,要把一个对象的属性和值转换拆分成另一个单独的Vo,当时想到传统的办法就是根据对象有多少个属性就new多少个vo,然后获取其get方法赋值。但如果对象有很多个属性,就有一堆代码,很糟糕和浪费时间,于是想到上次开发中遇到了java反射获取对象的属性名,所以这次也可以用java反射动态调用get方法。

 过程:

实体类:

@Data
public class TimeDivisionPo {

    @ExcelProperty("时间")
    private String dyTime;

    @ExcelProperty("00:01~01:00")
    private String one;

    @ExcelProperty("01:01~02:00")
    private String two;

    @ExcelProperty("02:01~03:00")
    private String three;

    @ExcelProperty("03:01~04:00")
    private String four;

    @ExcelProperty("04:01~05:00")
    private String five;

    @ExcelProperty("05:01~06:00")
    private String six;

    @ExcelProperty("06:01~07:00")
    private String seven;

    @ExcelProperty("07:01~08:00")
    private String eight;

    @ExcelProperty("08:01~09:00")
    private String nine;

    @ExcelProperty("09:01~10:00")
    private String ten;

    @ExcelProperty("10:01~11:00")
    private String eleven;

    @ExcelProperty("11:01~12:00")
    private String twelve;

    @ExcelProperty("12:01~13:00")
    private String thirteen;

    @ExcelProperty("13:01~14:00")
    private String fourteen;

    @ExcelProperty("14:01~15:00")
    private String fifteen;

    @ExcelProperty("15:01~16:00")
    private String sixteen;

    @ExcelProperty("16:01~17:00")
    private String seventeen;

    @ExcelProperty("17:01~18:00")
    private String eighteen;

    @ExcelProperty("18:01~19:00")
    private String nineteen;

    @ExcelProperty("19:01~20:00")
    private String twenty;

    @ExcelProperty("20:01~21:00")
    private String twentyOne;

    @ExcelProperty("21:01~22:00")
    private String twentyTwo;

    @ExcelProperty("22:01~23:00")
    private String twentyThree;

    @ExcelProperty("23:01~24:00")
    private String twentyFour;

}

要转换的VO类:

@Data
public class UserOperationVo {

    private String dyTime;

    private String time;

    private String name;

    private String data;
}

转换过程:

 public List<UserOperationVo> getTimeDivisionList(ParamsVo params) throws Exception {
       List<TimeDivisionPo> list= operationViewMapper.selectTimeDivisionList(params);

        Map<String,String> map= ImmutableMap.<String,String>builder()
                .put("00:01~01:00","One")
                .put("01:01~02:00","Two")
                .put("02:01~03:00","Three")
                .put("03:01~04:00","Four")
                .put("04:01~05:00","Five")
                .put("05:01~06:00","Six")
                .put("06:01~07:00","Seven")
                .put("07:01~08:00","Eight")
                .put("08:01~09:00","Nine")
                .put("09:01~10:00","Ten")
                .put("10:01~11:00","Eleven")
                .put("11:01~12:00","Twelve")
                .put("12:01~13:00","Thirteen")
                .put("13:01~14:00","Fourteen")
                .put("14:01~15:00","Fifteen")
                .put("15:01~16:00","Sixteen")
                .put("16:01~17:00","Seventeen")
                .put("17:01~18:00","Eighteen")
                .put("18:01~19:00","Nineteen")
                .put("19:01~20:00","Twenty")
                .put("20:01~21:00","TwentyOne")
                .put("21:01~22:00","TwentyTwo")
                .put("22:01~23:00","TwentyThree")
                .put("23:01~24:00","TwentyFour")
                .build();

        List<UserOperationVo> dataList=new ArrayList<>();

        for (TimeDivisionPo po:list){

            Class<?> aclass= TimeDivisionPo.class;

            Set<String> keys=map.keySet();

            for (String str:keys){
                UserOperationVo vo=new UserOperationVo();
                vo.setDyTime(po.getDyTime());
                vo.setTime(str);
                vo.setName(po.getApp());
                //拼接get方法,因为我需要将方法和时间对应起来,所以就直接用map对应起来,也可以用上篇文字,动态获取属性和其注解,遍历调用办法
                String invokeMethodName="get"+map.get(str);
                vo.setData((String) 
                //调用方法
                vo.setData((String) aclass.getMethod(invokeMethodName).invoke(po));
                dataList.add(vo);
            }

        }

        //根据时间进行分组
        Map<String, List<UserOperationVo>> maps = dataList.stream()
                .sorted(Comparator.comparing(UserOperationVo::getTime))
                .collect(Collectors.groupingBy(UserOperationVo::getTime));


        return dataList;
    }

结果展示:

 最后,参考了下面这篇文章,上面说的直接获取其属性名来调用方法的可前往这篇文章观看,自己复写一遍只是为了记录和以后查看。

java使用反射调用对象get方法-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值