package cn.com.goldenwater.dcproj.utils; import org.apache.commons.beanutils.BeanUtils; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 当把Person类作为BeanUtilTest的内部类时,程序出错
* java.lang.NoSuchMethodException: Property '**' has no setter method
* 本质:内部类 和 单独文件中的类的区别
* BeanUtils.populate方法的限制:
* The class must be public, and provide a public constructor that accepts no arguments.
* This allows tools and applications to dynamically create new instances of your bean,
* without necessarily knowing what Java class name will be used ahead of time */ public class BeanUtil { // Map --> Bean 2: 利用org.apache.commons.beanutils 工具类实现 Map --> Bean public static void transMap2Bean2(Map map, Object obj) { if (map == null || obj == null) { return; } try { BeanUtils.populate(obj, map); } catch (Exception e) { System.out.println("transMap2Bean2 Error " + e); } } /** * 通过Introspector 属性赋值 ,通过list过滤 * @param fromObj * @param toObj */ public static void copyObject(Object fromObj,Object toObj,List list) { if (fromObj == null || toObj == null) { return; } try { Class fromClass = fromObj.getClass(); BeanInfo beanInfo = Introspector.getBeanInfo(toObj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { try { String key = property.getName(); if (list != null && list.contains(key)) { continue; } Field filed = fromClass.getDeclaredField(key); filed.setAccessible(true); if (filed != null) { Object value = filed.get(fromObj); if (value != null) { property.getWriteMethod().invoke(toObj, value); } } } catch (Exception e) { System.out.println("setValue error,continue current pro" +e.getMessage()); } } } catch (Exception e) { System.out.println("copyObject Error" + e); } } /** * 通过BeanUtils.copyProperties 复制,无法过滤属性列表 * @param fromObj * @param toObj */ public static void copyObject1(Object fromObj,Object toObj){ if (fromObj == null || toObj == null) { return; } try { BeanUtils.copyProperties(toObj,fromObj); } catch (Exception e) { System.out.println("copyObject Error" + e); } } /** * 通过BeanUtils.copyProperty 复制 list过滤属性列表 * @param fromObj * @param toObj */ public static void copyObject2(Object fromObj,Object toObj,List list){ if (fromObj == null || toObj == null) { return; } Field []fields = toObj.getClass().getDeclaredFields(); for (Field field : fields){ String name = field.getName(); if (list != null && list.contains(name)) { continue; } try { BeanUtils.copyProperty(toObj, name, fromObj); } catch (Exception e) { System.out.println("copyObject Error" + e); } } } // Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean public static void transMap2Bean(Map map, Object obj) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (map.containsKey(key)) { Object value = map.get(key); // 得到property对应的setter方法 Method setter = property.getWriteMethod(); setter.invoke(obj, value); } } } catch (Exception e) { System.out.println("transMap2Bean Error " + e); } return; } // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map public static Map transBean2Map(Object obj) { if (obj == null) { return null; } Map map = new HashMap(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); String key=""; for (PropertyDescriptor property : propertyDescriptors) { key = property.getName(); // 过滤class属性 if (!"class".equals(key)) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, value); } } } catch (Exception e) { System.out.println("transBean2Map Error " + e); } return map; } }