您的位置:首页 > 编程语言 > Java开发

java遍历实体类的属性和数据类型以及属性值

2016-02-28 15:39 525 查看
原址:点击打开链接

/** 

 * 遍历实体类的属性和数据类型以及属性值 

 * @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("属性值为:空");  

                    }  

            }  

    }  

}  

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);
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: