Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. beanUtils.setExcludeNulls(true);
  2. beanUtils.copyProperties(dest, source);
  3.  
  4. public static String[] getNullPropertyNames (Object source) {
  5. final BeanWrapper src = new BeanWrapperImpl(source);
  6. java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
  7.  
  8. Set<String> emptyNames = new HashSet<String>();
  9. for(java.beans.PropertyDescriptor pd : pds) {
  10. Object srcValue = src.getPropertyValue(pd.getName());
  11. if (srcValue == null) emptyNames.add(pd.getName());
  12. }
  13. String[] result = new String[emptyNames.size()];
  14. return emptyNames.toArray(result);
  15. }
  16.  
  17. // then use Spring BeanUtils to copy and ignore null
  18. public static myCopyProperties(Object, src, Object target) {
  19. BeanUtils.copyProperties(src, target, getNullPropertyNames(src))
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement