将""过滤变成null
private static final String STRING_TYPE = "java.lang.String";
public static <T> T objFilter(T object) {
if (object == null) {
return object;
}
// 获取所有属性
Class clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//设置属性是可以访问的(包括私有)
field.setAccessible(true);
// 如果是空串,设置为null
if (STRING_TYPE.equals(field.getGenericType().getTypeName())) {
try {
if (ValidateUtil.isEmpty(field.get(object))) {
String name = field.getName();
Class type = field.getType();
Method method = clazz.getMethod("set" + name.replaceFirst(name.substring(0, 1),
name.substring(0, 1).toUpperCase()), type);
method.invoke(object, new Object[]{null});
}
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return object;
}