/*** 利用Java反射机制
* 当List中的对象属性-金额出现“.00”时,替换成“0”
*@authorczx
* @date 2014-10-29
*@paramlist 要处理的List可能是>类型的List
*@return处理后的List,即替换“.00”为“0”的List*/@SuppressWarnings({"unchecked", "rawtypes"})public staticList amonuntOfList2StringUtil(List list) {
List newList= newArrayList();//非空判断
if (list != null && list.size() > 0) {//遍历循环list
for(Object o : list) {//取得list中存放的对象所属的类
Class c =o.getClass();//取得该类的所有方法数组
Method[] ma =c.getMethods();if (ma.length > 0) {//遍历该方法数组
for(Method m : ma) {//仅处理set方法
if (m.getName().indexOf("set") >= 0) {try{//取得get()方法
Method newm =c.getMethod(m.getName().replace("set", "get"),newClass[] {});// 使用get()方法取得对应属性
Object newo = newm.invoke(o, new Object[] {});//属性满足去空.trim()后和“.00”相等时,替换为“0”
if (newo != null&& ".00".equals(newo.toString().trim())) {// 使用set()方法,设置属性
m.invoke(o, new Object[] { "0"});
}
}catch(IllegalArgumentException e) {
e.printStackTrace();
}catch(IllegalAccessException e) {
e.printStackTrace();
}catch(InvocationTargetException e) {
e.printStackTrace();
}catch(SecurityException e) {
e.printStackTrace();
}catch(NoSuchMethodException e) {
e.printStackTrace();
}
}
}
}
newList.add(o);
}returnnewList;
}returnlist;
}