1 3 package jodd.bean.modifier; 4 5 import jodd.bean.BeanUtil; 6 import jodd.introspector.ClassDescriptor; 7 import jodd.introspector.DefaultIntrospector; 8 9 12 public class TrimStringsBeanModifier implements BeanModifier { 13 14 public void modify(Object bean) { 15 if (bean == null) { 16 return; 17 } 18 ClassDescriptor bcd = DefaultIntrospector.lookup(bean.getClass()); 19 String [] getters = bcd.getAllBeanGetterNames(); 20 for (int i = 0; i < getters.length; i++) { 21 if (bcd.getBeanSetter(getters[i]) == null) { 22 continue; 23 } 24 onProperty(bean, getters[i]); 25 } 26 } 27 28 void onProperty(Object obj, String name) { 29 try { 30 Object value = BeanUtil.getProperty(obj, name); 31 if (value == null) { 32 return; 33 } 34 if (value instanceof String ) { value = ((String )value).trim(); 36 BeanUtil.setProperty(obj, name, value); 37 } else if (value.getClass().isArray() == true) { 38 if (value instanceof String []) { String [] valueArray = (String []) value; 40 for (int i = 0; i < valueArray.length; i++) { 41 valueArray[i] = valueArray[i].trim(); 42 } 43 } else { 44 Object [] valueArray = (Object []) value; for (int i = 0; i < valueArray.length; i++) { 46 if (valueArray[i] instanceof String ) { 47 valueArray[i] = ((String )valueArray[i]).trim(); 48 } 49 } 50 } 51 } 52 } catch (Exception ex) { 53 } 55 } 56 } 57 | Popular Tags |