protected static <T> Boolean trimFieldForStr(T obj) {
if (CommonUtil.isEmpty(obj))
return false;
Class<?> cls = obj.getClass();
Field[] fieldlist = cls.getDeclaredFields();
try {
Field[] fieldSups = cls.getSuperclass().getDeclaredFields();
setTrimValue1(obj, fieldlist);
setTrimValue2(obj, fieldSups);
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
return true;
}
// 设置属性值
private static <T> void setTrimValue1(T obj, Field[] l1)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, UnsupportedEncodingException {
for (Field field : l1) {
boolean accessible = field.isAccessible();
field.setAccessible(true);
if (field.getGenericType().toString().equals(
"class java.lang.String")) {
Method m = (Method) obj.getClass().getMethod(
"get" + getMethodName(field.getName()));
String val = (String) m.invoke(obj);// 调用getter方法获取属性值
if (val != null) {
field.set(obj, val.trim());
}
}
field.setAccessible(accessible);
}
}
// 设置属性值
private static <T> void setTrimValue2(T obj, Field[] l1)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, UnsupportedEncodingException {
for (Field field : l1) {
boolean accessible = field.isAccessible();
field.setAccessible(true);
if (field.getGenericType().toString().equals(
"class java.lang.String")) {
Method m = (Method) obj.getClass().getMethod(
"get" + getMethodName(field.getName()));
String val = (String) m.invoke(obj);// 调用getter方法获取属性值
if (val != null) {
field.set(obj, val.trim());
}
}
field.setAccessible(accessible);
}
}
04-18
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-29
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交