blob: d6fb869afbac1dc841f76bce7b796be6f9a1b374 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27/*
28 * The contents of this file are subject to the Sun Public License
29 * Version 1.0 (the "License"); you may not use this file except in
30 * compliance with the License. A copy of the License is available at
31 * http://www.sun.com/, and in the file LICENSE.html in the
32 * doc directory.
33 *
34 * The Original Code is HAT. The Initial Developer of the
35 * Original Code is Bill Foote, with contributions from others
36 * at JavaSoft/Sun. Portions created by Bill Foote and others
37 * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
38 *
39 * In addition to the formal license, I ask that you don't
40 * change the history or donations files without permission.
41 *
42 */
43
44package com.sun.tools.hat.internal.model;
45
46import java.util.Vector;
47import java.util.Hashtable;
48import java.util.Enumeration;
49
50import com.sun.tools.hat.internal.util.ArraySorter;
51import com.sun.tools.hat.internal.util.Comparer;
52
53/**
54 * @author A. Sundararajan
55 */
56
57public class ReachableObjects {
58 public ReachableObjects(JavaHeapObject root,
59 final ReachableExcludes excludes) {
60 this.root = root;
61
62 final Hashtable<JavaHeapObject, JavaHeapObject> bag = new Hashtable<JavaHeapObject, JavaHeapObject>();
63 final Hashtable<String, String> fieldsExcluded = new Hashtable<String, String>(); //Bag<String>
64 final Hashtable<String, String> fieldsUsed = new Hashtable<String, String>(); // Bag<String>
65 JavaHeapObjectVisitor visitor = new AbstractJavaHeapObjectVisitor() {
66 public void visit(JavaHeapObject t) {
67 // Size is zero for things like integer fields
68 if (t != null && t.getSize() > 0 && bag.get(t) == null) {
69 bag.put(t, t);
70 t.visitReferencedObjects(this);
71 }
72 }
73
74 public boolean mightExclude() {
75 return excludes != null;
76 }
77
78 public boolean exclude(JavaClass clazz, JavaField f) {
79 if (excludes == null) {
80 return false;
81 }
82 String nm = clazz.getName() + "." + f.getName();
83 if (excludes.isExcluded(nm)) {
84 fieldsExcluded.put(nm, nm);
85 return true;
86 } else {
87 fieldsUsed.put(nm, nm);
88 return false;
89 }
90 }
91 };
92 // Put the closure of root and all objects reachable from root into
93 // bag (depth first), but don't include root:
94 visitor.visit(root);
95 bag.remove(root);
96
97 // Now grab the elements into a vector, and sort it in decreasing size
98 JavaThing[] things = new JavaThing[bag.size()];
99 int i = 0;
100 for (Enumeration e = bag.elements(); e.hasMoreElements(); ) {
101 things[i++] = (JavaThing) e.nextElement();
102 }
103 ArraySorter.sort(things, new Comparer() {
104 public int compare(Object lhs, Object rhs) {
105 JavaThing left = (JavaThing) lhs;
106 JavaThing right = (JavaThing) rhs;
107 int diff = right.getSize() - left.getSize();
108 if (diff != 0) {
109 return diff;
110 }
111 return left.compareTo(right);
112 }
113 });
114 this.reachables = things;
115
116 this.totalSize = root.getSize();
117 for (i = 0; i < things.length; i++) {
118 this.totalSize += things[i].getSize();
119 }
120
121 excludedFields = getElements(fieldsExcluded);
122 usedFields = getElements(fieldsUsed);
123 }
124
125 public JavaHeapObject getRoot() {
126 return root;
127 }
128
129 public JavaThing[] getReachables() {
130 return reachables;
131 }
132
133 public long getTotalSize() {
134 return totalSize;
135 }
136
137 public String[] getExcludedFields() {
138 return excludedFields;
139 }
140
141 public String[] getUsedFields() {
142 return usedFields;
143 }
144
145 private String[] getElements(Hashtable ht) {
146 Object[] keys = ht.keySet().toArray();
147 int len = keys.length;
148 String[] res = new String[len];
149 System.arraycopy(keys, 0, res, 0, len);
150 ArraySorter.sortArrayOfStrings(res);
151 return res;
152 }
153
154 private JavaHeapObject root;
155 private JavaThing[] reachables;
156 private String[] excludedFields;
157 private String[] usedFields;
158 private long totalSize;
159}