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