技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

java如何反射遍歷實(shí)體類屬性和類型并賦值和獲取值

來源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-11-04 06:18┆點(diǎn)擊:

  實(shí)例如下:

  import java.lang.reflect.Field;

  import java.lang.reflect.InvocationTargetException;

  import java.lang.reflect.Method;

  import java.util.Date;

  /**

  * 獲取實(shí)體類型的屬性名和類型

  * @param model 為實(shí)體類

  * @author kou 為傳入?yún)?shù)

  */

  public class GetModelNameAndType

  {

  public static void testReflect(Object model) throws SecurityException,

  NoSuchMethodException, IllegalArgumentException,

  IllegalAccessException, InvocationTargetException, InstantiationException

  {

  // 獲取實(shí)體類的所有屬性,返回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();