/** * java讀取文件中的屬性類型 * @param model * @return * @throws Exception */ public static Map<String,String> getModelAttriButeType(Object model) throws Exception{ Field[] field = model.getClass().getDeclaredFields(); //獲取實體類的所有屬性,返回Field數組 Map<String,String> map = new HashMap<String, String>(); for(int j=0 ; j<field.length ; j++){ //遍歷所有屬性 String name = field[j].getName(); //獲取屬性的名字 //System.out.print("attribute name:"+name); 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 = model.getClass().getMethod("get"+name); String value = (String) m.invoke(model); //調用getter方法獲取屬性值 if(value != null){ System.out.println("attribute value:"+value); } }*/ type = type.replace("class ", ""); //System.out.println("=>:"+type); map.put(name, type); } return map; }