com.lone.common.util.MyBeanUtils Maven / Gradle / Ivy
The newest version!
package com.lone.common.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
/**
* Title:
* Description:
*/
public class MyBeanUtils
extends org.apache.commons.beanutils.BeanUtils {
private static void convert(Object dest, Object orig) throws
IllegalAccessException, InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException
("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty origDescriptors[] =
( (DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (DynaBean) orig).get(name);
try {
copyProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else if (orig instanceof Map) {
Iterator names = ( (Map) orig).keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (Map) orig).get(name);
try {
copyProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else
/* if (orig is a standard JavaBean) */
{
PropertyDescriptor origDescriptors[] =
PropertyUtils.getPropertyDescriptors(orig);
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(orig, name) &&
PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(orig, name);
copyProperty(dest, 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){
copyProperty(tobean, name, value);
}
}
catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
/**
* 把orig和dest相同属性的value复制到dest中
* @param dest
* @param orig
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyBean2Bean(Object dest, Object orig) throws Exception {
convert(dest, orig);
}
public static void copyBean2Map(Map map, Object bean){
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
for (int i =0;i