1. 程式人生 > >Java利用反射實現物件之間相同屬性複製

Java利用反射實現物件之間相同屬性複製

package com.utils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName: BeanUtil.java
 *
 * @Description: BeanUtil
 *
 * @author GERRARD
 *
 * @date 2015年1月19日下午1:38:19
 * 
 */
public final class BeanUtil {

	/**
	 * 利用反射實現物件之間相同屬性複製
	 * 
	 * @param source 要複製的
	 * @param to	 複製給
	 */
	public static void copyProperties(Object source, Object target) throws Exception {
		
		copyPropertiesExclude(source, target, null);
	}

	/**
	 * 複製物件屬性
	 * 
	 * @param from
	 * @param to
	 * @param excludsArray
	 *            排除屬性列表
	 * @throws Exception
	 */
	public static void copyPropertiesExclude(Object from, Object to,
			String[] excludsArray) throws Exception {
		
		List<String> excludesList = null;
		
		if (excludsArray != null && excludsArray.length > 0) {
			
			excludesList = Arrays.asList(excludsArray); // 構造列表物件
		}
		
		Method[] fromMethods = from.getClass().getDeclaredMethods();
		Method[] toMethods = to.getClass().getDeclaredMethods();
		Method fromMethod = null, toMethod = null;
		String fromMethodName = null, toMethodName = null;
		
		for (int i = 0; i < fromMethods.length; i++) {
			
			fromMethod = fromMethods[i];
			fromMethodName = fromMethod.getName();
			
			if (!fromMethodName.contains("get"))
				continue;
			// 排除列表檢測
			if (excludesList != null
					&& excludesList.contains(fromMethodName.substring(3)
							.toLowerCase())) {
				
				continue;
			}
			toMethodName = "set" + fromMethodName.substring(3);
			toMethod = findMethodByName(toMethods, toMethodName);
			
			if (toMethod == null)
				continue;
			Object value = fromMethod.invoke(from, new Object[0]);
			
			if (value == null)
				continue;
			// 集合類判空處理
			if (value instanceof Collection) {
				
				Collection<?> newValue = (Collection<?>) value;
				
				if (newValue.size() <= 0)
					continue;
			}
			
			toMethod.invoke(to, new Object[] { value });
		}
	}

	/**
	 * 物件屬性值複製,僅複製指定名稱的屬性值
	 * 
	 * @param from
	 * @param to
	 * @param includsArray
	 * @throws Exception
	 */
	public static void copyPropertiesInclude(Object from, Object to,
			String[] includsArray) throws Exception {
		
		List<String> includesList = null;
		
		if (includsArray != null && includsArray.length > 0) {
			
			includesList = Arrays.asList(includsArray); 
			
		} else {
			
			return;
		}
		Method[] fromMethods = from.getClass().getDeclaredMethods();
		Method[] toMethods = to.getClass().getDeclaredMethods();
		Method fromMethod = null, toMethod = null;
		String fromMethodName = null, toMethodName = null;
		
		for (int i = 0; i < fromMethods.length; i++) {
			
			fromMethod = fromMethods[i];
			fromMethodName = fromMethod.getName();
			
			if (!fromMethodName.contains("get"))
				continue;
			
			// 排除列表檢測
			String str = fromMethodName.substring(3);
			
			if (!includesList.contains(str.substring(0, 1).toLowerCase()
					+ str.substring(1))) {
				continue;
			}
			
			toMethodName = "set" + fromMethodName.substring(3);
			toMethod = findMethodByName(toMethods, toMethodName);
			
			if (toMethod == null)
				continue;
			
			Object value = fromMethod.invoke(from, new Object[0]);
			
			if (value == null)
				continue;
			
			// 集合類判空處理
			if (value instanceof Collection) {
				
				Collection<?> newValue = (Collection<?>) value;
				
				if (newValue.size() <= 0)
					continue;
			}
			
			toMethod.invoke(to, new Object[] { value });
		}
	}

	/**
	 * 從方法陣列中獲取指定名稱的方法
	 * 
	 * @param methods
	 * @param name
	 * @return
	 */
	public static Method findMethodByName(Method[] methods, String name) {
		
		for (int j = 0; j < methods.length; j++) {
			
			if (methods[j].getName().equals(name)) {
				
				return methods[j];
			}
				
		}
		return null;
	}
}