`
fireinjava
  • 浏览: 476123 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Java copyProperties

    博客分类:
  • Java
阅读更多

自己写了个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);
		}

	}
}

其中List<PropertyMethod> listPm = new ArrayList<PropertyMethod>();用来存放完整的属性及属性Get/set方法,不借助 PropertyMethod而直接用两个Map也可。

 

另外两种copyProperties:

    org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);

                            //source中的Timestamp类型属性值不可为null 否则会报错

    org.springframework.beans.BeanUtils.copyProperties(source, target);

 

2
0
分享到:
评论

相关推荐

    java Beanutils.copyProperties( )用法详解

    主要介绍了java Beanutils.copyProperties( )用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    BeanMapper:比BeanUtils.copyProperties更快

    一个BeanUtils.copyProperties的小型快速替代。 起因 由于BeanUtils(Spring或Apache Commons)的copyProperties实现是利用反射实现的,它在大量调用时具有比较严重的性能问题。 BeanMapper通过javassist类库实现在...

    commons-beanutils-jesse.zip(20080717更新)

    apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换。 2008-07-17更新: 增加了...

    commons-beanutils-jesse.zip(20080825更新)

    apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换; 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换。 2008-07-17更新: 增加了BeanUtils的copy...

    BeanCopyUtils.java

    继承org.springframework.beans.BeanUtils 修改copyProperties方法赋值时,为null时不进行处理,方便开发工具类

    commons-beanutils-jesse.zip

    apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换. 注:该包在对null的Date转换时会抛出No value...

    java8源码-wx_order:wx_order

    BeanUtils.copyProperties 可以直接copy对象中的内容 过滤采用stream 随机数random 断点调试 读懂报错信息 断言到底是检测什么? 单元测试就一定要断言吗 无用的断言 关于错误 就是{日志+异常} 里层外层的错误报告 ...

    datahandler.rar

    业务代码解耦方案与案例源码,类似于BeanUtils.copyProperties工具,不仅能进行相同字段名相同类型的字段值复制,还能进行不同类型或字段的转换

    shoppingmall2:测试

    shoppingmall2 ... 除此之外,还可以使用BeanUtils.copyProperties,例如Account account = modelMapper.map(AccountDto.Create dto,Account.class); 像这样。 用@Valid等检查域上的对象,并将结果值作为Bi

    客户关系管理系统框架搭建(二)

    public class SysUserGroup implements java.io.Serializable { private Integer id; private String remark; // 备注 private String name; // 部门名称 private String principal; // 部门负责...

Global site tag (gtag.js) - Google Analytics