阅读:3044次
评论:0条
更新时间:2012-10-25
package com.nms.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.LinkedHashMap; /** * @说明 对象操纵高级方法 * @author cuisuqiang * @version 1.0 * @since */ public class ObjectUtil { /** * 复制对象obj,类似于值传递,非引用 */ public synchronized static Object cloneObject(Object obj) throws Exception{ ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(obj); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in =new ObjectInputStream(byteIn); return in.readObject(); } /** * 返回一个对象的属性和属性值的JSON字符串 * 返回格式({id:1},{name:cc},{pass:null}) */ @SuppressWarnings("unchecked") public synchronized static String getProperty(Object entityName) { StringBuffer sb = new StringBuffer(""); try { Class c = entityName.getClass(); Field field[] = c.getDeclaredFields(); for (Field f : field) { Object v = invokeMethod(entityName, f.getName(), null); sb.append("{" + f.getName() + ":" + v + "},"); } if(sb.length() > 0){ sb.delete(sb.length() - 1, sb.length()); } } catch (Exception e) { sb = new StringBuffer(""); } return sb.toString(); } /** * 获得一个对象各个属性的字节流 */ @SuppressWarnings("unchecked") public synchronized static byte[] getPropertyStream(Object entityName) throws Exception { Class c = entityName.getClass(); Field field[] = c.getDeclaredFields(); ByteBuffer bbuf = ByteBuffer.allocate(1024 * 10); for (Field f : field) { Object v = invokeMethod(entityName, f.getName(), null); if(f.getType().toString().equals("int")){// 支持Int bbuf.put(ToolObjectToByte.intToByte(Integer.parseInt(v.toString()))); }else if(f.getType().toString().equals("class java.lang.String")){// 支持 String bbuf.put(v.toString().getBytes()); }else if(f.getType().toString().equals("short")){// 支持 Short bbuf.put(ToolObjectToByte.shortToByte((Short)v)); }else if(f.getType().toString().equals("byte")){// 支持 Byte bbuf.put((Byte)v); } } // 回绕缓冲区 一是将 curPointer 移到 0, 二是将 endPointer 移到有效数据结尾 bbuf.flip(); byte[] byten = new byte[bbuf.limit()]; bbuf.get(byten, bbuf.position(), bbuf.limit()); // 得到目前为止缓冲区所有的数据 return byten; } /** * 获得对象属性的值 */ @SuppressWarnings("unchecked") private synchronized static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); Method method = null; try { method = ownerClass.getMethod("get" + methodName); } catch (SecurityException e) { } catch (NoSuchMethodException e) { return " can't find 'get" + methodName + "' method"; } return method.invoke(owner); } /** * 返回一个对象的属性和属性值 */ @SuppressWarnings("unchecked") public synchronized static LinkedHashMap<String,String> getPropertyMap(Object entityName) { LinkedHashMap<String,String> map = new LinkedHashMap<String, String>(); try { Class c = entityName.getClass(); // 获得对象属性 Field field[] = c.getDeclaredFields(); for (Field f : field) { Object v = invokeMethod(entityName, f.getName(), null); map.put(f.getName(), v.toString()); } } catch (Exception e) { map = null; } return map; } }
评论 共 0 条 请登录后发表评论