Member variable value trim for all string types of an entity class

Source: Internet
Author: User

Using reflection, you can perform a trim operation on the value of a member variable of all string types in an entity class

(1) Trim

/*** *  the value of all member variables in object, perform trim operations <br> *  That is, remove the spaces  *  @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<field> fieldslist =getallfieldlist (Obj.getclass ()); for (Int i=0;i<fieldslist.size ( ); 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,&NBSP;STR);}}} 

Application Scenarios

Whether to clear the space before and after the value of the member variable Boolean istrim2=false; String Istrim22str=dictionaryparam.get (constant2.dictionary_group_global_setting, "Is_trim_columnvalue"); 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) Set the member variable with a value of empty string to null

/*** *  to change an object's hollow string to null *  @param  obj :  object to modify: java bean * @ param istrim :  whether to clear the space before and after the value of the member variable  *  @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<field> fieldslist =getallfieldlist (Obj.getclass ()); for (Int i=0;i<fieldslist.size ( ); 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) {//clears the leading and trailing spaces 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) {//clear the trailing space f.setaccessible (true); F.set (OBJ,&NBSP;STR);}}}} /*** *  not 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 */);}

scenario :

Whether to clear the space before and after the value of the member variable Boolean istrim2=false; String Istrim22str=dictionaryparam.get (constant2.dictionary_group_global_setting, "Is_trim_columnvalue"); Valuewidget.isnullorempty (ISTRIM22STR)) {Istrim2=boolean.parseboolean (ISTRIM22STR);} try {//To change the object's hollow string to Nullreflecthwutils.convertempty2null (ORDERSDETAIL,ISTRIM2); Valuewidget.isnullorempty (Ordersdetail.gettoothorders ())) {Reflecthwutils.convertempty2null ( Ordersdetail.gettoothorders (), isTrim2);//Do not need to set again}if (!) Valuewidget.isnullorempty (Ordersdetail.getproducts ())) {Reflecthwutils.convertempty2null ( Ordersdetail.getproducts (), isTrim2);//Do not need to set again}} catch (SecurityException e) {e.printstacktrace ();} catch ( Nosuchfieldexception e) {e.printstacktrace (),} catch (IllegalArgumentException e) {e.printstacktrace ();} catch ( Illegalaccessexception e) {e.printstacktrace ();}


This article is from the "Whuang" blog, make sure to keep this source http://huangkunlun520.blog.51cto.com/2562772/1599348

Member variable value trim for all string types of an entity class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.