javabean對象自動賦值給另外一個javabean對象

方法1:把JavaBean的from的值自動set給to,省略了本身從from中get而後再set給to

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;java

public static Object convertBean2Bean(Object from, Object to) {   
        try {   
            BeanInfo beanInfo = Introspector.getBeanInfo(to.getClass());   
            PropertyDescriptor[] ps = beanInfo.getPropertyDescriptors();   
  
            for (PropertyDescriptor p : ps) {   
               Method getMethod = p.getReadMethod();   
               Method setMethod = p.getWriteMethod();   
               if (getMethod != null && setMethod != null) {   
                   try {   
                      Object result = getMethod.invoke(from);   
                      setMethod.invoke(to, result);   
                   } catch (Exception e) {   
                      // 若是from沒有此屬性的get方法,跳過   
                      continue;   
                   }   
               }   
            }   
        } catch (Exception e) {   
           e.printStackTrace();   
        }   
  
        return to;   
    }   
 
方法2:
    /**  
     * 不支持to繼承(to是其餘bean的子類)  
     */   

import java.lang.reflect.Field;
import java.lang.reflect.Method;spa

   public static Object coverBean2Bean(Object from, Object to) {   
        Class fClass = from.getClass();   
        Class tClass = to.getClass();   
        // 拿to全部屬性(若是有繼承,父類屬性拿不到)   
        Field[] cFields = tClass.getDeclaredFields();   
        try {   
            for (Field field : cFields) {   
               String cKey = field.getName();   
               // 肯定第一個字母大寫   
               cKey = cKey.substring(0, 1).toUpperCase() + cKey.substring(1);   
  
               Method fMethod;   
               Object fValue;   
               try {   
                    fMethod = fClass.getMethod("get" + cKey);// public方法   
                    fValue = fMethod.invoke(from);// 取getfKey的值   
               } catch (Exception e) {   
                 // 若是from沒有此屬性的get方法,跳過   
                 continue;   
               }   
  
                try {   
                    // 用fMethod.getReturnType(),而不用fValue.getClass()   
                    // 爲了保證get方法時,參數類型是基本類型而傳入對象時會找不到方法   
                    Method cMethod = tClass.getMethod("set" + cKey, fMethod.getReturnType());   
                    cMethod.invoke(to, fValue);   
                } catch (Exception e) {   
                    // 若是to沒有此屬性set方法,跳過   
                    continue;   
                }   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
  
        return to;   
    }   
  
  
 3.打印bean   
  
   /**      * 打印bean信息  
     */   
   public static void printInvoke(Object object) {   
      Method[] ms = object.getClass().getMethods();   
      String str = spare;   
      str += "start log object: " + object.getClass().getSimpleName() + "\r\n";   
      str += spare;   
      System.out.print(str);   
  
      for (int i = 0; i < ms.length; i++) {   
         if (ms[i].getName().indexOf("get") != -1   
             && !ms[i].getName().equals("getClass")) {   
             try {   
                 System.out.println(ms[i].getName() + " = "   
                 + ms[i].invoke(object));   
             } catch (Exception e) {   
                 e.printStackTrace();   
             }   
         }   
      }   
  
     System.out.println(spare);   
   }   
相關文章
相關標籤/搜索