java list trimSize_对实体类的所有String类型的成员变量值trim

使用反射,可以对实体类中的所有String类型的成员变量的值执行trim操作

(1)trim/***

* 对object中的所有成员变量的值,执行trim操作

* 即去掉首尾的空格

* @param obj

* @throws SecurityException

* @throws NoSuchFieldException

* @throws IllegalArgumentException

* @throws IllegalAccessException

*/

public static void trimObject(Object obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException

{

if(obj==null){

return;

}

List fieldsList =getAllFieldList(obj.getClass());

for(int i=0;i

Field f=fieldsList.get(i);

Object vObj=getObjectValue(obj,f );

if(f.getType().getName().equals("java.lang.String") && (vObj instanceof String) ){

String str=(String)vObj;

if(str!=null){

str=str.trim();

f.setAccessible(true);

f.set(obj, str);

}

}

}

}

应用场景// 是否清除成员变量的值前后的空格

boolean isTrim2=false;

String isTrim22Str=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING,"is_trim_columnValue");

if(!ValueWidget.isNullOrEmpty(isTrim22Str)){

isTrim2=Boolean.parseBoolean(isTrim22Str);

}

if(isTrim2&&user!=null){

try {

ReflectHWUtils.trimObject(user);

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

(2)把值为空字符串的成员变量设置为null/***

* 把对象中空字符串改为null

* @param obj : 要修改的对象:java bean

* @param isTrim : 是否清除成员变量的值前后的空格

* @throws SecurityException

* @throws NoSuchFieldException

* @throws IllegalArgumentException

* @throws IllegalAccessException

*/

public static void convertEmpty2Null(Object obj,boolean isTrim)

throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{

if(obj==null){

return;

}

List fieldsList =getAllFieldList(obj.getClass());

for(int i=0;i

Field f=fieldsList.get(i);

Object vObj=getObjectValue(obj,f );

if(f.getType().getName().equals("java.lang.String") && (vObj instanceof String) ){

String str=(String)vObj;

if(isTrim){//清除首尾的空格

if(str!=null){

str=str.trim();

}

}

if(SystemHWUtil.EMPTY.equals(str)){

//System.out.println(f.getName());

//System.out.println(f.getType().getName());

f.setAccessible(true);

f.set(obj, null);

}else{

if(isTrim){//清除首尾的空格

f.setAccessible(true);

f.set(obj, str);

}

}

}

}

}

/***

* 不trim

* @param obj

* @throws SecurityException

* @throws NoSuchFieldException

* @throws IllegalArgumentException

* @throws IllegalAccessException

*/

public static void convertEmpty2Null(Object obj) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{

if(obj==null){

return;

}

convertEmpty2Null(obj, false/* isTrim */);

}

应用场景:// 是否清除成员变量的值前后的空格

boolean isTrim2=false;

String isTrim22Str=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING,"is_trim_columnValue");

if(!ValueWidget.isNullOrEmpty(isTrim22Str)){

isTrim2=Boolean.parseBoolean(isTrim22Str);

}

try {

//把对象中空字符串改为null

ReflectHWUtils.convertEmpty2Null(ordersDetail,isTrim2);

if(!ValueWidget.isNullOrEmpty(ordersDetail.getToothOrders())){

ReflectHWUtils.convertEmpty2Null(ordersDetail.getToothOrders(),isTrim2);//不需要再set一遍

}

if(!ValueWidget.isNullOrEmpty(ordersDetail.getProducts())){

ReflectHWUtils.convertEmpty2Null(ordersDetail.getProducts(),isTrim2);//不需要再set一遍

}

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值