使用JAVA反射机制移除对象中字符串属性的空格。
注:如果属性本身为“”,也会被替换。 需要过滤的情况下,object!=null之后追加判断即可
publicstaticvoidobjecttotrim (objectobj ) throws IllegalAccessException {
映射图=new hashmap (;
field [ ] declared fields=obj.getclass ().getDeclaredFields;
场域:取消场域(for ) {
String type=field.getType ().getCanonicalName );
if (string utils.equals (Java.lang.string ),type ) ) ) ) ) ) ) )。
Field.setaccessible(true;
objectobject=field.get(obj;
对象!=null ) {
String trim=object.toString ().replace (',' );
map.put(field.getname )、trim );
}
}
}
场域:取消场域(for ) {
if(map.get(field.getname ) )!=null ) {
strings=map.get(field.getname ();
Field.setaccessible(true;
Field.set(obj,s );
}
}
}
publicstaticvoidmain (string [ ] args ) throws IllegalAccessException { )。
IdCode idCode=new IdCode (;
idcode.setdeleteflag(1;
idcode.set create date (local datetime.now );
idCode.setDeptId (' );
idcode.seteducationurl(abvc );
idcode.setidcode(bcDDD );
system.out.println (idcode.tostring () );
system.out.print ln ((==========抽出了空格==========) )
objecttotrim(idcode;
system.out.println (idcode.tostring () );
}