java对象属性拷贝,不拷贝空值
/**
* 对象拷贝
* 数据对象空值不拷贝到目标对象
*
* @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
}
}
}
} 阅读更多
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了