方法一:
/** * * 遍歷對象屬性值(利用反射實現),可以在需要對 對象中的每個字段都執行相同的處理時使用 * */ public class DisposeUtil { public static Object dispose(Object object){ Field[] field = object.getClass().getDeclaredFields(); // 獲取實體類的所有屬性,返回Field數組 try { for (int j = 0; j < field.length; j++) { // 遍歷所有屬性 String name = field[j].getName(); // 獲取屬性的名字 name = name.substring(0, 1).toUpperCase() + name.substring(1); // 將屬性的首字符大寫,方便構造get,set方法 String type = field[j].getGenericType().toString(); // 獲取屬性的類型 if (type.equals("class java.lang.String")) { // 如果type是類類型,則前面包含"class ",后面跟類名 Method m = object.getClass().getMethod("get" + name); String value = (String) m.invoke(object); // 調用getter方法獲取屬性值 //.....處理開始........ 執行處理方法 //.....處理結束........ m = object.getClass().getMethod("set"+name,String.class); m.invoke(object, t_value); } if (type.equals("class java.lang.Integer")) { Method m = object.getClass().getMethod("get" + name); Integer value = (Integer) m.invoke(object); if (value == null) { m = object.getClass().getMethod("set"+name,Integer.class); m.invoke(object, 1); } } if (type.equals("class java.lang.Boolean")) { Method m = object.getClass().getMethod("get" + name); Boolean value = (Boolean) m.invoke(object); if (value == null) { m = object.getClass().getMethod("set"+name,Boolean.class); m.invoke(object, false); } } if (type.equals("class java.util.Date")) { Method m = object.getClass().getMethod("get" + name); Date value = (Date) m.invoke(object); if (value == null) { m = object.getClass().getMethod("set"+name,Date.class); m.invoke(object, new Date()); } } // 如果有需要,可以仿照上面繼續進行擴充,再增加對其它類型的判斷 } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return object; } }
方法二:
public class test { public static void main(String[] args) throws Exception{ User e = new User(); reflect(e); } public static void reflect(User e) throws Exception{ Class cls = e.getClass(); Field[] fields = cls.getDeclaredFields(); for(int i=0; i<fields.length; i++){ Field f = fields[i]; f.setAccessible(true); System.out.println("屬性名:" + f.getName() + " 屬性值:" + f.get(e)); } } }
方法三(實體類):
//java中遍歷實體類,獲取屬性名和屬性值 public static void testReflect(Object model) throws Exception{ for (Field field : model.getClass().getDeclaredFields()) { field.setAccessible(true); System.out.println(field.getName() + ":" + field.get(model) ); } }
方法四(實體類或拓展類):
public static void test2(Object obj) { try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { System.out.println(name+":"+ propertyUtilsBean.getNestedProperty(obj, name)); } } } catch (Exception e) { e.printStackTrace(); } }
pom.xml需要配依賴
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>