Java反射获取属性和值

Java反射获取属性和值

经常会碰到需要反射的情景,处理好了数据,不知道对应哪个属性,就要用到反射

// An highlighted block
    public void test() {
        List<ExcelMappingDatagovTemplate> excelMappingList = excelMappingService.getExcelMapping();
        ExcelMappingDatagovTemplate excel = excelMappingList.get(0);
        try {

            Field[] declaredFields = excel.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String attributeName = declaredField.getName();
                String type = declaredField.getGenericType().toString();
                
                //对应类型要一个一个测试,也许是包装类,也许是基本数据类型
                if (type.equals("class java.lang.String")) {

                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    String value = (String) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value);
                    }
                }
                if (type.equals("class java.lang.Integer")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    Integer value = (Integer) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value);
                    }
                }
                if (type.equals("int")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    int value = (int) m.invoke(excel);

                        System.out.println("attribute value:" + value);

                }
                if (type.equals("class java.lang.Short")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    Short value = (Short) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value);
                    }
                }
                if (type.equals("class java.lang.Double")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    Double value = (Double) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value);
                    }
                }
                if (type.equals("class java.lang.Boolean")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    Boolean value = (Boolean) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value);
                    }
                }
                if (type.equals("class java.util.Date")) {
                    Method m = excel.getClass().getMethod("get" + firstUpperCase(attributeName));
                    Date value = (Date) m.invoke(excel);
                    if (value != null) {
                        System.out.println("attribute value:" + value.toLocaleString());
                    }
                }
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
    
    //方法名首字母大写
    public static String firstUpperCase(String str) {
        return str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值