熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java中類似於C語言中Sizeof功能實現(二)

2022-06-13   來源: JSP教程 

  在做了所有這些准備之後下面就是這種圖形遍歷的標准實現
  public static IObjectProfileNode profile (Object obj)
  {
  final IdentityHashMap visited = new IdentityHashMap ();
  
  final ObjectProfileNode root = createProfileTree (obj visited
  CLASS_METADATA_CACHE);
  finishProfileTree (root);
  
  return root;
  }
  
  private static ObjectProfileNode createProfileTree (Object obj
  IdentityHashMap visited
  Map metadataMap)
  {
  final ObjectProfileNode root = new ObjectProfileNode (null obj null);
  
  final LinkedList queue = new LinkedList ();
  
  queueaddFirst (root);
  visitedput (obj root);
  
  final ClassAccessPrivilegedAction caAction =
  new ClassAccessPrivilegedAction ();
  final FieldAccessPrivilegedAction faAction =
  new FieldAccessPrivilegedAction ();
  
  while (! queueisEmpty ())
  {
  final ObjectProfileNode node = (ObjectProfileNode) queueremoveFirst ();
  
  obj = nodem_obj;
  final Class objClass = objgetClass ();
  
  if (objClassisArray ())
  {
  final int arrayLength = ArraygetLength (obj);
  final Class componentType = objClassgetComponentType ();
  
  // Add shell pseudonode:
  final AbstractShellProfileNode shell =
  new ArrayShellProfileNode (node objClass arrayLength);
  shellm_size = sizeofArrayShell (arrayLength componentType);
  
  nodem_shell = shell;
  nodeaddFieldRef (shell);
  
  if (! componentTypeisPrimitive ())
  {
  // Traverse each array slot:
  for (int i = ; i < arrayLength; ++ i)
  {
  final Object ref = Array.get (obj, i);
  
  if (ref != null)
  {
  ObjectProfileNode child =
  (ObjectProfileNode) visited.get (ref);
  if (child != null)
  ++ child.m_refcount;
  else
  {
  child = new ObjectProfileNode (node, ref,
  new ArrayIndexLink (node.m_link, i));
  node.addFieldRef (child);
  
  queue.addLast (child);
  visited.put (ref, child);
  }
  }
  }
  }
  }
  else // the object is of a non-array type
  {
  final ClassMetadata metadata =
  getClassMetadata (objClass, metadataMap, caAction, faAction);
  final Field [] fields = metadata.m_refFields;
  
  // Add shell pseudo-node:
  final AbstractShellProfileNode shell =
  new ObjectShellProfileNode (node,
  metadata.m_primitiveFieldCount,
  metadata.m_refFields.length);
  shell.m_size = metadata.m_shellSize;
  
  node.m_shell = shell;
  node.addFieldRef (shell);
  
  // Traverse all non-null ref fields:
  for (int f = 0, fLimit = fields.length; f < fLimit; ++ f)
  {
  final Field field = fields [f];
  
  final Object ref;
  try // to get the field value:
  {
  ref = field.get (obj);
  }
  catch (Exception e)
  {
  throw new RuntimeException ("cannot get field [" +
  field.getName () + "] of class [" +
  field.getDeclaringClass ().getName () +
  "]: " + e.toString ());
  }
  
  if (ref != null)
  {
  ObjectProfileNode child =
  (ObjectProfileNode) visited.get (ref);
  if (child != null)
  ++ child.m_refcount;
  else
  {
  child = new ObjectProfileNode (node, ref,
  new ClassFieldLink (field));
  node.addFieldRef (child);
  
  queue.addLast (child);
  visited.put (ref, child);
  }
  }
  }
  }
  }
  
  return root;
  }
  
  private static void finishProfileTree (ObjectProfileNode node)
  {
  final LinkedList queue = new LinkedList ();
  IObjectProfileNode lastFinished = null;
  
  while (node != null)
  {
  // Note that an unfinished nonshell node has its child count
  // in m_size and m_children[0] is its shell node:
  
  if ((node.m_size == 1) || (lastFinished == node.m_children [1]))
  {
  node.finish ();
  lastFinished = node;
  }
  else
  {
  queue.addFirst (node);
  for (int i = 1; i < node.m_size; ++ i)
  {
  final IObjectProfileNode child = node.m_children [i];
  queue.addFirst (child);
  }
  }
  
  if (queue.isEmpty ())
  return;
  else
  node = (ObjectProfileNode) queue.removeFirst ();
  }
  }
  
  該代碼是上一篇Java Q&A, "Attack of the Clones."使用的"通過反射克隆"實現的遠親。Tw.WinGwiT.Com如前所述,它緩存了反射元數據來提高性能,並且使用了一個標識散列映射來標記訪問過的對象。profile()方法從寬度優先遍歷中的具有IObjectProfileNode的生成樹的原始對象圖形開始,以合計和分配所有節點尺寸的快速後序遍歷結束。profile()返回一個 IObjectProfileNode,即產生的生成樹的根,它的尺寸就是整個圖形的尺寸。
  當然, profile()的輸出只有當我有一個很好的方法擴展它時才有用。為了這個目的,每個IObjectProfileNode 必須支持由節點訪問者和節點過濾器一起進行的測試:
  
  interface IObjectProfileNode
  {
  interface INodeFilter
  {
  boolean accept (IObjectProfileNode node);
  
  } // End of nested interface
  
  interface INodeVisitor
  {
  /**
  * Pre-order visit.
  */
  void previsit (IObjectProfileNode node);
  
  /**
  * Post-order visit.
  */
  void postvisit (IObjectProfileNode node);
  
  } // End of nested interface
  
  boolean traverse (INodeFilter filter, INodeVisitor visitor);
  
  ...
  
  } // End of interface
  
  節點訪問者只有當伴隨的過濾器為null或者過濾器接收該節點時才對樹節點進行操作。為了簡便,節點的子節點只有當節點本身已經測試時才進行測試。前序遍歷和後序遍歷訪問都支持。來自java.lang.Object處理程序的尺寸提供以及所有初級數據都集中放在一個偽碼內,這個偽碼附屬於代表對象實例的每個"真實"節點。這種處理程序節點可通過IObjectProfileNode.shell()訪問,也可在IObjectProfileNode.children()列表中顯示出來:目的就是能夠編寫數據過濾器和訪問者,使它們可在實例化的數據類型的同一起點上考慮初級數據。
  如何實現過濾器和訪問者就是你的事了。作為一個起點,類ObjectProfileFilters (見本文的download)提供幾種有用的堆棧過濾器,它們可幫助你在節點尺寸、與父節點的尺寸相關的節點尺寸、與根對象相關的節點尺寸等等的基礎上剪除大對象樹。
  ObjectProfilerVisitors類包含IObjectProfileNode.dump()使用的默認訪問者,也包含能夠為更高級的對象浏覽創建XML轉儲的訪問者。將配置文件轉換為SwingTreeModel也是很容易的。
  為了便於理解,我們創建了一個上文提及的兩個字符串排列對象的完整轉儲:
  
  public class Main
  {
  public static void main (String [] args)
  {
  Object obj = new String [] {new String ("JavaWorld"),
  new String ("JavaWorld")};
  
  IObjectProfileNode profile = ObjectProfiler.profile (obj);
  
  System.out.println ("obj size = " + profile.size () + " bytes");
  System.out.println (profile.dump ());
  }
  
  } // End of class
  
  該代碼結果如下:
  obj size = 106 bytes
  106 -> : String[]
  58 (54.7%) -> [0] : String
  34 (32.1%) -> String#value : char[] refcount=
   (%) > length=>
   (%) > prim/ ref fields>
   (%) > length=>
   (%) > [] : String
   (%) > prim/ ref fields>
  
  實際上如前所述內部的字符排列(被javalangString#value訪問) 可被兩個字符串共享即使ObjectProfilerprofile()將該排列的從屬關系指向第一個發現的字符串它還是通知說該排列共享(如它的下一句代碼refcount=所示)
  
  簡單的sizeof()
  ObjectProfilerprofile()創建了一個節點圖形它的尺寸一般來說是原始對象圖形的幾倍如果你只需要根對象尺寸你可以使用更快更有效的方法ObjectProfile
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19338.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.