java反射遍歷實體類屬性和類型,并賦值和獲取值的簡單方法
更新時間:2017年04月13日 10:01:54 投稿:jingxian
下面小編就為大家?guī)硪黄猨ava反射遍歷實體類屬性和類型,并賦值和獲取值的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; /** * 獲取實體類型的屬性名和類型 * @param model 為實體類 * @author kou 為傳入?yún)?shù) */ public class GetModelNameAndType { public static void testReflect(Object model) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { // 獲取實體類的所有屬性,返回Field數(shù)組 Field[] field = model.getClass().getDeclaredFields(); // 獲取屬性的名字 String[] modelName = new String[field.length]; String[] modelType = new String[field.length]; for (int i = 0; i < field.length; i++) { // 獲取屬性的名字 String name = field[i].getName(); modelName[i] = name; // 獲取屬性類型 String type = field[i].getGenericType().toString(); modelType[i] = type; //關(guān)鍵。。??稍L問私有變量 field[i].setAccessible(true); //給屬性設(shè)置 field[i].set(model, field[i].getType().getConstructor(field[i].getType()).newInstance("kou")); // 將屬性的首字母大寫 name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1) .toUpperCase()); if (type.equals("class java.lang.String")) { // 如果type是類類型,則前面包含"class ",后面跟類名 Method m = model.getClass().getMethod("get" + name); // 調(diào)用getter方法獲取屬性值 String value = (String) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Integer")) { Method m = model.getClass().getMethod("get" + name); Integer value = (Integer) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Short")) { Method m = model.getClass().getMethod("get" + name); Short value = (Short) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Double")) { Method m = model.getClass().getMethod("get" + name); Double value = (Double) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Boolean")) { Method m = model.getClass().getMethod("get" + name); Boolean value = (Boolean) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.util.Date")) { Method m = model.getClass().getMethod("get" + name); Date value = (Date) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value.toLocaleString()); } } } } public static void main(String[] args) { try { testReflect(new VO()); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } }
以上這篇java反射遍歷實體類屬性和類型,并賦值和獲取值的簡單方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在java中使用SPI創(chuàng)建可擴展的應(yīng)用程序操作
這篇文章主要介紹了在java中使用SPI創(chuàng)建可擴展的應(yīng)用程序操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Struts1簡介和入門_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Struts1簡介和入門的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09用Java實現(xiàn)春聯(lián)?支持自定義字體顏色
大家好,本篇文章主要講的是用Java編寫春聯(lián)?支持自定義字體顏色,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01