遍歷物件的屬性,判斷屬性的值,並且賦值,用來替代三元運算
阿新 • • 發佈:2019-01-09
public Object getQueryCriteria1(Object fullProcessDetail) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ParseException { Class cls = fullProcessDetail.getClass(); Field[] fields = fullProcessDetail.getClass().getDeclaredFields(); FullProcess fp = new FullProcess();SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss"); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; String type = f.getGenericType().toString(); String name = f.getName(); //獲取屬性的名字 name = name.substring(0, 1).toUpperCase() + name.substring(1); //將屬性的首字元大寫,方便構造get,set方法f.setAccessible(true); if ("class java.lang.String".equals(type)) { // String value = (String) f.get(fullProcessDetail); if (value != null && !"".equals(value)) { f.set(fullProcessDetail, ((String) f.get(fullProcessDetail)).trim());} } /* if ("class java.math.BigDecimal".equals(type)) { Method m = cls.getClass().getMethod("get" + name); BigDecimal value = (BigDecimal) m.invoke(cls); //呼叫getter方法獲取屬性值 if (value != null && !"".equals(value)) { f.set(fullProcessDetail, f.get(fullProcessDetail).toString()); } }*/ /* if ("class java.util.Date".equals(type)) { Method m = cls.getClass().getMethod("get" + name); Date value = (Date) m.invoke(cls); //呼叫getter方法獲取屬性值 if (value != null && !"".equals(value)) { f.set(fullProcessDetail, sdf.parse(f.get(fullProcessDetail).toString())); } }*/ } return fullProcessDetail; }