Java copyProperties


自分でcopyPropertiesメソッドを書いて、apacheとspringの強さはありませんが、機能は自分で十分で、サードパーティのライブラリを導入する必要はありません.
 
package bean;

import java.lang.reflect.Method;

/**
 * @version 1.0
 * @author fireinjava
 * @data Dec 11, 2009
 * @  :             get(  sourceBean) set(  targetBean)  
 */
public class PropertyMethod {

	private String propertyName;
	private Method methodRead;
	private Method methodWrite;
	
	public String getPropertyName() {
		return propertyName;
	}
	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
	public Method getMethodRead() {
		return methodRead;
	}
	public void setMethodRead(Method methodRead) {
		this.methodRead = methodRead;
	}
	public Method getMethodWrite() {
		return methodWrite;
	}
	public void setMethodWrite(Method methodWrite) {
		this.methodWrite = methodWrite;
	}
	
}

 
package bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author fireinjava
 * @data Dec 11, 2009
 * @  :  Bean(     JavaBean,            )
 */
public class BeanUtils {
	
	public static void copyProperties(Object source, Object target) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		copyProperties(source, target, null);
	}

	/**
	 *             Bean              (     )
	 */
	public static void copyProperties(Object source, Object target, String[] ignoreProperties)
			throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		if (source == null)
			throw new IllegalArgumentException("Source must not be null");
		else if (target == null)
			throw new IllegalArgumentException("Target must not be null");

		List<String> ignoreList = null;
		if (ignoreProperties != null)
			ignoreList = Arrays.asList(ignoreProperties);

		List<PropertyMethod> listPm = new ArrayList<PropertyMethod>();
		Map<String, Method> mapMethodWrite = new HashMap<String, Method>();
		Map<String, Method> mapMethodRead = new HashMap<String, Method>();

		Method[] methodTarget = target.getClass().getMethods();
		for (Method mt : methodTarget)
			if (mt.getName().startsWith("set")) {
				String propertyName = mt.getName().substring(3, 4).toLowerCase() + mt.getName().substring(4);
				if (ignoreList == null || !ignoreList.contains(propertyName))
					mapMethodWrite.put(propertyName, mt);
			}

		Method[] methodSource = source.getClass().getMethods();
		for (Method ms : methodSource)
			if (ms.getName().startsWith("get"))
				mapMethodRead.put(ms.getName().substring(3, 4).toLowerCase() + ms.getName().substring(4), ms);
			else if (ms.getName().startsWith("is"))
				mapMethodRead.put(ms.getName().substring(2, 3).toLowerCase() + ms.getName().substring(3), ms);

		Iterator<String> iter = mapMethodWrite.keySet().iterator();
		while (iter.hasNext()) {
			String propertyName = iter.next();
			if (mapMethodRead.get(propertyName) != null) {
				PropertyMethod pm = new PropertyMethod();
				pm.setPropertyName(propertyName);
				pm.setMethodRead(mapMethodRead.get(propertyName));
				pm.setMethodWrite(mapMethodWrite.get(propertyName));
				listPm.add(pm);
			}
		}

		for (PropertyMethod pm : listPm) {
			Object[] obj = new Object[1];
			obj[0] = pm.getMethodRead().invoke(source, new Object[0]);
			pm.getMethodWrite().invoke(target, obj);
		}

	}
}

ここで、ListlistPm=new ArrayList();完全な属性および属性Get/setメソッドを格納するために使用されます. PropertyMethodで直接2つのMapを使ってもいいです.
 
他の2つのcopyProperties:
    org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
                           //ソースのTimestampタイプ属性値はnullではありません さもないと誤報になる
    org.springframework.beans.BeanUtils.copyProperties(source, target);