下午写了一个属性复制方法,记录如下:
class POUtil{ /** * * Function : 将一个source中的属性到复制到dest * @author : Liaokailin * CreateDate : 2014-6-30 * version : 1.0 * @param <T> * @param dest * @param source * @return * @throws IntrospectionException */ public static <T extends PO> T copyBean(T dest,PO source) throws IntrospectionException{ BeanInfo beanInfo = Introspector.getBeanInfo(dest.getClass()) ; PropertyDescriptor[] pdes = beanInfo.getPropertyDescriptors() ; for(int i = 0,length =pdes.length ;i<length ;i++ ){ PropertyDescriptor pd = pdes[i] ; // System.out.println(pd.getName()); try { PropertyDescriptor sourcePd = new PropertyDescriptor(pd.getName(), source.getClass()) ; Method sourceMethod = sourcePd.getReadMethod() ; Object result = sourceMethod.invoke(source) ; Method pdWriteMethod = pd.getWriteMethod() ; pdWriteMethod.invoke(dest, result) ; } catch (Exception e) { continue ; } } return dest ; } }