Created
May 3, 2016 06:18
-
-
Save lhcpig/f451b025782e812bf13925b21f594ae2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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