利用java反射机制写的一个实体类属性拷贝的工具

package model;

/**

 *  本工具只适合 同类型同名非空属性的拷贝

 */

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BeanUtil {

	/**
	 * @Describe 实体类属性拷贝工具
	 * @param source 源实体类
	 * @param target 目标实体类
	 * @Date 2017年4月21日
	 * @author 402544
	 */
	@SuppressWarnings("rawtypes")
	public void copyProperties(Object source, Object target) {
		/**
		 * 获得目标对象信息
		 */
		Class tClass = target.getClass();

		Field[] tfields = tClass.getDeclaredFields();// 目标属性集合
		if (tfields.length <= 1) {// 判断是否有超类
			Class tSuperClass = tClass.getSuperclass();
			tfields = tSuperClass.getDeclaredFields();// 目标超级累的属性的集合
		}
		/**
		 * 获得源对象信息
		 */
		Class sClass = source.getClass();
		// 通过默认构造方法创建一个新的对象,此方法不适用内部类。
		// Object sourceCopy = sClass.getConstructor(new
		// Class[]{}).newInstance(new Object[]{});
		Field[] sfields = sClass.getDeclaredFields();// 源对象所有属性集合
		if (sfields.length <= 1) {// 判断是否有超类
			Class sSuperClass = sClass.getSuperclass();
			sfields = sSuperClass.getDeclaredFields();// 源对象超类所有属性集合
		}
		for (int i = 0, len = tfields.length; i < len; i++) {// 循环目标对象属性
			Field tfield = tfields[i];
			if (!tfield.isAccessible()) { // 判断属性是否私有
				tfield.setAccessible(true);
			}
			Class<?> tPropertyType = tfield.getType(); // 目标对象属性类型
			// 转换源对象 属性的 类型
			tPropertyType = convertPropertyType(tPropertyType);
			String tFieldName = tfield.getName(); // 目标对象属性名称

			// Method[] method = sClass.getDeclaredMethods(); //获得源对象所有方法。
			for (int j = 0, k = sfields.length; j < k; j++) { // 循环源对象属性
				Field sfield = sfields[j];
				// System.out.println(sfield.getType() +"\t"+ sfield.getName());
				if (!sfield.isAccessible()) { // 判断属性是否私有
					sfield.setAccessible(true);
				}
				Class<?> sPropertyType = sfield.getType(); // 源对象 属性类型
				sPropertyType = convertPropertyType(sPropertyType);
				String sFieldName = sfield.getName(); // 源对象 属性名称
				if (tFieldName.equals(sFieldName) && !"serialVersionUID".equals(sFieldName)) { // 如果属性名称相同
					if (sPropertyType == tPropertyType) { // 类型相同
						// 源对象第一个属性名首字母大写
						String firstLetter = sFieldName.substring(0, 1).toUpperCase();
						// 获取和源对象属性对应的getXXX()方法的名称
						String getMethodName = "get" + firstLetter + sFieldName.substring(1);
						// 获取和源对象属性对应的getXXX()方法
						try {
							Method getMethod = sClass.getMethod(getMethodName, new Class[] {});
							// 调用源对象的getXXX()方法 取值
							Object value = getMethod.invoke(source, new Object[] {});
							if (value != null) {
								// 获取目标属性对应的 setXXX()方法的名称 因为名称相同所以可以通用
								String setMethodName = "set" + firstLetter + sFieldName.substring(1);
								// 获取目标属性对应的setXXX()方法
								Method setMethod = tClass.getMethod(setMethodName, new Class[] { tfield.getType() });
								// 拷贝属性值
								setMethod.invoke(target, new Object[] { value });
							}
						} catch (NoSuchMethodException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SecurityException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		}
	}

	private Class<?> convertPropertyType(Class<?> sPropertyType) {
		if (sPropertyType == String.class) {

		} else if (sPropertyType == Integer.class) {
			sPropertyType = int.class;
		} else if (sPropertyType == Byte.class) {
			sPropertyType = byte.class;
		} else if (sPropertyType == Short.class) {
			sPropertyType = short.class;
		} else if (sPropertyType == Float.class) {
			sPropertyType = float.class;
		} else if (sPropertyType == Double.class) {
			sPropertyType = double.class;
		} else if (sPropertyType == Character.class) {
			sPropertyType = char.class;
		} else if (sPropertyType == Long.class) {
			sPropertyType = long.class;
		} else if (sPropertyType == Boolean.class) {
			sPropertyType = boolean.class;
		}
		return sPropertyType;

	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值