java遍曆實體類的屬性和資料類型以及屬性值

來源:互聯網
上載者:User

標籤:model   targe   length   java   double   span   div   param   sys   

遍曆實體類的樹形和資料類型一級屬性值

    /**     * 遍曆實體類的屬性和資料類型以及屬性值     * @param model     * @throws NoSuchMethodException     * @throws IllegalAccessException     * @throws IllegalArgumentException     * @throws InvocationTargetException     */    public static void reflectTest(Object model) throws NoSuchMethodException,                    IllegalAccessException, IllegalArgumentException,                    InvocationTargetException {        // 擷取實體類的所有屬性,返回Field數組        Field[] field = model.getClass().getDeclaredFields();        // 遍曆所有屬性        for (int j = 0; j < field.length; j++) {                // 擷取屬性的名字                String name = field[j].getName();                // 將屬性的首字元大寫,方便構造get,set方法                name = name.substring(0, 1).toUpperCase() + name.substring(1);                // 擷取屬性的類型                String type = field[j].getGenericType().toString();                // 如果type是類類型,則前麵包含"class ",後面跟類名                System.out.println("屬性為:" + name);                if (type.equals("class java.lang.String")) {                        Method m = model.getClass().getMethod("get" + name);                        // 調用getter方法擷取屬性值                        String value = (String) m.invoke(model);                        System.out.println("資料類型為:String");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }                if (type.equals("class java.lang.Integer")) {                        Method m = model.getClass().getMethod("get" + name);                        Integer value = (Integer) m.invoke(model);                        System.out.println("資料類型為:Integer");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }                if (type.equals("class java.lang.Short")) {                        Method m = model.getClass().getMethod("get" + name);                        Short value = (Short) m.invoke(model);                        System.out.println("資料類型為:Short");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }                if (type.equals("class java.lang.Double")) {                        Method m = model.getClass().getMethod("get" + name);                        Double value = (Double) m.invoke(model);                        System.out.println("資料類型為:Double");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }                if (type.equals("class java.lang.Boolean")) {                        Method m = model.getClass().getMethod("get" + name);                        Boolean value = (Boolean) m.invoke(model);                        System.out.println("資料類型為:Boolean");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }                if (type.equals("class java.util.Date")) {                        Method m = model.getClass().getMethod("get" + name);                        Date value = (Date) m.invoke(model);                        System.out.println("資料類型為:Date");                        if (value != null) {                                System.out.println("屬性值為:" + value);                        } else {                                System.out.println("屬性值為:空");                        }                }        }    }

由於我的實體bean裡有double類型,我又不想用其封裝類的方法擷取值,於是我在他的基礎上又加入了一段代碼,

                if (type.equals("double")) {                    Method m = model.getClass().getMethod("get" + name);                    double value = (double) m.invoke(model);                    System.out.println("資料類型為:double");                    if (value >0) {                            System.out.println("屬性值為:" + value);                    } else {                            System.out.println("屬性值為:空");                    }                }                System.out.println("屬性類型為:"+type);

這樣一來,就可以知道屬性類型是什麼,可以很好的加判斷語句了。當然原文代碼還有很多值得最佳化的地方,我這裡就沒有進行最佳化,準備等我將它與poi匯出整合之後再來最佳化。

java遍曆實體類的屬性和資料類型以及屬性值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.