对象工具类
来源:互联网 发布:mac能玩魔兽世界吗 编辑:程序博客网 时间:2024/06/05 10:47
package com.yunmall.framework.core.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.apache.commons.beanutils.BeanUtils;import com.yunmall.framework.core.log.LogTrace;/** * 对象工具类 * * @version 1.0 * @createDate 2012-8-6 * @modifyDate 2012-8-14 */public class BeanUtil extends BeanUtils { private static final LogTrace log = new LogTrace("BeanUtil"); /** * 创建bean的拷贝 * * @param bean * 源 * @return bean的 拷贝 */ public static Object cloneBean(Object bean) { try { return BeanUtils.cloneBean(bean); } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * 将源对象中的属性值set给目标对象中名称相同的属性 * * @param dest * 目标对象 * @param orig * 源对象 */ public static void copyProperties(Object dest, Object orig) { try { BeanUtils.copyProperties(dest, orig); } catch (Exception e) { log.error(e.getMessage()); } } /** * 将对象序列化 * * @param obj * 要序列化的对象 * @return 序列化后的流 */ public static ByteArrayOutputStream objToByte(Object obj) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); oos.writeObject(obj); } catch (IOException e1) { log.error(e1.getMessage()); } finally { try { oos.close(); } catch (IOException e) { log.error(e.getMessage()); } } return bos; } /** * 反序列化 * * @param byt * 对象的序列化字节 * @return 解析后的对象 */ public static Object byteToObj(byte[] byt) { ObjectInputStream ois = null; Object obj = null; try { ois = new ObjectInputStream(new ByteArrayInputStream(byt, 0, byt.length)); obj = ois.readObject(); } catch (IOException e) { log.error(e.getMessage()); } catch (ClassNotFoundException e) { log.error(e.getMessage()); } finally { try { ois.close(); } catch (IOException e) { log.error(e.getMessage()); } } return obj; } /** * 自动遍历PO属性值,并输出 * @author 池京男 * @param obj */ public static String getLogString(Object obj) { if (obj == null) return ""; StringBuffer str = new StringBuffer(); try { Method[] sourceMethods = obj.getClass().getMethods(); for (int i = 0; i < sourceMethods.length; i++) { if (sourceMethods[i].getName().startsWith("get")) { String typeString = sourceMethods[i].getReturnType().getName(); // 类型 String type = getReturnType(typeString); String filed = sourceMethods[i].getName().substring(3); // 属性 if (filed != null && filed.toLowerCase().equals("serialversionuid")) { continue; } String value = ""; Object object = sourceMethods[i].invoke(obj, (Object[]) null); if (object != null) { value = sourceMethods[i].invoke(obj, (Object[]) null).toString(); } str.append(type + " " + filed + "=" + value); str.append("\n"); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return str.toString(); } //返加值类型 private static String getReturnType(Object type) { String string = type.toString(); int index = string.lastIndexOf(".") + 1; return string.substring(index); }}