| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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的内部类时,程序出错<br>
- * java.lang.NoSuchMethodException: Property '**' has no setter method<br>
- * 本质:内部类 和 单独文件中的类的区别 <br>
- * BeanUtils.populate方法的限制:<br>
- * The class must be public, and provide a public constructor that accepts no arguments. <br>
- * This allows tools and applications to dynamically create new instances of your bean, <br>
- * 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<String, Object> 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<String> 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<String> 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<String, Object> 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<String, Object> transBean2Map(Object obj) {
- if (obj == null) {
- return null;
- }
- Map<String, Object> map = new HashMap<String, Object>();
- 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;
- }
- }
|