/**
* 将返回值的数据null 转为0
* @param o
* @param <T>
* @return
*/
public static <T> T reflect(T o) {
if (o != null) {
Class<? extends Object> cls = o.getClass();
Field[] fields = cls.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
f.setAccessible(true);
try {
if (f.getType() == Double.class && f.get(o) == null) {
f.set(o, 0.0);
}
if (f.getType() == Integer.class && f.get(o) == null) {
f.set(o, 0);
}
if (f.getType() == BigDecimal.class && f.get(o) == null){
f.set(o, new BigDecimal(0));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return o;
}
将一个返回值null 转为0 (利用反射)
最新推荐文章于 2024-10-20 11:01:49 发布