自个写的COPYPROPERTY

 
package com.ygsoft.czp.ticketsys.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.ygsoft.czp.model.AcUser;
import com.ygsoft.czp.model.WebCzpCzrw;
import com.ygsoft.czp.test.common.ClassPropertyBean;

/**
 *  进行一些JAVABEAN的相关操作 〖功能模块〗: BeanUtil(功能) 〖目 的〗: 〖作 者〗:   * 〖创建日期〗:   〖版 本〗: 1.00<br>
 * 〖版权信息〗:   * 〖更改记录〗: 更改时间、更改人、更改原因、更改内容<br>
 */
public class BeanUtil {
    
    
	/**
	 * 将源对象中的数据拷贝到目标对象中
	 * 
	 * @param target
	 * @param source
	 */
	public static void copyProperty(Object target, Object source) {
		Field[] tarFields = target.getClass().getDeclaredFields();
		Field[] sourFields = source.getClass().getDeclaredFields();
		for (int i = 0; i < tarFields.length; i++) {
			Field f = tarFields[i];
			int n = f.getModifiers();
			// 判断是FINAL字段
			if (Modifier.isFinal(n)) {
				continue;
			}
			f.setAccessible(true);
			String tarType = tarFields[i].getType().toString();
			String name = tarFields[i].getName();
			for (int j = 0; j < sourFields.length; j++) {
				Field temp = sourFields[j];
				int m = temp.getModifiers();
				if (Modifier.isFinal(m)) {
					continue;
				}
				temp.setAccessible(true);
				String sourType = sourFields[j].getType().toString();
				String sourName = sourFields[j].getName();
				if (name.equalsIgnoreCase(sourName)) {
					try {
						if (tarType.equals(sourType)) {
							f.set(target, temp.get(source));

						} else if ((tarType.indexOf("Date") != -1 || (tarType
								.indexOf("Timestamp") != -1) )
								&& sourType.indexOf("String") != -1) {
							String time = (String) temp.get(source);
							if (time != null && !(time.trim().equals(""))) {
								String dateStr = "yyyy-MM-dd HH:mm:ss";
								if (time.trim().length() < 11) {
									dateStr = "yyyy-MM-dd";
								}
								SimpleDateFormat myFmt2 = new SimpleDateFormat(	dateStr);
								Date date = new Date();
								try {
									date = myFmt2.parse(time);
								} catch (ParseException e) {
									System.out
											.println("copyProperty 时不能进行字符串到日期的转换");
									e.printStackTrace();
								}
								f.set(target, date);
							}
						} else if (tarType.indexOf("String") != -1
								&& (sourType.indexOf("Date") != -1 || (sourType
										.indexOf("Timestamp") != -1)||(sourType
												.indexOf("CnDate") != -1))) {
							Date time = (Date) temp.get(source);
							if (time != null) {
								SimpleDateFormat myFmt2 = new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss");
								f.set(target, myFmt2.format(time));
							}

						}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
							if(temp.get(source)!=null){
								f.set(target, ""+temp.get(source) );
							}else{
								f.set(target, "");
							}
						}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
							if(temp.get(source)!=null && !temp.get(source).equals("")){
								f.set(target, new Long(temp.get(source).toString()) );
							}
						}else if(tarType.indexOf("Double") != -1 && sourType.indexOf("String") != -1){
							if(temp.get(source)!=null && !temp.get(source).equals("")){
								f.set(target, new Double(temp.get(source).toString()) );
							}
						}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Double") != -1){
							if(temp.get(source)!=null){
								f.set(target, ""+temp.get(source) );
							}else{
								f.set(target, "");
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}  
				}

				temp.setAccessible(false);
				
			}
			f.setAccessible(false);
		}
	}

	/**
	 * 将源对象中的数据拷贝到目标对象中
	 * 
	 * @param target
	 * @param source
	 * @param namesList 如果传入某个属性,则某个属性不进行COPY
	 * 
	 */
	public static void copyProperty(Object target, Object source,List namesList) {
		Field[] tarFields = target.getClass().getDeclaredFields();
		Field[] sourFields = source.getClass().getDeclaredFields();
		for (int i = 0; i < tarFields.length; i++) {
			Field f = tarFields[i];
			int n = f.getModifiers();
			// 判断是FINAL字段
			if (Modifier.isFinal(n)) {
				continue;
			}
			f.setAccessible(true);
			String tarType = tarFields[i].getType().toString();
			String name = tarFields[i].getName();
			if(namesList.contains(name)){
				continue;
			}
			for (int j = 0; j < sourFields.length; j++) {
				Field temp = sourFields[j];
				int m = temp.getModifiers();
				if (Modifier.isFinal(m)) {
					continue;
				}
				temp.setAccessible(true);
				String sourType = sourFields[j].getType().toString();
				String sourName = sourFields[j].getName();
			
				if (name.equalsIgnoreCase(sourName)) {
					try {
						if (tarType.equals(sourType)) {
							f.set(target, temp.get(source));
						} else if ((tarType.indexOf("Date") != -1 || (tarType
								.indexOf("Timestamp") != -1))
								&& sourType.indexOf("String") != -1) {
							String time = (String) temp.get(source);
							if (time != null && !(time.trim().equals(""))) {
								String dateStr = "yyyy-MM-dd HH:mm:ss";
								if (time.trim().length() < 11) {
									dateStr = "yyyy-MM-dd";
								}
								SimpleDateFormat myFmt2 = new SimpleDateFormat(
										dateStr);
								Date date = new Date();
								try {
									date = myFmt2.parse(time);
								} catch (ParseException e) {
									System.out
											.println("copyProperty 时不能进行字符串到日期的转换");
									e.printStackTrace();
								}
								f.set(target, date);
							}
						} else if (tarType.indexOf("String") != -1
								&& (sourType.indexOf("Date") != -1 || (sourType
										.indexOf("Timestamp") != -1))) {
							Date time = (Date) temp.get(source);
							if (time != null) {
								SimpleDateFormat myFmt2 = new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss");
								f.set(target, myFmt2.format(time));
							}

						}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
							f.set(target, new Long(temp.get(source).toString()) );
						}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
							f.set(target, ""+temp.get(source) );
						}
					} catch (IllegalArgumentException e) {

						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
				}

				temp.setAccessible(false);
			}
			f.setAccessible(false);
		}
	}
	  //
	    private static ConcurrentHashMap<Class,  Field[]> classPropertyMap = new ConcurrentHashMap<Class,  Field[]>();

	    /**
	     * 由类得到类的所有属性,并进行缓存,使得再次使用时可以取到相关信息。
	     * @param cla
	     * @return
	     */
	    public static Field[] getClassProperty(Class cla) {
		Field[] fileds= classPropertyMap.get(cla);
		if (fileds != null) {
		    return fileds;
		}else {
		    fileds = cla.getDeclaredFields();
		    classPropertyMap.put(cla, fileds);
		    return fileds;
		}
	    }
	/**
	 * 将对象转换为一个XML字符串,返回
	 * 
	 * @param target
	 * @param source
	 */
	public static String convertBeanToXML(Object source,String beanName) {
		StringBuffer sb = new StringBuffer();
		 Field[] sourFields = getClassProperty(source.getClass());
		 beanName = beanName.equals("")?source.getClass().getName().substring(source.getClass().getName().lastIndexOf(".")+1):beanName;
			sb.append("<"+beanName+">");
			for (int j = 0; j < sourFields.length; j++) {
				Field temp = sourFields[j];
				int m = temp.getModifiers();
				if (Modifier.isFinal(m)) {
					continue;
				}
				temp.setAccessible(true);
				String sourName = sourFields[j].getName();
				sb.append("\r\n\t<"+sourName+">");
				try {
				    if(temp.get(source) != null){
					   sb.append(temp.get(source) );
				    }
				} catch (IllegalArgumentException e) {
				    e.printStackTrace();
				} catch (IllegalAccessException e) {
				    
				    e.printStackTrace();
				}
				sb.append("</"+sourName+">");
		
				temp.setAccessible(false);
				
			}
			sb.append("\r\n</"+beanName+">");
		return sb.toString();
	}
	/**
	 * 将对象转换为一个XML字符串,返回
	 * 
	 * @param target
	 * @param source
	 */
	public static String convertBeanToXML(Object source,String beanName,Map map) {
		StringBuffer sb = new StringBuffer();
		Field[] sourFields = getClassProperty(source.getClass());
		beanName = beanName.equals("")?source.getClass().getName():beanName;
		sb.append("<"+beanName+">");
		for (int j = 0; j < sourFields.length; j++) {
			Field temp = sourFields[j];
			int m = temp.getModifiers();
			if (Modifier.isFinal(m)) {
				continue;
			}
			temp.setAccessible(true);
			String sourName = sourFields[j].getName();
			if(map.containsKey(sourName)){
    				sb.append("\r\n\t<"+sourName+">");
    				try {
    				    if(temp.get(source) != null){
    					   sb.append(temp.get(source) );
    				    }
    				} catch (IllegalArgumentException e) {
    				    e.printStackTrace();
    				} catch (IllegalAccessException e) {
    				    
    				    e.printStackTrace();
    				}
    				sb.append("</"+sourName+">");
			}
			temp.setAccessible(false);
		}
		sb.append("\r\n</"+beanName+">");
		return sb.toString();
	}
	 
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值