java物件屬性拷貝,不拷貝空值
阿新 • • 發佈:2019-02-09
/**
* 物件拷貝
* 資料物件空值不拷貝到目標物件
*
* @param dataObject
* @param toObject
* @throws NoSuchMethodException
* copy
*/
public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
{
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name) &&PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if(value!=null){
getInstance().setSimpleProperty(tobean, name, value);
}
}
catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
* 物件拷貝
* 資料物件空值不拷貝到目標物件
*
* @param dataObject
* @param toObject
* @throws NoSuchMethodException
* copy
*/
public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
{
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name) &&PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if(value!=null){
getInstance().setSimpleProperty(tobean, name, value);
}
}
catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}