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