兩個具有相同屬性的對象之間屬性的拷貝

在處理業務邏輯時,偶爾會有這樣的需求:對象A已經實例化,但是有一些格式上的不符合標準,對象B沒有具體的屬性值但是格式卻符合標準。

一般這樣的情況會出現在DTO對象與VO對象之間。DTO需要實現序列化纔可以傳輸。

這時候就需要對象屬性的拷貝:

public static void copyProperties(Object source, Object target) throws BeansException {  
		    Assert.notNull(source, "Source must not be null");  
		    Assert.notNull(target, "Target must not be null");  
		    Class<?> actualEditable = target.getClass();  
		    PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);  //得到屬性數組
		    for (PropertyDescriptor targetPd : targetPds) {//通過循環對屬性一一賦值
				if (targetPd.getWriteMethod() != null) {//針對某屬性,目標對象是否具有寫方法(setter)
					PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
		        if (sourcePd != null && sourcePd.getReadMethod() != null) {//源對象是否具有讀方法(getter)
					try {
		            Method readMethod = sourcePd.getReadMethod();  
		            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {  
		              readMethod.setAccessible(true);  //強制修改getter的修飾符爲public
		            }  
		            Object value = readMethod.invoke(source);//獲取屬性值
						// 這裏判斷以下value是否爲空 當然這裏也能進行一些特殊要求的處理 例如綁定時格式轉換等等
		            if (value != null) {  
		              Method writeMethod = targetPd.getWriteMethod();  
		              if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {  
		                writeMethod.setAccessible(true);  //強制修改setter方法的修飾符爲public
		              }  
		              writeMethod.invoke(target, value);//賦值
					}
		          } catch (Throwable ex) {  
		            throw new FatalBeanException("Could not copy properties from source to target", ex);  
		          }  
		        }  
		      }  
		    }  
		  } 
順便體會一下java反射機制的強大。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章