Skip to content

Instantly share code, notes, and snippets.

@lhcpig
Created May 3, 2016 06:18
Show Gist options
  • Save lhcpig/f451b025782e812bf13925b21f594ae2 to your computer and use it in GitHub Desktop.
Save lhcpig/f451b025782e812bf13925b21f594ae2 to your computer and use it in GitHub Desktop.
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
// then use Spring BeanUtils to copy and ignore null
public static void myCopyProperties(Object, src, Object target) {
BeanUtils.copyProperties(src, target, getNullPropertyNames(src))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment