当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

Java中类似于C语言中Sizeof功能实现(二)


  在做了所有这些准备之后,下面就是这种图形遍历的标准实现:
  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 ();
  
  queue.addFirst (root);
  visited.put (obj, root);
  
  final ClassAccessPrivilegedAction caAction =
  new ClassAccessPrivilegedAction ();
  final FieldAccessPrivilegedAction faAction =
  new FieldAccessPrivilegedAction ();
  
  while (! queue.isEmpty ())
  {
  final ObjectProfileNode node = (ObjectProfileNode) queue.removeFirst ();
  
  obj = node.m_obj;
  final Class objClass = obj.getClass ();
  
  if (objClass.isArray ())
  {
  final int arrayLength = Array.getLength (obj);
  final Class componentType = objClass.getComponentType ();
  
  // Add shell pseudo-node:
  final AbstractShellProfileNode shell =
  new ArrayShellProfileNode (node, objClass, arrayLength);
  shell.m_size = sizeofArrayShell (arrayLength, componentType);
  
  node.m_shell = shell;
  node.addFieldRef (shell);
  
  if (! componentType.isPrimitive ())
  {
  // Traverse each array slot:
  for (int i = 0; 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."使用的"通过反射克隆"实现的远亲。如前所述,它缓存了反射元数据来提高性能,并且使用了一个标识散列映射来标记访问过的对象。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=2
  34 (32.1%) ->
  24 (22.6%) ->
  24 (22.6%) ->
  24 (22.6%) -> [1] : String
  24 (22.6%) ->
  
  实际上,如前所述,内部的字符排列(被java.lang.String#value访问) 可被两个字符串共享。即使ObjectProfiler.profile()将该排列的从属关系指向第一个发现的字符串,它还是通知说,该排列共享(如它的下一句代码refcount=2所示)。
  
  简单的sizeof()
  ObjectProfiler.profile()创建了一个节点图形,它的尺寸一般来说是原始对象图形的几倍。如果你只需要根对象尺寸,你可以使用更快更有效的方法ObjectProfile
相关内容
赞助商链接