在做了所有这些准备之后,下面就是这种图形遍历的标准实现:
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
闽公网安备 35060202000074号