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());
    }

你可能感兴趣的:(Java,java)